CSS padding is used to create space between the content of an element and its border. Unlike margins, which create space around an element and push other elements away, padding creates space inside an element, preserving the element's overall size and shape. Padding helps control the spacing between the content and the border of an element.

The padding property is used to define the padding for an element, and it can take one, two, three, or four values, each representing the padding on different sides of the element.

Here's the syntax of the padding property:

selector {
  padding: top right bottom left;
}

 

Now, let's break down each part of the padding property and provide examples for each:

1) Padding on All Sides: When you specify a single value for padding, it sets the padding on all four sides of the element. The value can be in pixels (px), em units, percentages (%), or other valid CSS units.

/* Example using padding on all sides */
.box {
  padding: 10px;
}

 

Above example sets a padding of 10 pixels on all sides of elements with the class "box."

2) Padding on Vertical and Horizontal Sides: When you specify two values for padding, the first value sets the top and bottom padding, and the second value sets the left and right padding.

/* Example using padding on vertical and horizontal sides */
.content {
  padding: 10px 20px;
}

 

Above examlpe sets a padding of 10 pixels for the top and bottom and 20 pixels for the left and right of elements with the class "content."

3) Padding on Individual Sides: When you specify four values for padding, they apply to the top, right, bottom, and left padding individually, in the order top-right-bottom-left.

/* Example using padding on individual sides */
.section {
  padding: 10px 20px 15px 30px;
}

 

Above example sets different paddings on individual sides for elements with the class "section."

We can use padding to create space between an element's content and its border. This can be especially useful for adding breathing room around text, images, or other content within containers. Padding helps improve the readability and visual appeal of your web pages.

Here's an example that demonstrates using padding on HTML elements:

<!DOCTYPE html>
<html>
<head>
  <title>Padding Example</title>
  <style>
    /* CSS styles */
    .box {
      width: 200px;
      height: 100px;
      background-color: lightblue;
      padding: 20px;
    }

    .content {
      width: 300px;
      padding: 10px 20px;
      background-color: lightgray;
    }

    .section {
      width: 250px;
      padding: 10px 20px 15px 30px;
      background-color: lightgreen;
    }
  </style>
</head>
<body>
  <!-- HTML content -->
  <div class="box">
    <p>This is a box with padding.</p>
  </div>

  <div class="content">
    <p>This is some content with padding.</p>
  </div>

  <div class="section">
    <p>This is a section with individual padding values.</p>
  </div>
</body>
</html>

In this example, we have three div elements with different classes: "box," "content," and "section." The CSS styles apply padding to each of these elements, creating space around their content. The result is three boxes with varying amounts of padding inside them.

Latest Tutorial

Newly published tutorials

Laravel 10 - Send mail using AWS SES

Learn to send Email using Laravel 10 with AWS SES service.

Laravel - How to upload file to AWS S3 Bucket

Explore how we can upload file using the laravel on Amazon S3 bucket.

Laravel 10 - Tutorial to create CRUD Application

Learn to create crud application using laravel 10. Step by step guidance will help to understand the concept of crud.