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

Rhonda Bradley
Rhonda Bradley
2,010 Points

Why/when "color" vs. "background-color?"

We've used: "background-color" to set the color for the body background, and we've also used "border-color" to set the color for the border. ex: background-color: #fff

Yet to set the color for links, headline text, and nav links we've used only the term "color." ex: color: #999;

Then to set the nav background color we used: "background." ex: background: #599a68;

Could someone please explain what the rules are? Or is it possible we just memorize the rules for each different case?

Thanks very much, Rhonda

2 Answers

Erik McClintock
Erik McClintock
45,783 Points

Rhonda,

The simple "color" property is reserved for font color, as you mentioned, and the difference between the other ones? Background-color and background, border-color and border? The "background" and "border" properties are simply shorthand for the rest, and can set all of their respective properties in one declaration. For example, this:

div {
    background: #52ba3d url('../img/picture.jpg') no-repeat cover;
}

Is the same as writing out this:

div {
    background-color: #52ba3d;
    background-image: url('../img/picture.jpg');
    background-repeat: no-repeat;
    background-size: cover;
}

You just don't have to spend all that time to write out each and every individual property declaration! They allow you to condense it all into the generic, blanket "background" property. The same idea goes for "border-color" and "border". There is no rule as to when to use one over the other, and the reason why you would is to save yourself time and lines of code. Neither way is strictly better than the other, necessarily. It really boils down to personal preference! It's good to become familiar with all of the individual properties that are available to you, but once you are, shorthands are a nice way to achieve the same effect in a quicker fashion!

Hope this helps!

Erik

Rhonda Bradley
Rhonda Bradley
2,010 Points

That's awesome. Thanks so much for making it all crystal clear!