Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

why does't styling body css overwrite the specific elements I already styled?

In css I have styled all the different elements nested within the body tag - headers, links, etc and after all that I have styled the body tag. If css is cascading why doesn't it overwrite all the specific styling I already did (by making everything in the body tag the style I chose for body)? It has just styled the the bits that were "left-over"- i.e. the sections that had no specific styling.

Does the cascading overwrite function just mean that if you restyle something later on that command will be obeyed?

I'm not stuck, I'm just curious.

Thanks :)

2 Answers

You are on point.

But look at it like this: the body tag is a parent tag and every other tag that resides in the body is a child of body.

So if you style the body tag than the whole body will take that styling.

When you style any tag that is a child of body, the styling will be applied to that element you selected.

The other styling still stays for body.

If you wrote a rule for the body tag again in your code ( after the initial body styling ) -> the second rules would be applied.

body {
    background-color: black;
    color: white;
}

//****************************
// some styling in between
//****************************

body {
    background-color: blue;
}

This is the example that you can take a look at.

You can style an exact same element n-times in your CSS and the last declared rule will take an affect.

In the example above:

1st Styling: background color will be black & the font color white 2nd Styling: the background color changes to blue and the font color stays white -> If you wrote a new rule for the font color then that rule would be applied.

Hope this helped you a bit.

great! thank you for explaining it, I understand it now

What you styled earlier probably had a higher specificity than the body selector. Guil explains how specificity works in one of his early videos in CSS Basics. Alternatively, you could google "CSS specificity" to find out more.

Peter has a good point.

My explenation was a bit of a crash course type. But CSS specificity is what you should look into!