How to override background image defined in CSS wi

2020-06-30 04:34发布

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!

7条回答
Explosion°爆炸
2楼-- · 2020-06-30 04:57

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.

查看更多
我只想做你的唯一
3楼-- · 2020-06-30 04:59

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

查看更多
倾城 Initia
4楼-- · 2020-06-30 05:01

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;
查看更多
不美不萌又怎样
5楼-- · 2020-06-30 05:05

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.

查看更多
手持菜刀,她持情操
6楼-- · 2020-06-30 05:07

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.

查看更多
姐就是有狂的资本
7楼-- · 2020-06-30 05:11

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.

查看更多
登录 后发表回答