Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS

Ben Gillam
Ben Gillam
4,819 Points

Having trouble applying a background image to my project.

I am trying to apply a background image to my first web-site, but it is not being applied. I am writing: body { background: url('img/arctic.jpg'); } into my main.css, but the change is not being applied to any of my pages. When i make other changes to my css, they are applied. I have found a work-a-round by writing <body background="img/arctic.jpg"> into my html documents, but the html validator says this method is obsolete and I don't want to fail my project.

Any ideas on what I'm doing wrong?

Try adding ../ just before img. That will wake you out of the CSS folder that I image your CSS file is stored in.

3 Answers

Try adding ../ just before img. That will wake you out of the CSS folder that I image your CSS file is stored in.

Kevin D
Kevin D
8,646 Points

+1

I'm guessing that the reason why it's working when you place it in your html document is because the HTML file is located one level above the img folder. So when you do img/artic.jpg in your HTML file, it can find the img folder and then your artic.jpg file

However, when you link it in your CSS file, it will try to do the same thing. But this is where the error happens. Your CSS file is probably in it's own sub folder and will be at the same level as the img folder. So it will not be able to find your image. You have to use the '../' to go back a level so it looks like this: ../img/artic.jpg

Marco Boretto
Marco Boretto
29,821 Points

hey Ben,

verify if the url generated links to the right asset. to verify so check the source code. if clicking the link no image will be generated it means the url you specified in the css rule is wrong. maybe is missing ''/'' before or the relative path url("img/arctic.jpg") is to be changed

enjoy coding Marco

Ben Gillam
Ben Gillam
4,819 Points

Thank you for your replies. I had tried writing ..img/arctic, I didn't know you had to write "../", which made everything work.