问题描述
您好,我正在尝试使用 CSS 为我的页面使用背景.图片来自另一个文件夹,我尝试将她引用到 CSS 文件.
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.
CSS 文件 URL 为:/var/www/soFit/BO
The CSS file URL is: /var/www/soFit/BO
图片文件网址为:/var/www/soFit/BO/images/login
The image file URL is: /var/www/soFit/BO/images/login
我的 CSS 文件:
#login-bg
{
background-image:url('/images/login/login_background.jpg');
font-size: 20px;
}
我的 HTML 代码:
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?
推荐答案
去掉图片URL前面的正斜杠,如下:
Remove the leading forward-slash from the image URL, as follows:
#login-bg {
background-image:url('images/login/login_background.jpg');
font-size: 20px;
}
通过使用前导正斜杠,浏览器会尝试从域的 Web 根目录(例如 /var/www/
)而不是当前 (CSS) 文件位置加载图像.
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.
这篇关于CSS 背景图片 URL 路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!