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 badges
Add a ment  | 

9 Answers 9

Reset to default 3
.day body{

Should be

body.day{
  • The first one applies to the <body> which is inside an element with the class day
  • Mine applies to <body> with the day 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