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 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,534 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,534 Points

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