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 Responsive Web Design and Testing Adjust the Profile Page and Header

Andrew Zhao
Andrew Zhao
3,267 Points

In CSS, is padding considered to be part of the width of an element?

For example, let's say I have the following rule:

div#element {
    width:400px;
    padding: 0 20px;
}

Will this produce a div that is 400px and constricts the inner contents by 20px on the left side and 20px on the right side?

OR

Will this produce a 440px div?

2 Answers

Alex Heil
Alex Heil
53,547 Points

hey Andrew Zhao ,

by default padding adds the additional units to the element. so from your example the width of 400px with the 20px padding on both sides would end up in a 440px element.

however there's a nice thing that can change this behaviour by changing the box-model to border-box. with this when you tell an element to be 400px and then add padding it will stay at 400px and the padding will be auto-calculated (substracted) from it. to use it you write it like this:

box-sizing: border-box;

hope that helps and have a nice day ;)

Andrew Zhao
Andrew Zhao
3,267 Points

Hey there Alex,

Thank you for your accurate, thoroughly helpful, and very kindly answer.

:)

Regards, AZ

Alex Heil
Alex Heil
53,547 Points

you're welcome andrew, great to hear this helped you out ;)