Secondary aquatic adaptations evolved independently more than 30 times from terrestrial vertebrate ancestors. For decades, non-avian dinosaurs were believed to be an exception to this pattern. Only a few species have been hypothesized to be partly or predominantly aquatic. However, these hypotheses remain controversial, largely owing to the difficulty of identifying unambiguous anatomical adaptations for aquatic habits in extinct animals. Here we demonstrate that the relationship between bone density and aquatic ecologies across extant amniotes provides a reliable inference of aquatic habits in extinct species. We use this approach to evaluate the distribution of aquatic adaptations among non-avian dinosaurs. We find strong support for aquatic habits in spinosaurids, associated with a marked increase in bone density, which precedes the evolution of more conspicuous anatomical modifications, a pattern also observed in other aquatic reptiles and mammals. Spinosaurids are revealed to be aquatic specialists with surprising ecological disparity, including subaqueous foraging behaviour in Spinosaurus and Baryonyx, and non-diving habits in Suchomimus. Adaptation to aquatic environments appeared in spinosaurids during the Early Cretaceous, following their divergence from other tetanuran theropods during the Early Jurassic.