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 trialAndrew Zhao
3,267 PointsIn 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
53,547 Pointshey 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
3,267 PointsHey there Alex,
Thank you for your accurate, thoroughly helpful, and very kindly answer.
:)
Regards, AZ
Alex Heil
53,547 Pointsyou're welcome andrew, great to hear this helped you out ;)