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 CSS Foundations Backgrounds and Borders Advanced Backgrounds

SAMUEL LAWRENCE
PLUS
SAMUEL LAWRENCE
Courses Plus Student 8,447 Points

Is there any difference between the **background-clip** and the **background-origin** property?

Hi is there any significant difference between the background-clip and the background-origin property. Guil was very vague about the two properties. He simply showed the same example with two different property names without really explaining their application. From what I gathered they seem to be doing the exact same thing, even uses the exact same values. So why are there two properties that do the exact same thing with exact same values? Or is there a certain application where one would be better to use than the other? thanks guuys!

5 Answers

Lady Do While
Lady Do While
6,027 Points

The background-origin defines where your image/color starts, whether at the border, padding or content box. The background-clip property defines where you image ends (or is clipped), whether at the border, padding or content box.

Small images wont normally need to be clipped or scaled down, whereas setting a border-origin and then not clipping it could look very weird. Thumbnails, like Zhihao Wang mentioned above is a practical example

Take a look at this article for the best explanation and many visual examples: https://coderwall.com/p/h7dwaq/say-hello-to-background-origin-and-background-clip-css3-new-features

SAMUEL LAWRENCE
PLUS
SAMUEL LAWRENCE
Courses Plus Student 8,447 Points

Hi, I totally get it now. Thanks for your time dude. Merry Christmas to you too.

Zhihao Wang
Zhihao Wang
9,687 Points

Hello Samuel,

These background properties might seem very similar at first glance, but they do have a significance difference..

The background-clip property specifies how the background should be displayed. To be more specific, I'll list some examples.

#home{
     background-image: url('insertimghere');
     background-clip: content-box;
}

This will make it so that the background image will only take up the space of the content. This means that all margin and padding applied to this element will not have that background to it. Basically by specifying content-box for the background-clip, it is where you background is 'clipped' off from so to speak.

In practice, the default value of background-clip is border-box, where the background covers the whole element, here's a link where you can play around with these values and get a sense of what it does: http://www.w3schools.com/cssref/playit.asp?filename=playcss_background-clip

Background-origin is more or less where the background-position should be relative to. It basically sets the origin or where X and Y equal to 0 relative to the value given. Here's an example.

#home{
   background-image: url('insertimghere');
   background-origin: content-box;
}

This means that that where X and Y equals zero will be the top left of the content box only. It will look like it's indented in because the content box does not include padding or margins. Here's a reference link worth checking out: http://www.w3schools.com/cssref/playit.asp?filename=playcss_background-origin

I know these may seem very similar because they look like they do similar things, but it is all dependent on what you need to do.

cheers.

SAMUEL LAWRENCE
PLUS
SAMUEL LAWRENCE
Courses Plus Student 8,447 Points

Hi, thanks for your reply. I think I understand this. But as you said it depends on what you need to do. So could you give another example where it would be better to use background-clip over background-origin and vise versa? Thanks.

SAMUEL LAWRENCE
PLUS
SAMUEL LAWRENCE
Courses Plus Student 8,447 Points

Hi I didn't really get it when you explained it, but after looking at the links you sent I think I got it. the thing is I understand the values and what they do. I was more confused about their application. Because you see because in the course it was used to clip and size a background image that was used for the entire background, my brain sort of got stuck that it will only work for full background images or colors etc. But the example with the smiley made it all clear for me. Huge thanks dude.

Zhihao Wang
Zhihao Wang
9,687 Points

No problem man!

I'll also give you an applicable example like you asked for above. Let's say I was creating a portfolio gallery. I want to create multiple pictures with borders around them to differentiate them from one another.

i.e http://www.dmxzone.com/downloads/images/animated_thumbnail_gallery.png

So, a great way of doing this is using the background-clip property:

.portfolio-images{
     margin: 10px;
     padding: 10px;
     width: 500px;
     height: 500px;
     background-image: url('images/exampleimage.png');
     background-clip: padding-box;
     background-size: cover;
}

As you can see, background-clip property can help serve as a perimeter, a border if you will. This way you can use retina images to display in a fixed area. What I mean is, you don't have to manually size the image down and can just use the background-clip and background-size properties to contain it in one place.

Background-origin, I would say, is better for those small individual item images. Treat it like a position property; wherever you set your origin, your background-position values would be relative to those. I think the w3schools link I referenced up there explains it pretty well.

Happy Holidays!