12

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
  • 993
  • 1
  • 8
  • 12

2 Answers2

16

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

.foo {
    background-size: auto 100%;
}
Sampson
  • 259,174
  • 73
  • 529
  • 557
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
  • 734
  • 7
  • 13