简体   繁体   English

CSS背景图片不起作用

[英]CSS background-image does not work

It does not work When I followed the answer from similar questions 当我遵循类似问题的答案时,它不起作用

My css file...... 我的css文件...

.tail{
    height:300px;
    width:300px;
    background-image:url('C:\Users\joseph\Documents\GitHub\Spring2014\CMP342\MainProject\WebContent\WEB-INF\img\login.png');
}

in html 在HTML

<div class="tail">
</div>

path for image: checked linked in html: checked 图片的路径:已检查链接到html:已检查

You need to use the file:// URI Scheme . 您需要使用file:// URI Scheme

.tail{
    height:300px;
    width:300px;
    background-image:url('file://c:/Users/joseph/Documents/GitHub/Spring2014/CMP342/MainProject/WebContent/WEB-INF/img/login.png');
}

If your site is not on your local computer, then you can upload it to a service like imgur.com , and link to the hosted image. 如果您的站点不在本地计算机上,则可以将其上载到imgur.com之类的服务,然后链接到托管图像。

You just have to locate the image path starting fron the path where your have you index file. 您只需要从索引文件所在的路径开始定位图像路径即可。

Let say your index.html is located in MainProject then you have to use it like this: 假设您的index.html位于MainProject那么您必须像这样使用它:

.tail{
    height:300px;
    width:300px;
    background-image:url('WebContent\WEB-INF\img\login.png');
}

This is not correct path don't use backslash for path in css property. 这是不正确的路径,请不要在CSS属性中使用反斜杠作为路径。 CSS doesn't allow the backslash (\\) CSS不允许反斜杠(\\)

background-image:url('C:\Users\joseph\Documents\GitHub\Spring2014\CMP342\MainProject\WebContent\WEB-INF\img\login.png');

Try this 尝试这个

background-image:url('../img/login.png');

OR 要么

background-image:url('C:/Users/joseph/Documents/GitHub/Spring2014/CMP342/MainProject/WebContent/WEB-INF/img/login.png');

Try to use / instead of \\ 尝试使用/代替\\

.tail{
    height:300px;
    width:300px;
    background-image:url('file://C:/Users/joseph/Documents/GitHub/Spring2014/CMP342/MainProject/WebContent/WEB-INF/img/login.png');
}

However, I'd suggest you to use relative path instead of absolute path. 但是,我建议您使用相对路径而不是绝对路径。 For example, if your img folder is the same directory as your HTML file then you can use: 例如,如果您的img文件夹与HTML文件位于同一目录,则可以使用:

.tail{
    height:300px;
    width:300px;
    background-image:url('img/login.png');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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