CSS padding is the space between the content of an element and its border. It is defined using the padding
property, which can be set for each side of an element (top, right, bottom, left) or for all sides at once.
Here’s an example of setting padding for all sides of an element:
div {
padding: 10px;
}
In this example, the padding of 10px will be applied to all sides of the div
element.
You can also set different padding values for each side using the padding-top
, padding-right
, padding-bottom
, and padding-left
properties:
div {
padding-top: 5px;
padding-right: 10px;
padding-bottom: 15px;
padding-left: 20px;
}
In this example, the top padding is 5px, the right padding is 10px, the bottom padding is 15px, and the left padding is 20px.
You can also use the padding
property to set different values for each side in a shorthand notation. Here are some examples:
div {
padding: 10px 20px; /* top and bottom padding is 10px, left and right padding is 20px */
}
div {
padding: 10px 20px 30px; /* top padding is 10px, left and right padding is 20px, bottom padding is 30px */
}
div {
padding: 10px 20px 30px 40px; /* top padding is 10px, right padding is 20px, bottom padding is 30px, left padding is 40px */
}
You can also use the padding
property with the inherit
, initial
, or unset
values to inherit, set to the initial value, or unset the padding of an element, respectively.
div {
padding: inherit; /* inherits the padding of its parent element */
}
div {
padding: initial; /* sets the padding to its initial value (usually 0) */
}
div {
padding: unset; /* unsets the padding of the element */
}
Padding can be used to create space between the content of an element and its border, and to control the layout and spacing of elements on a web page. It’s important to use padding in a consistent and organized way to create a visually pleasing and functional layout.