admin管理员组文章数量:1356866
Basically what I'm trying to do is this ... I have a site that I want to make change the background day or night ... so day one thing((till around 7pm or so)) and then night till around 7 am or so .. I have a script in place that changes the class of my body tag to reflect two different body elements in my CSS page ..
.night body {
font-size:18px;
color:#999;
background-image:url(nightwolfwall.jpg);
background-attachment:fixed;
background-repeat:no-repeat;
background-color: #000;
}
.day body {
font-size:18px;
color:#999;
background-image:url(daywolfwall.jpg);
background-attachment:fixed;
background-repeat:no-repeat;
background-color: #000;
}
but for the life of me .. I cannot get them to work .. I've even tried to change the class of the body tag manually to test the class ids .. and nothing .. am I doing something wrong in the css?
Basically what I'm trying to do is this ... I have a site that I want to make change the background day or night ... so day one thing((till around 7pm or so)) and then night till around 7 am or so .. I have a script in place that changes the class of my body tag to reflect two different body elements in my CSS page ..
.night body {
font-size:18px;
color:#999;
background-image:url(nightwolfwall.jpg);
background-attachment:fixed;
background-repeat:no-repeat;
background-color: #000;
}
.day body {
font-size:18px;
color:#999;
background-image:url(daywolfwall.jpg);
background-attachment:fixed;
background-repeat:no-repeat;
background-color: #000;
}
but for the life of me .. I cannot get them to work .. I've even tried to change the class of the body tag manually to test the class ids .. and nothing .. am I doing something wrong in the css?
Share Improve this question edited Nov 2, 2012 at 10:55 gdoron 150k59 gold badges302 silver badges371 bronze badges asked Nov 2, 2012 at 10:50 Rune LyallRune Lyall 291 silver badge2 bronze badges9 Answers
Reset to default 3.day body{
Should be
body.day{
- The first one applies to the
<body>
which is inside an element with the classday
- Mine applies to
<body>
with theday
class.
So as for:
.night body{
Should be:
body.night{
Use body.day
and body.night
selectors instead.
.day body
means a body
tag nested under any tag with class day
.
You are using descendant selectors which mean "A body element that is a descendant of an element (of any type) that is a member of night/day".
To match a body element that is a member of a class:
body.classname
Use
body.night {
to select the body
of class night
.
If you have a class of "night" on your body tag then you should reference it in your CSS like so:
body.night {
// css styles
}
You need to give this way:
body.night {}
body.day {}
In the way you give right now, it can be applied for the <html>
tag. So, giving this way:
<html class="day">
<html class="night">
Works in this case. :)
body.night {
/* CODE */
}
body.day{
/* CODE */
}
CSS rules are basically written hierarchical. The most abstract element left and the most specific right. In other words, an element that is inside another is always right from the former.
When a class is assigned to an element, you write the class connected to the element (same with ID's) and not with a space in between.
body.day
is not the same as body .day
. The later targets an element inside body
that has a class day
. This can be any element.
Also note that there is no restriction on how deep an element can be when using this code. For example: body .day
has loads of possible HTML structures. For example:
<body>
<div class="day">
</div>
</body>
But also:
<body>
<div>
<div>
<div>
<div class="day">
</div>
</div>
</div>
</div>
</body>
Note: you can select only direct children of an element by using >
e.g. body > div.test
will only target divs that are immediately inside body
and that have a class test
.
Body tag doesn't have any enclosing tag other than html so your css class should be
CSS
body {
margin:0;
/* other property */
}
.night {
background:#000;
}
HTML
<html>
<head>
</head>
<body class="night">
</body>
</html>
Change your css to
body.night { ... }
or add your night/day classes to html element instead.
本文标签: javascriptbody class not workingStack Overflow
版权声明:本文标题:javascript - body class not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743968323a2570301.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论