CSS Padding

CSS Padding

CSS Padding


The CSS padding property is used for creating space around the content of an element. CSS provides different properties, with the help of which you can set the padding for an element’s each side (right, left, top and bottom).

The individual sides

With the help of the following properties you can set the padding for each side of an element:

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

As you can guess, for the top, we use padding-top, for bottom padding-bottom, for left side padding-left and right padding-right.

All the padding properties can have the values below:

  • length, which is used for specifying padding in px, pt, cm, etc,
  • %, which specifies padding in % of the width of the containing element,
  • inherit, which specifies that the padding must be inherited from its parent element.

Take into account, that CSS padding property cannot accept negative values. Its default value for all padding properties is 0.

Example of the individual padding properties:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      div {
        background-color: yellow;
        padding-top: 50px;
        padding-right: 30px;
        padding-bottom: 50px;
        padding-left: 80px;
      }
    </style>
  </head>
  <body>
    <h2>Individual padding properties</h2>
    <div>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua..
    </div>
  </body>
</html>

Padding as a shorthand property

The CSS padding property is a shorthand property for the individual padding properties below:

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

In the cases, when the padding property has only 1 value, for example, adding: 35px, all the four paddings are 35px.

Example of the padding shorthand property with one value:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      div {
        background-color: green;
        padding: 35px;
      }
    </style>
  </head>
  <body>
    <h2>Individual padding properties</h2>
    <div>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </div>
  </body>
</html>

The padding property may have 2 values, for example padding: 20px 40px, where top and bottom paddings are 20px, right and left paddings are 40px.

p {
  padding: 20px 40px;
}

This is the same as the code above.

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

Example of the padding shorthand property with two values:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      p {
        background-color: #1c87c9;
        color: white;
        padding: 20px 40px;
      }
    </style>
  </head>
  <body>
    <p>Paragraph with background-color, color and padding properties.</p>
  </body>
</html>

Result

The padding property may have 3 values, for example, padding: 20px 15px 35px;, where top padding is 20px, right and left paddings are 15px and bottom padding is 35px.

p {
  padding: 20px 15px 35px;
}

This is the same as the code above.

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

Example of the padding shorthand property with three values:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      div {
        background-color: lightblue;
        padding: 20px 15px 35px;
      }
    </style>
  </head>
  <body>
    <h2>Example of the padding shorthand property</h2>
    <div>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </div>
  </body>
</html>

And finally, the padding property can have four values, for example padding: 25px 50px 75px 100px;, where top padding is 25px, right padding is 50px, bottom padding is 75px and left padding is 100px.

p {
  padding: 25px 50px 75px 100px;
}

Example of the padding shorthand property with four values:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      div {
        background-color: #95e5f7;
        padding: 25px 50px 75px 100px;
      }
    </style>
  </head>
  <body>
    <h2>Example of the padding shorthand property</h2>
    <div>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </div>
  </body>
</html>
Reactions

Post a Comment

0 Comments

close