CSS background image URL path

2019-03-25 13:27发布

Hello Im trying to use background for my page using CSS. The image is from another folder and I try to reference her to CSS file.

The CSS file URL is: /var/www/soFit/BO

The image file URL is: /var/www/soFit/BO/images/login

My CSS file:

#login-bg   
{

    background-image:url('/images/login/login_background.jpg');
    font-size: 20px;
}

My HTML code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<link rel="stylesheet" type="text/css" href="styles.css" />

</head>

<body id="login-bg"> 

<form action="" method="POST">

Username:  <input type="text" name="username" > 
<br>
Password:  <input type="text" name="password" > 
<br>
<input type="submit" value="enter"> 

</form>

</body>
</html>

Why I cant see my background image?

3条回答
一夜七次
2楼-- · 2019-03-25 14:02

just write

background-image:url('images/login/login_background.jpg');

when you write /images it means images folder is coming from the root directory of the website. which in this case is not correct.

查看更多
别忘想泡老子
3楼-- · 2019-03-25 14:08

Remove the leading forward-slash from the image URL, as follows:

#login-bg {
    background-image:url('images/login/login_background.jpg');
    font-size: 20px;
}

By using the leading forward-slash, browser tries to load the image from the web root directory of the domain (e.g /var/www/) instead of the current (CSS) file location.

查看更多
Bombasti
4楼-- · 2019-03-25 14:24

Try this:

#login-bg   
{

    background-image:url('images/login/login_background.jpg');
    font-size: 20px;
}

Using the slash '/' at the beginning of the route makes it an absolute route, not a relative route, which is what you want.

查看更多
登录 后发表回答