如何用另一个 CSS 覆盖 CSS 中定义的背景图像?

发布于 2024-10-14 02:04:06 字数 487 浏览 3 评论 0 原文

我有一个“Core.css”,它定义了网站的页面背景图像以及主题。但对于特定页面,我只想更改背景。关于如何在单独的 CSS 文件中实现这一点有什么建议吗?

该页面的 HTML 为:

<head>
    <link rel="stylesheet" type="text/css" href="core.css" />
    <link rel="stylesheet" type="text/css" href="index.css" />

core.css 定义:

body
{
        background-image: url('bg.png');
}

而 index.css 定义:

body
{
    background-image:('homeBg.png');
}

谢谢!

I have a 'Core.css' which defines a page background image, along with the theme, for the site. But for a specific page I want to change just the background. Any suggestions on how this can be achieved in a separate CSS file?

The HTML for the page is:

<head>
    <link rel="stylesheet" type="text/css" href="core.css" />
    <link rel="stylesheet" type="text/css" href="index.css" />

And core.css defines:

body
{
        background-image: url('bg.png');
}

While index.css defines:

body
{
    background-image:('homeBg.png');
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

伤感在游骋 2024-10-21 02:04:06

如果您想将背景图像替换为空(即使其不活动或“将其关闭”),请在下游样式表中使用“none”关键字:

 background-image: none;

If you want to replace the background image with nothing (i.e. make it inactive or "turn it off"), use the "none" keyword in your downstream style sheet:

 background-image: none;
悲凉≈ 2024-10-21 02:04:06

后面定义的背景应该取代前面的背景。因此,如果您有:

Site1.css 其中有:

.img {
    background: ...
}

Site2.css 其中有:

.img {
    background: ...
}

那么如果 Site2.css 包含在页面上的 Site1.css 之后,则 Site2.css .img 将替换 Site1.css 中的 .img。

更新:我不确定为什么正文标签没有被正确替换。你能尝试给它一个类或id,并用它代替body吗?

例如

<body id="backgroundTest">

,然后在 css 文件中执行 #backgroundTest { background-image... }

以防万一,您可以检查 homeBg.png 是否存在以及 index.css 是否存在。 http://yourpage.com/homeBg.pnghttp://yourpage.com/index.css 应该都存在。

background defined later should replace the previous ones. So if you have:

Site1.css which has:

.img {
    background: ...
}

Site2.css which has:

.img {
    background: ...
}

then Site2.css .img would replace .img within Site1.css if Site2.css is included after Site1.css on your page.

UPDATE: I'm not sure why the body tag is not being replaced correctly. Could you try to give it a class or id, and use that instead of body?

e.g.

<body id="backgroundTest">

And then in the css files you would do #backgroundTest { background-image... }

And just in case, could you check if homeBg.png exists and index.css. http://yourpage.com/homeBg.png and http://yourpage.com/index.css should both exist.

夏九 2024-10-21 02:04:06

对于特定页面,您可以在页面中使用 css 规则,使用 !重要。如果您在文件中添加your-selector {background: url("the-path-for-the-bg-image") no-repeat !important;},将覆盖默认背景。

For the specific page you can use a css rule in the page, using !important. If you add your-selector {background: url("the-path-for-the-bg-image") no-repeat !important;} in the file, will override the default background.

忘羡 2024-10-21 02:04:06

在index.css写入中,

 body { background-image:('homeBg.png') !important; 

您可以通过在新文件中在其后写入“!important”来覆盖任何地方定义的任何属性

in index.css write

 body { background-image:('homeBg.png') !important; 

you can override any property defined anywhere by writing "!important" after it in new file

初心 2024-10-21 02:04:06

使用与原始规则相同的选择器在 CSS 规则中设置背景,并在原始规则之后包含新的 CSS 文件,或者确保新规则具有更高特异性的选择器: http://www.w3.org/TR/CSS2/cascade.html#specificity

最后你可以为背景属性赋予 !important 标志,但这通常是最后的手段,也是样式表组织不良的标志。

Either set the background in a CSS rule with the same selector as the original rule, and include your new CSS file after the original one, or make sure your new rule has a selector which has a higher specificity: http://www.w3.org/TR/CSS2/cascade.html#specificity

Finally you could give the background property the !important flag, however that is usually a last resort and the sign of a badly organized style sheet.

雨巷深深 2024-10-21 02:04:06

如果页面背景是在正文中设置的,您可以简单地通过为该特定页面中的正文提供类或 id 并添加(也可以在同一个 css 文件中...)来否决它:

body.someClass {
  background: ...
}

body#someID {
  background: ...
}

(在 < code>body 部分并不是真正需要的,因为类和 id 推翻了选择器)

If the page background is set in the body, you can simply overrule it by giving the body in that specific page a class or an id and add (can also be in the same css file...):

body.someClass {
  background: ...
}

or

body#someID {
  background: ...
}

(in both the body part is not really needed as the class and the id overrule the selector)

未央 2024-10-21 02:04:06

我也有同样的问题。

我最终所做的是在我的样式表中进行覆盖,我不仅将其应用于正文,html:

所以...

#id, html, body { background-color: #FFF }

希望这可以帮助某人。

I had the same problem.

What I ended up doing was in my stylesheet which was doing the override, I applied it to not only body, html:

So...

#id, html, body { background-color: #FFF }

Hope this might help someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文