CSS Padding


Padding is like adding space around what's inside a box, within the lines that outline the box.

This element has a padding of 70px.

The CSS padding properties create room around the content of an element, within its set borders.


Padding - Individual Sides

CSS provides ways to define how much space should be added around each side of an element.

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

You can use different values for padding properties:

  • length - specifies a padding in px, pt, cm, etc.
  • % - specifies a padding in % of the width of the containing element
  • inherit - specifies that the padding should be inherited from the parent element

Padding - Shorthand Property

You can make the code shorter by using just one property to set all the padding properties.

The padding property is a short way to set padding for different sides all at once:

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

How it works:

When you use the padding property with four values:

  • padding: 25px 50px 75px 100px;
    • top padding is 25px
    • right padding is 50px
    • bottom padding is 75px
    • left padding is 100px

If you use the padding property with three values:

  • padding: 25px 50px 75px;
    • top padding is 25px
    • right and left paddings are 50px
    • bottom padding is 75px

If you use the padding property with three values:

  • padding: 25px 50px;
    • top and bottom paddings are 25px
    • right and left paddings are 50px

If the padding property has only one value:

  • padding: 25px;
    • all four paddings are 25px

Padding and Element Width

The CSS width property tells us how wide the stuff inside an element is. This stuff includes everything inside the element except for the padding, border, and margin.

If you set a width for something, like a box, and then add some extra space inside it (we call it padding), that extra space will actually make the whole thing wider. This can sometimes be a problem.

To ensure that the width stays fixed at 300 pixels, regardless of how much padding is added, you can utilize the box-sizing property. This property ensures that the element retains its original width. So, if you add more padding, the space available for content inside the element will shrink accordingly.