Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
The padding shorthand Property in CSS
The padding property in CSS is a shorthand property that allows you to set the inner spacing for all four sides of an element in a single declaration. It controls the space between an element's content and its border.
Syntax
selector {
padding: value;
}
Possible Values
| Number of Values | Description | Order |
|---|---|---|
| 1 value | Applied to all four sides | top, right, bottom, left |
| 2 values | First = top/bottom, Second = left/right | vertical, horizontal |
| 3 values | First = top, Second = left/right, Third = bottom | top, horizontal, bottom |
| 4 values | Clockwise from top | top, right, bottom, left |
Example 1: Four Values
When using four values, they are applied clockwise starting from the top −
<!DOCTYPE html>
<html>
<head>
<style>
.box {
border: 2px solid #3498db;
background-color: #ecf0f1;
padding: 35px 70px 50px 40px;
width: 200px;
margin: 20px;
}
</style>
</head>
<body>
<div class="box">
This box has different padding on each side: 35px top, 70px right, 50px bottom, 40px left.
</div>
</body>
</html>
A box with blue border appears with uneven internal spacing - more padding on the right side, moderate padding on top and left, and medium padding on the bottom.
Example 2: Three Values
With three values, the middle value applies to both left and right sides −
<!DOCTYPE html>
<html>
<head>
<style>
.box {
border: 2px solid #e74c3c;
background-color: #fadbd8;
padding: 20px 40px 30px;
width: 200px;
margin: 20px;
}
</style>
</head>
<body>
<div class="box">
This box has 20px top padding, 40px left and right padding, and 30px bottom padding.
</div>
</body>
</html>
A box with red border and light pink background shows more padding on the left and right sides, with slightly more padding on the bottom than the top.
Example 3: Two Values
With two values, the first applies to top/bottom and the second to left/right −
<!DOCTYPE html>
<html>
<head>
<style>
.box {
border: 2px solid #27ae60;
background-color: #d5f4e6;
padding: 25px 50px;
width: 200px;
margin: 20px;
}
</style>
</head>
<body>
<div class="box">
This box has 25px padding on top and bottom, and 50px padding on left and right.
</div>
</body>
</html>
A box with green border and light green background displays with equal padding on top and bottom, and larger equal padding on left and right sides.
Example 4: Single Value
A single value applies the same padding to all four sides −
<!DOCTYPE html>
<html>
<head>
<style>
.box {
border: 2px solid #9b59b6;
background-color: #ebdef0;
padding: 30px;
width: 200px;
margin: 20px;
}
</style>
</head>
<body>
<div class="box">
This box has uniform 30px padding on all four sides.
</div>
</body>
</html>
A box with purple border and light purple background shows equal padding of 30px on all sides, creating uniform internal spacing.
Conclusion
The padding shorthand property provides a convenient way to set internal spacing for elements. Remember the clockwise order (top, right, bottom, left) when using multiple values, and use fewer values for symmetric designs.
