简体   繁体   中英

Why does this css not produce a background image?

body{    
background:#fff url('images/testProfile.jpg') no-repeat right top;
}

I assume this code should work so please suggest useful debugging steps...eg how to adjust image properties and position in extreme ways...

Keep in mind that url() in CSS is relative to the stylesheet and not the page. This might be a problem here.

Most likely because the image isn't being found. Look at the images loaded via the dev tools of a good browser (Like Chrome, Safari or Firefox + firebug). You should be able to see images it tried to load along with the server responses for them. If it's a 404, then you have your image path wrong.

And remember that the image urls are always relative to the stylesheet location, not the page itself. So the relative path may be different than you think.


Debugging example

This page: http://jsfiddle.net/qUfn9/

With css: body { background: url('foo.png') }

Makes the chrome dev tools say this:

在此处输入图片说明

try using an absolute path for the url

body{    
background:#fff url('/images/testProfile.jpg') no-repeat right top;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM