CSS Background image not loading

2019-01-18 23:00发布

I have followed all of the tutorials, which all say the say thing. I specify my background inside of body in my css style sheet, but the page just displays a blank white background. Image is in the same directory as the .html and the .css pages. The tutorial says that

<body background="image.jpeg">

is deprecated, so I use, in css,

body {background: url('image.jpeg');}

no success. Here is the entire css style sheet:

body 
{
background-image: url('nickcage.jpg');
padding-left: 11em;
padding-right: 20em;
font-family:
Georgia, "Times New Roman",
Times, serif; 
color: red;        
}

14条回答
Emotional °昔
2楼-- · 2019-01-18 23:27

for me following line is working for me , I put it in the css of my body ( where image is in same folder in which my css and .html files ) :

background: url('test.jpg');

Other thing that you must care about is , be careful about image extension , be sure that your image has .jpeg or .jpg extension. Thanks

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-18 23:27

The path to the image should be relative, that means if css file is in css folder than you should start path with ../ such as

body{
   background-image: url(../images/logo.jpg);
}

this is because you have to get back to home dir by ../ path and then /images/logo.jpg path to the image file. If you are including css file in html itself

body{
   background-image: url(images/logo.jpg);
}
this code will work just fine.

查看更多
萌系小妹纸
4楼-- · 2019-01-18 23:29

I had the same issue. The problem ended up being the path to the image file. Make sure the image path is relative to the location of the CSS file instead of the HTML.

查看更多
beautiful°
5楼-- · 2019-01-18 23:29

I had changed the document root for my wamp-server-installed apache web server. so using the relative url i.e. (images/img.png) didn't work. I had to use the absolute url in order for it to work i.e.

background-image:url("http://localhost/project_x/images/img.png");

查看更多
爷、活的狠高调
6楼-- · 2019-01-18 23:31

If you place image and css folder inside a parent directory suppose assets then the following code works perfectly. Either double quote or without a double quote both work fine.

body{
   background: url("../image/bg.jpg");
}

In other cases like if you call a class and try to put a background image in a particular location then you must mention height and width as well.

查看更多
欢心
7楼-- · 2019-01-18 23:34

First of all, wave bye-bye to those quotes:

background-image: url(nickcage.jpg); // No quotes around the file name

Next, if your html, css and image are all in the same directory then removing the quotes should fix it. If, however, your css or image are in subdirectories of where your html lives, you'll want to make sure you correctly path to the image:

background-image: url(../images/nickcage.jpg); // css and image live in subdorectories

background-image: url(images/nickcage.jpg); // css lives with html but images is a subdirectory

Hope it helps.

查看更多
登录 后发表回答