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 ?
Asked
Active
Viewed 5,603 times
2 Answers
17
Just set the height to 100%
and the width to auto
:
.foo {
background-size: auto 100%;
}

Sampson
- 265,109
- 74
- 539
- 565
-
2Yes, but he want to other way around, height 100% width auto. – Karl-André Gagnon Mar 11 '14 at 19:44
-
So swap the values; am I missing something? – Sampson Mar 11 '14 at 19:46
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