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 How to Make a Website Customizing Colors and Fonts Use Color in CSS

Why define values for both <body> and <p>?

Just curious...

If I define a <body> color value won't it also change <p> as well? Why would I have to dictate the colors for each, If they have the same outcome?

For instance

body {

fff;

}

gives me the same as

p {

fff;

}

Thanks!

SERGIO RODRIGUEZ
SERGIO RODRIGUEZ
17,532 Points

Strictly speaking, you don't have to specify the color for each and every html element in the css file; for example if you want your complete page to have blue background you could simply write:

body {
background-color: blue;
}

But, if by any chance, you wanted a specific h2 tag (with class="myclass") to have a red background, you would write something like this:

.myclass {
background-color: red;
}

In this case, even though you are saying that you want a blue background for all the page, the h2 tags with class="myclass" will have a red background because their css has more specificity than the body's css.

Specificity is the way that css works, that's why sometimes you may want to "rewrite" a previous more general rule with a more specific one.

If you are going to definitely leave the same color for the body and the paragraphs, you won't need the css for the <p> tags.

Hope this helps,

Sergio

Please remember to select the best answer, this will help improve the quality of the forums,.

1 Answer

Robert Karlsson
Robert Karlsson
8,021 Points

It may seem that they have the same effect and they do, sort of. In your example the color of the text in the document will turn white in both cases, but the setting the color on body have an effect on more than that. Below is an example of how setting color on both body and p which overwrites the color of the text, but does not overwrite the border color of the div.

<html>
  <head>
    <style>
    body { 
      color: red;} 
    div {
      border: 2px solid;
    } 
    p {
      color: blue;
    }
    </style>
  </head>
  <body>
    <div>
      <p>Hello</p>
    </div>
  </body>
</html>

The border of the div is still red, but the text inside is blue. This is because the color rule also covers things like borders and when we put it in the body the p is a child and therefore gets the color. The color tag in p itself wont overwrite the border color since it needs another attribute to target itself, border-color. Children of p will have a blue border.

The sum of it all. The color attribut have an effect on more than just text and by specifically setting the p color to red overwrites it just for the paragraph element, but the rule still have an effect on all other elements.