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

Mystery: background image won't display, tried everything

I downloaded a background image from subtleimages.com and tried to apply it to my project. I was following along with Guil in the CSS deep dive and building my project.

The background-image property simply will not work in my CSS. I have tried so many different ways, and I simply can't get an image to display from CSS. I have tried:

background-image: url('img/oldmoon.jpg');

Someone in another thread, as well as several people on the web, have suggested that you must back out of the CSS file before linking to the image file. So I tried this:

background-image: url('../img/oldmoon.jpg');

To no avail. I don't understand what is going on. It is quite frustrating. I am running out of things to try here.

Could you please tell the complete path of your image?

C:\Users\James\Desktop\Portfolio\img

place this link in your index.html page are you able to open it <a href="img/oldmoon.jpg>Open Image</a>

Which link and where in the HTML should I place it?

5 Answers

So, I decided to delete all my CSS and start afresh. When I did, the background began to work. I think I pinpointed that the problem was this:

background-image: url('../img/oldmoon.jpg'); background: #111;

The problem seems to have been that I used the "background" shorthand to set the color instead of "background-color". This was causing, apparently, the color to override the background image.

Yes exactly

Nice job troubleshooting your issue

I had the same problem with this png that I downloaded from subtle patterns!

Yes the image is placed in the "img" folder and the index.html file is placed in the project folder which I named "portfolio".

add this code html <a href="img/oldmoon.jpg">Open Image</a> in your index.html file are you able to open it. ?

Yes, indeed. It opens to a new page with the image.

okay then set width of that element on which you are applying this background image. try setting width to 400px, same height and refresh

Could you please mention complete path where your image is placed ? i mean is it placed in img folder? and where is your index.html file?

Nothing happened.

.wrapper { background-image: url('img/oldmoon.jpg'); width: 400px; height: 400px; }

Path in CSS file really depends on the location of the CSS snippet you're loading. eg this is the folder structure:

/project

  • index.html /img
    • oldmoon.jpg /css
    • screen.css (suppose you state .wrapper here)

then when you load index.html that contains .wrapper and linked the screen.css, the CSS engine will read the path as: localhost/css/img/oldmoon.jpg (which is incorrect)

in this case, changing it to ../img will solve the issue.