Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Manoj Gurung
Manoj Gurung
5,318 Points

can anyone help me maintain aspect ratio of background image that fills the div, whatever the screen size ?

.banner { margin: 0 auto; width: 100vw; height:90vh; background: gold; background-image: url(../img/ORdarkgrahue.jpg); background-size: 100% 100%; background-repeat:no-repeat; height: 300px; margin:10; margin-top: 60px; font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif; }

Hi Manoj,

Would you mind posting your code here, or a link to your repo?

1 Answer

Steven Parker
Steven Parker
231,007 Points

As you discovered, your current setting ("background-size: 100% 100%") will stretch the image to fit the element.

The settings you are looking for are:

background-size: cover;
background-position: center;

Note that some of the image will get trimmed off if the element doesn't have the same aspect ratio as the image, but the portion shown will still have its original aspect ratio.