Rune Lyall
Rune Lyall

Reputation: 29

body class not working

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

Answers (9)

Bram Vanroy
Bram Vanroy

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

vyakhir
vyakhir

Reputation: 1793

Change your css to

body.night { ... }

or add your night/day classes to html element instead.

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

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

Mihai Matei
Mihai Matei

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

Quentin
Quentin

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

Billy Moat
Billy Moat

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

Kos
Kos

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

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382294

Use

body.night {

to select the body of class night.

Upvotes: 1

gdoron
gdoron

Reputation: 150283

.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{

Upvotes: 3

Related Questions