简体   繁体   中英

CSS background-image

Are there any common pitfalls to using this attribute? I can't seem to get it to work. The image is in the same directory as my HTML page and I checked the absolute path which is correct. I also tried setting the background to a random color to see if my div placement was off or something, but the color showed.

I used:

body{

background-image: url('url');

}

If the URL is absolute, there's very little that can go wrong. Try creating an <img> element with the src as the url you're trying to use. If that works, make sure you're specifying it correctly in the CSS. Since you didn't say exactly what you're putting in, it's kind of hard to judge.

The url specified inside url() is relative to the CSS file, so make sure your path to the image is relative to the css file.

eg if your css file is in a folder, and your image in an images folder, and both folders are in the same parent directory, you would access the image in the css with the path ../images/file_name.png

Try,

  • background:url(image.jpg) . Notice, I'm not using background-image , and just background

  • Make sure the path of the image is correct and relative to the CSS file.

  • Make sure the filename of the image is correct. The filename, including the extension, can be case sensitive.

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