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 trialSika Stanton
2,198 Pointsin CSS how come sometimes you use background: and other times you use background-color: to change the color?
Was working on this challenge and was just curious. Thanks.
/* Complete the challenge by writing CSS below */
body {
text-align: center;
}
p {
color: lightslategrey;
}
header {
background: orange;
color: white;
}
div {
background-color: lightblue;
}
<!DOCTYPE html>
<html>
<head>
<title>Lake Tahoe</title>
<link rel="stylesheet" href="page.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<span>Journey through the Sierra Nevada Mountains</span>
<h1>Lake Tahoe, California</h1>
</header>
<p>
Lake Tahoe is one of the most breathtaking attractions located in California. It's home to a number of ski resorts, summer outdoor recreation, and tourist attractions. Snow and skiing are a significant part of the area's reputation.
</p>
<a href="#">Find out more</a>
<div>
<h2>Check out all the Wildlife</h2>
<p>
As spawning season approaches, the fish acquire a humpback and protuberant jaw. After spawning, they die and their carcasses provide a feast for gatherings of mink, bears, and Bald eagles.
</p>
<a href="#">See the Wildlife</a>
<div>
</body>
</html>
2 Answers
Nick Pfisterer
8,064 PointsHi Sika,
They're interchangeable in this case, so it is a matter of personal preference whether you want to use one, the other or even both; as seen here.
The background property is a shorthand property that lets you declare several background-related properties with one line of code instead of writing them out individually. You do not have to declare every compatible property for the shorthand to work; you can just declare one or a few. So, while it may not be visually consistent, it is perfectly valid to write CSS like this:
header {
background: gray;
}
footer {
background-color: black;
}
I hope that helps!
Marcus Parsons
15,719 Pointsbackground
is the shorthand property for all of the background properties. It includes background-color
so that you don't have to write out the full background-color
property. background
is more versatile because you can declare background colors, images, a background's repeat pattern, the background's size, etc. in one short property instead of defining every single property.
Here's an example:
body {
background: #00ff00 url("smiley.gif") no-repeat fixed;
}
is the same as saying
background-color: #00ff00;
background-image: url("smiley.gif");
background-repeat: no-repeat;
background-position: fixed;
I hope that clears that up for you!