Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background-size:contain "y"?

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 ?

like image 629
Brennan Sei Avatar asked Mar 11 '14 19:03

Brennan Sei


People also ask

What is background-size contain?

If the background-size is contain or cover : While preserving its intrinsic proportions, the image is rendered at the largest size contained within, or covering, the background positioning area. If the image has no intrinsic proportions, then it's rendered at the size of the background positioning area.

What is background position y in CSS?

The background-position-y CSS property sets the initial vertical position for each background image. The position is relative to the position layer set by background-origin .

What does background-size 100% 100% mean?

Pretty sure background-size: cover; means the image will fill the element while maintaining its aspect ratio, while background-size: 100%; will just make the image fill 100% width of the element.

What is the size of a background image?

The best website background image size is 1920 x 1080 pixels, according to Malama Online Marketing, and the ideal ratio is 16:9.


1 Answers

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

.foo {
    background-size: auto 100%;
}
like image 200
Sampson Avatar answered Oct 17 '22 09:10

Sampson