Skip to content

CSS Padding

The CSS padding properties are used to create space inside an element, between its content and its border. Padding contributes to the element’s visible size.

Padding clears an area around the content within the element’s boundaries. Any background color or image applied to the element will extend into the padding area.

Think of it like the matting around a picture in a frame: the padding is the matting, the content is the picture, and the border is the frame.

You can control the padding on all four sides individually or use a shorthand property.

Padding can be specified using various units:

Value TypeDescription
lengthDefines a fixed padding using units like px, em, rem. Example: padding: 10px;
%Defines padding as a percentage relative to the width of the containing block. Example: padding: 5%;

Note: Percentage padding (even padding-top and padding-bottom) is calculated based on the width of the containing block, which can sometimes be counter-intuitive.

You can set the padding for each side separately:

  • padding-top
  • padding-right
  • padding-bottom
  • padding-left

Example:

.box {
padding-top: 10px;
padding-right: 20px;
padding-bottom: 10px;
padding-left: 20px;
border: 1px solid black;
background-color: lightyellow;
}

The padding property is a shorthand that allows you to set padding for all sides in one declaration. It can accept one, two, three, or four values:

  • One value: padding: 10px; (Applies 10px padding to all four sides)
  • Two values: padding: 10px 20px; (First value applies to top and bottom, second value applies to left and right)
  • Three values: padding: 10px 20px 5px; (First value for top, second for left and right, third for bottom)
  • Four values: padding: 10px 20px 5px 15px; (Values applied in clockwise order: top, right, bottom, left)

Example using two values:

.button {
/* 5px padding top/bottom, 15px padding left/right */
padding: 5px 15px;
border: 1px solid gray;
background-color: lightblue;
display: inline-block; /* To make padding visible */
}

Example using four values:

.content-area {
/* 10px top, 20px right, 30px bottom, 40px left */
padding: 10px 20px 30px 40px;
border: 1px solid green;
}

Remember that padding increases the total space occupied by an element. How this affects the calculated width and height depends on the box-sizing property.

  • box-sizing: content-box (default): Padding is added outside the specified width and height.
  • box-sizing: border-box: Padding is included within the specified width and height (content area shrinks).

Using box-sizing: border-box; is often recommended for more predictable layouts when using padding.

PropertyDescription
paddingShorthand to set all four padding properties.
padding-topSets the top padding.
padding-rightSets the right padding.
padding-bottomSets the bottom padding.
padding-leftSets the left padding.