13

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 ?

Community
  • 1
  • 1
Brennan Sei
  • 1,033
  • 1
  • 9
  • 12

2 Answers2

17

Just set the height to 100% and the width to auto:

.foo {
    background-size: auto 100%;
}
Sampson
  • 265,109
  • 74
  • 539
  • 565
0

I think this will do it:

.myDiv { background-size: cover; }

See the details here: http://www.w3schools.com/cssref/css3_pr_background-size.asp

Harvey A. Ramer
  • 763
  • 7
  • 13