CSS 在样式表中定义时不显示背景图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6064965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 00:35:10  来源:igfitidea点击:

Background image doesn't show when defined in stylesheet

cssbackground

提问by edgar

I am very new to css so this maybe a simple answer. I have 2 scenarios and 1 works the other doesn't. I hope someone can help me out.

我对 css 很陌生,所以这可能是一个简单的答案。我有 2 个场景,1 个工作,另一个没有。我希望有人可以帮助我。

WORKS:

作品:

<head>
<style type="text/css"> 
body { 
    background-image:url('views/default/images/home.jpg');
;}
</style>
</head>

DOESN'T WORK:

不工作:

<head>
<link rel="stylesheet" type="text/css" href="views/default/home_style.css" />
</head>

In home_style.css>

在 home_style.css>

body{ 
    background-image:url('views/default/images/home.jpg');
    margin-top: 0px !important; 
    padding-top: 0px !important; 
}

回答by sdleihssirhc

It looks like your CSS file is in the views/default/folder, while the image is in the views/default/images/folder.

看起来您的 CSS 文件在views/default/文件夹中,而图像在views/default/images/文件夹中。

Define image paths in your CSS relative to the CSS file, not the HTML file that displays everything:

在 CSS 中定义相对于CSS 文件的图像路径,而不是显示所有内容的 HTML 文件:

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

回答by JMASTER B

In my case:

就我而言:

I had a CSSfolder. So, inside my CSSfolder I had my style.cssso I was typing inside my style.cssthe following:

我有一个CSS文件夹。所以,在我的CSS文件夹中,我有我的,style.css所以我在里面输入style.css了以下内容:

background: url('img/mybgimg.jpg')

background: url('img/mybgimg.jpg')

instead of

代替

background: url('../img/mybgimg.jpg')

background: url('../img/mybgimg.jpg')

...I hope this can help to anyone who is having the same issue.

...我希望这可以帮助任何有同样问题的人。