如何用另一个CSS覆盖CSS中定义的背景图片? [英] How to override background image defined in CSS with another CSS?

查看:469
本文介绍了如何用另一个CSS覆盖CSS中定义的背景图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个"Core.css",它定义了网站的页面背景图像以及主题.但是对于特定的页面,我只想更改背景.关于如何在单独的CSS文件中实现此目标的任何建议?

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?

页面的HTML是:

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

core.css定义:

And core.css defines:

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

而index.css定义:

While index.css defines:

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

谢谢!

推荐答案

背景应替换先前的背景.因此,如果您有:

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

Site1.css具有:

Site1.css which has:

.img {
    background: ...
}

Site2.css具有:

Site2.css which has:

.img {
    background: ...
}

然后,如果将Site2.css包括在页面上的Site1.css之后,则Site2.css .img将替换Site1.css中的.img.

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

更新:我不确定为什么body标签没有被正确替换.您可以尝试给它提供一个类或id,然后使用它代替body吗?

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?

例如

<body id="backgroundTest">

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

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

以防万一,可以检查homeBg.png是否存在和index.css. http://yourpage.com/homeBg.png

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.

这篇关于如何用另一个CSS覆盖CSS中定义的背景图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆