CSS Padding

The padding property allows you to specify how much space should appear between the content of an element and its border . The CSS padding properties are used to generate space around content. The padding clears an area around the content (inside the border) of an element.

With CSS, you have full control over the padding. There are CSS properties for setting the padding for each side of an element (top, right, bottom, and left).

Padding Syntax

padding: length|initial|inherit;

Padding - Individual Sides

CSS has properties for specifying the padding for each side of an element:

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

All the padding properties can have the following values:

  • length - specifies a padding in px, pt, cm, etc.
  • % - specifies a padding in % of the width of the containing element
  • initial - sets this property to its default value
  • inherit - specifies that the padding should be inherited from the parent element

Example

p {
    padding-top: 20px;
    padding-right: 10px;
    padding-bottom: 20px;
    padding-left: 10px;
}

Note: Unlike margin, Padding cannot have negative value. Lowest minimum value of padding is 0 (in all measuring units)

Padding - Shorthand Property

To shorten the code, it is possible to specify all the padding properties in one property.

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

Syntax

p {
    padding: padding-top padding-right padding-bottom padding-left;
}

Example : 

p {
    padding: 20px 10px 20px 10px;
}

If the padding property has four values:

  • padding: 25px 50px 75px 100px;

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

If the padding property has three values:

  • padding: 25px 50px 75px;

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

If the padding property has two values:

  • padding: 25px 50px;

    • top and bottom paddings are 25px
    • right and left paddings are 50px

If the padding property has one value:

  • padding: 25px;

    • all four paddings are 25px

The inherit Value

Below example lets the left margin be inherited from the parent element:

div.container {
    border: 1px solid #000;
    padding-left: 100px;
}

p.left {
    padding-left: inherit;
}

Loading ...

Related Results :

  1. CSS Padding
Note :
  • Related Posts are generally User Blog posts.
  • or Other tutorials from other networks of w3clan.com.
  • Any registered user can create related posts based on search term tags.

About the Author