Reputation: 29
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?
Upvotes: 1
Views: 3062
Reputation: 28505
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
.
Upvotes: 1
Reputation: 1793
Change your css to
body.night { ... }
or add your night/day classes to html element instead.
Upvotes: 0
Reputation: 167202
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. :)
Upvotes: 1
Reputation: 24276
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>
Upvotes: 0
Reputation: 944054
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
Upvotes: 2
Reputation: 21050
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
}
Upvotes: 1
Reputation: 72279
Use body.day
and body.night
selectors instead.
.day body
means a body
tag nested under any tag with class day
.
Upvotes: 2
Reputation: 150283
.day body{
Should be
body.day{
<body>
which is inside an element with the class day
<body>
with the day
class.So as for:
.night body{
Should be:
body.night{
Upvotes: 3