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

Issue with background image

Hello treehouse community! I am a beginner to HTML and CSS I have done research trying to solve my current issue but I am trying to apply a static image as the background.

However, the image gets distorted when the browser window is manipulated in anyway, or if there is a toolbar/address bar/anything above it.

How would I make it so that the image does not distort whenever the page is manipulated, but rather it gets cropped? Squarespace.com is a great example of what I'm talking about but I believe they use a video as their background image not something static.

4 Answers

instead of using

background-size: 100% 100%;

try using

background-size: cover;

when you use percentages it will scale the image to the size of its element. So if your element doesn't have a specified size, the image will adjust to whatever size it changes to.

<body> <div id="topleft"> <img src="Background_filter.jpg"/> </div> <img src="Circle logo.png"/> </body>

thats my html so far and the CSS file is listed below

body { background-image:url('Circle logo.png'); background-repeat:no-repeat; background-size:100% 100%; }

.topleft{ position:absolute; top: 0; left: 0; background-image: url('Background_filter.jpg'); width: 100px; height: 100px; }

Thanks it worked. If I am adding another image how do I maintain aspect ratio whenever the window is manipulated?? The background-size: cover; works for the background image but who do I apply the same to another image over the background. Thanks for the help

html { background: url(submission_img.jpg) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; } </style> <center><img src="Circle_logo.png" style=" /></center>

I am trying to keep the aspect ratio of the Circle_logo image during windown manipulation.