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 Basics (2014) The Box Model Padding

Sahar Nasiri
Sahar Nasiri
7,454 Points

Percentage size of paddings

I didn't understand the differences between percentage size and pixel size of the paddings!

3 Answers

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,075 Points

Well from how I understand it, padding adds a space between a box and the contents within it. So if you specify a pixel based padding then, the content will be pushed away from the box the specified pixels.

Percentages work based on the box's main width, so for example if you have a box, lets say we call it, #content-box, and give it a width of lets say 300px, and a padding of 1% 5% 5% 10% (not a realistic value at all):

#content-box {
width: 300px;
padding: 1% 5% 5% 10%;
}

Then the content will be pushed 1% of 300 (in pixels) from the top of the box, pushed 5% of 300 (in pixels) from the right and bottom of the box, and finally pushed 10% of 300 (in pixels) from the left of the box.

  • padding-top = 1% of 300px = 300 x .01 = 3px;
  • padding-right = 5% of 300px = 300 x .05 = 15px;
  • padding-bottom = 5% of 300px = 300 x .05 = 15px;
  • padding-left = 10% of 300px = 300 * .10 = 30px;

Hopefully this was enough to help, if you still need help let me know!

Edit: Changed post for readability - Dane E. Parchment Jr. (Moderator)

Jason DeValadares
Jason DeValadares
7,190 Points

To add to Dane's answer if the parent element is sized in percentages, and the padding or margin is also set in percentages then that padding/margin will grow and shrink depending on size of the element. Can sometimes be used to get spacing to look good on a dynamic page.

Dane Parchment
Dane Parchment
Treehouse Moderator 11,075 Points

Thanks for adding that important info, totally forgot about that!

Sahar Nasiri
Sahar Nasiri
7,454 Points

Thank you for adding. It helps a lot :)

Great!