Is there a way of making a background fit a div "height" while keeping his ratio ? Like a contain but for "y" only ? according to this answer there isn't Background Image Scaling While Maintaining Aspect Ratio however considering that the most voted answer quoted some css3 possiblities I was wondering if there was a way nowdays to do so in css3 or maybe javascript ?
Is there a way of making a background fit a div "height" while keeping his ratio ? Like a contain but for "y" only ? according to this answer there isn't Background Image Scaling While Maintaining Aspect Ratio however considering that the most voted answer quoted some css3 possiblities I was wondering if there was a way nowdays to do so in css3 or maybe javascript ?
Share Improve this question edited May 23, 2017 at 10:29 CommunityBot 11 silver badge asked Mar 11, 2014 at 19:39 Brennan SeiBrennan Sei 1,0431 gold badge9 silver badges12 bronze badges2 Answers
Reset to default 17Just set the height to 100%
and the width to auto
:
.foo {
background-size: auto 100%;
}
I think this will do it:
.myDiv { background-size: cover; }
See the details here: http://www.w3schools./cssref/css3_pr_background-size.asp