Course Content
CSS Basic
0/1
CSS Selectors
0/1
CSS Comments
0/1
CSS Backgrounds
0/1
CSS Borders
0/1
CSS Outline
0/1
CSS Fonts
0/1
CSS Height and Width
0/1
CSS Margins and Paddings
0/2
CSS Icons
0/1
CSS Links
0/1
CSS Lists
0/1
CSS Tables
0/1
CSS Display Properties
0/1
CSS Max-Width Property
0/1
CSS Positioning Elements
0/1
CSS Z-Index Property
0/1
CSS Overflow
0/1
CSS Float
0/1
CSS Opacity
0/1
CSS Forms
0/1
CSS Dropdowns
0/1
CSS Buttons
0/1
CSS Media Queries
0/1
About Lesson

Definition:

In CSS, the padding property is used to set the padding area for all four sides of an element. Padding is the space between the content of an element and its border.

Syntax:

HTML
selector {
padding: top right bottom left;
}
  • top, right, bottom, and left are optional values that represent the padding on each side of the element. They can be specified in various units, such as pixels (px), em, rem, percentages, etc.

If you provide one value, it sets the padding for all sides. If you provide two values, the first value sets the top and bottom padding, and the second value sets the right and left padding. If you provide three values, the first value sets the top padding, the second sets the right and left padding, and the third sets the bottom padding. If you provide four values, they set the top, right, bottom, and left padding individually.

Example:

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    /* Set padding for a div with the class 'box' */
    .box {
      width: 200px;
      background-color: lightblue;
      border: 2px solid navy;
      /* Padding shorthand property: padding: top right bottom left; */
      padding: 20px 10px 30px 15px;
      margin: 20px;
    }
 

    /* Set padding for a paragraph inside the div */
    .box p {
      padding: 10px;
    }
  </style>
  <title>Padding Example</title>
</head>
<body>
 

  <div class="box">
    <p>This is a box with specified padding.</p>
  </div>
 

</body>
</html>

In this example:

  • The first CSS rule sets the padding for the div element with the class box. The shorthand padding property is used to set different padding values for each side: 20px for the top, 10px for the right, 30px for the bottom, and 15px for the left.
  • The second CSS rule sets additional padding for the paragraphs (p) inside the div. This padding is 10px on all sides.

You can modify the values to adjust the padding according to your design requirements. The use of padding helps create space and improves the overall aesthetics of your webpage.

Output: