Padding
Padding is the space between an element's contents and border. It is still within the element itself, not around it
Last updated
Padding is the space between an element's contents and border. It is still within the element itself, not around it
Last updated
Unlike many other CSS properties, padding is not inherited from parent elements. You must explicitly set it on the elements where you want it.
Setting padding is as simple as giving the padding property a space value via:
pixels
em/rem
a percentage
HTML
CSS
Setting padding in pixels
Adding padding in pixels is a great way to set padding that never changes. Regardless of the text size the user has set, the padding will always be shown at the same size.
Select your element, declare a padding property, and set it to a numerical value in pixels:
Setting padding in em/rem
You can also set padding as an em or rem value. The padding value will then be relativeto the element's text size. If an h1 has a font-size of 32px, 1em of padding would be 32px. If a p has a font-size of 16px, 1em of padding would be 16px.
Setting padding with a relative unit like em or rem means that if a user has a default font size set in their browser, your design will be relative to that font size. This could allow for more design consistency.
Setting padding via percentages
You can also set padding as a percentage value. The percentage is relative to the widthof the containing element. This is a bit abstract, so stick with me.
Take the following example of a paragraph within the body of your page:
Say that:
the body of the page is 850px wide.
the paragraph inside the body is 300px wide.
If you set padding: 10% on the paragraph element, the paragraph's padding will be 85px (10% of 850px); not 30px (10% of 300px).
Padding percentages are not relative to the width of the element itself; they're relative to the width of its containing element.
To calculate your ideal padding value as a percentage, here's some easy math:
padding as percentage = desired padding in pixels / width of containing element * 100
In the previously example, a body contains the heading and paragraph. Because the body is 500px wide, the h1's padding (5%) will be 25px. The paragraph's padding (3%) will be 15px.
Setting multiple padding values
You'll often want to set one vertical padding value and another horizontal padding value. Let's take the case of buttons; it's a little too intense to have 30px of padding on every side of a button (top, right, bottom, and left):
You can set top, right, bottom, and left padding values in that order:
If the top/bottom values are the same, and the left/right values are the same, you can also just set two values: one for the top/bottom padding and another for left/right padding.
Margin is the CSS property that controls spacing between elements. In this diagram, see how it relates to the other layout elements you've seen so far:
An element has padding around it, which is then surrounded by a border, which then has margins outside of it that determine the space between this element and its neighbor(s).
Margins are similar to padding in that they can be set as:
pixels
em/rem values
percentages that are relative to the width of the containing block.
Margins also can have a value called auto
that is useful for centering content. We'll see it at the end of the chapter.
Let's start with checking out an example using a paragraph and some images:
Here's the HTML and CSS for the example below:
HTML
CSS
Now that you've seen the HTML and CSS for our example, let's go ahead and set some margins!
Adding margins in pixels is a great way to set margins that never change, regardless of the size of the element or the text size involved.
Let''s select the image element, declare the margin
property, and set it to 20 pixels:
By specifying one single value, you set the top, bottom, right, and left margins all to the same amount. Now, 20px of margin has been added to the top, left, bottom, and right of each image element. This means that there's more space between:
the paragraph and the images, which is a result of the image's top margin value
the images themselves, which is a result of the image's left and right margin values
Setting margins in em/rem
You can also set margins using em or rem values. The margin value will then be relative to the element's text size. If an h1 has a font-size of 32px, a 1em margin would be 32px. If a p has a font-size of 16px, a 1em margin would be 16px.
Setting margin with a relative unit like em or rem means that if a user has a default font size set in their browser, your design will be relative to that font size. This could allow for more design consistency.
You can also set margins as percentage values. Like we saw in the last chapter for padding, the percentage is relative to the width of the containing element.
Take the following example of a paragraph within the body of your page:
Let's say that:
the body of the page is 850px wide.
the paragraph inside the body is 300px wide.
If you set margin: 10% on the paragraph element, the paragraph's margin will be 85px (10% of 850px); not 30px (10% of 300px).
Margin percentages are not relative to the width of the element itself; they're relative to the width of its containing element.
margin value as percentage = desired margin in pixels / width of containing element * 100
You'll sometimes want to set top/bottom margins that are different than the left/right margins.
You can therefore set top, right, bottom, and left margin values in that order:
If your top/bottom values are the same, and your left/right values are the same, you can also just set two values: one for the top/bottom margins and another for left/right margins.
Why is there still some space between the paragraph and the images in the example above, even though the margin is 0?
Good question. Default spacing between elements can be tricky to control. That space is the paragraph's default margin that you didn't specify; paragraphs have 16px of margin around them by default.
In order to get rid of that space, you'd have to work on the paragraph's margins or set a negative top margin value for the images (yes, negative values are possible!):
Collapsing margins
Margins exhibit one unusual feature: vertical margins collapse.
Let's say you have the following elements stacked on top of each other:
one element that has a bottom margin of 30px
one element that has a top margin of 20px
The vertical margin between them seems like it would be 50px. After all, 20 + 30 = 50. In reality, the vertical margin between them will be 30px. When there are two different vertical margin values touching each other, only the largest of the two will be kept. This applies to block elements.
HTML
CSS
Remember the acronym TRBL (top, right, bottom, left) if you have a hard time remembering the value order.
To calculate your ideal margin value as a percentage, here's some easy math: