The developmental environment can potentially alter the adult social environment and influence traits targeted by sexual selection such as body size. In this study, we manipulated larval density in male and female Drosophila melanogaster, which results in distinct adult size phenotypes–high (low) densities for small (large) adults–and measured sexual selection in experimental groups consisting of adult males and females from high, low, or a mixture of low and high larval densities. Overall, large adult females (those reared at low larval density) had more matings, more mates and produced more offspring than small females (those reared at high larval density). The number of offspring produced by females was positively associated with their number of mates (i.e. there was a positive female Bateman gradient) in social groups where female size was experimentally varied, likely due to the covariance between female productivity and mating rate. For males, we found evidence that the larval environment affected the relative importance of sexual selection via mate number (Bateman gradients), mate productivity, paternity share, and their covariances. Mate number and mate productivity were significantly reduced for small males in social environments where males were of mixed sizes, versus social environments where all males were small, suggesting that social heterogeneity altered selection on this subset of males. Males are commonly assumed to benefit from mating with large females, but in contrast to expectations we found that in groups where both the male and female size varied, males did not gain more offspring per mating with large females. Collectively, our results indicate sex-specific effects of the developmental environment on the operation of sexual selection, via both the phenotype of individuals, and the phenotype of their competitors and mates.