CSS Padding Property Explained with code examples

The CSS padding property sets the space between an element’s content and its border. It can be used to add whitespace, or “breathing room,” around an element. Padding can be applied to any element that has a visible border, including block-level and inline-level elements.

The padding property can be set using a shorthand notation or individual values for each side of the element.

  1. Shorthand notation:

The shorthand notation for the padding property is:

padding: top right bottom left;

Where top, right, bottom, and left are values in pixels, percentages, ems, or other CSS units.

For example:

padding: 20px; // Applies 20px padding to all four sides
padding: 10px 20px; // Applies 10px padding to top and bottom, and 20px padding to left and right
padding: 10px 20px 30px; // Applies 10px padding to top, 20px padding to left and right, and 30px padding to bottom
padding: 10px 20px 30px 40px; // Applies 10px padding to top, 20px padding to right, 30px padding to bottom, and 40px padding to left
  1. Individual values:

The padding property can also be set using individual values for each side of the element:

padding-top: value;
padding-right: value;
padding-bottom: value;
padding-left: value;

Where value is a pixel, percentage, ems, or other CSS unit.

For example:

padding-top: 10px;
padding-right: 20px;
padding-bottom: 30px;
padding-left: 40px;

This will apply 10px padding to the top, 20px padding to the right, 30px padding to the bottom, and 40px padding to the left of the element.

In conclusion, the padding property in CSS is used to add space between an element’s content and its border. It can be set using shorthand notation or individual values for each side of the element. By using the padding property, you can improve the readability and overall look of your web pages.