CSS Properties

Padding

The CSS property padding can be used to create space between the content of an element and its border. The value of this property can be specified using any of the length unit specification alternatives we discussed in a previous lesson.

In the next section, we will look at the margin property. This property also deals with spacing, but creates spacing around an element instead of between.

Code example: shorthand

.padded-box {
padding: 1em; /* equivalent */
padding: 1em 1em; /* equivalent */
padding: 1em 1em 1em 1em; /* equivalent */
border: 1px solid black;
}

which produces

.padded-box

Notice how there is a 1em distance between the text and the border on the top, right, bottom and left sides.

Code example: extended

.padded-box {
padding-top: 1em;
padding-right: 1em;
padding-bottom: 1em;
padding-left: 1em;
border: 1px solid black;
}

This .padded-box class and the previous one accomplish the exact same style. Which one we choose is up to us, though most developers tend to prefer the shorthand version.

Read more on MDN

padding

Test yourself

Description

Add 2em padding on the top and bottom sides and 2.5em padding on the left and right sides to the .quote <div> element.

All relevant rules have been defined for you. Add declarations where appropriate. Whether you want use the shorthand or extended notation is up to you.

Tests

  1. 1 Your .quote element should have a top padding of 2em.
  2. 2 Your .quote element should have a bottom padding of 2em.
  3. 3 Your .quote element should have a right padding of 2.5em.
  4. 4 Your .quote element should have a left padding of 2.5em.
Editor
Preview