Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS

Mani Saini
Mani Saini
3,101 Points

What is the difference between background and background-color proptery in CSS?

What is the difference between background and background-color property in CSS?

2 Answers

Tobias Helmrich
Tobias Helmrich
31,601 Points

No, you don't have to use the background property. Just think of background as a shorthand property to write all of the above mentioned properties in one property.

If you want to have a background-color for an image you could either write it with two properties:

body {
    background-color: blue;
    background-image: url('image.jpg');
}

or you could use the shorthand property background:

body {
    background: blue url('image.jpg');
}

I hope that makes it more clear.

Tobias Helmrich
Tobias Helmrich
31,601 Points

Hey Mani,

with the background-color property you can only set the background color of an element. The background property on the other hand is a shorthand property that allows you to set one or more of the following properties: background-clip, background-color, background-image, background-origin, background-position, background-repeat, background-size, background-attachment.

I hope that helps, if you have further questions let me know! :)

Mani Saini
Mani Saini
3,101 Points

So does that mean if i want to change the background color of an image, i will use background property?