CSS padding property
The padding
Property in CSS
The padding
property in CSS is used to create space between an element's content and its border. It is essential for controlling the internal spacing within an element, ensuring that the content does not touch the border and enhancing the readability and aesthetics of web content.
Values of padding
Fixed Values (e.g.,
px
,em
,rem
,%
)- Pixels (
px
): Defines padding in pixels, providing a fixed and absolute measurement..fixed-padding { padding: 20px; }
- Em Units (
em
): Defines padding relative to the font size of the element or its parent..em-padding { padding: 2em; }
- Root Em Units (
rem
): Defines padding relative to the root element’s font size..rem-padding { padding: 1.5rem; }
- Percentage (%): Defines padding as a percentage of the width of the containing block.
.percent-padding { padding: 5%; }
- Pixels (
Individual Sides
padding-top
: Sets the padding at the top of an element..padding-top { padding-top: 10px; }
padding-right
: Sets the padding to the right of an element..padding-right { padding-right: 15px; }
padding-bottom
: Sets the padding at the bottom of an element..padding-bottom { padding-bottom: 10px; }
padding-left
: Sets the padding to the left of an element..padding-left { padding-left: 15px; }
Shorthand
- The
padding
property can be used as a shorthand to set the padding for all four sides at once. It accepts one to four values:- One Value: Applies the same padding to all four sides.
.all-sides { padding: 20px; }
- Two Values: The first value applies to the top and bottom, and the second value applies to the left and right.
.vertical-horizontal { padding: 10px 15px; }
- Three Values: The first value applies to the top, the second value applies to the left and right, and the third value applies to the bottom.
.top-horizontal-bottom { padding: 10px 15px 20px; }
- Four Values: Each value applies to a specific side in the order: top, right, bottom, left.
.all-sides-different { padding: 10px 15px 20px 25px; }
- One Value: Applies the same padding to all four sides.
- The
Examples
<div class="container">
<div class="box fixed-padding">Fixed Padding</div>
<div class="box em-padding">Em Padding</div>
<div class="box rem-padding">Rem Padding</div>
<div class="box percent-padding">Percentage Padding</div>
</div>
.container {
border: 1px solid #ccc;
padding: 20px;
}
.box {
background-color: lightblue;
border: 1px solid darkblue;
}
.fixed-padding {
padding: 20px;
}
.em-padding {
padding: 2em;
}
.rem-padding {
padding: 1.5rem;
}
.percent-padding {
padding: 5%;
}
Explanation
- Fixed Padding: The
.fixed-padding
element has a padding of 20px on all sides, creating consistent internal spacing between the content and the border. - Em Padding: The
.em-padding
element has a padding of 2em, which adjusts based on the font size of the element or its parent. - Rem Padding: The
.rem-padding
element has a padding of 1.5rem, which is relative to the root font size. - Percentage Padding: The
.percent-padding
element has a padding of 5% of the container's width, adjusting dynamically based on the container's size.
Use Cases
- Content Spacing: Use padding to ensure that the content inside an element does not touch its borders, improving readability and aesthetics.
- Responsive Design: Apply padding using relative units like percentages,
em
, orrem
to make spacing adapt to different screen sizes and font sizes. - Element Styling: Adjust padding to create consistent spacing within buttons, form fields, and other interactive elements.