Home 〉 CSS Tutorials 〉 CSS Padding Explained with Examples | CSS Box Model Guide
CSS Padding Explained with Examples | CSS Box Model Guide !
What is padding in CSS, and how does it affect element spacing?
Dive into the concept of padding within the CSS box model. This tutorial explains how to add space inside elements, between the content and the border, using the 'padding' property.
Padding is the space inside an element, between the content and its border. It pushes the content inward from the edges of the element.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
padding: 20px;
background-color: #f2f2f2;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div class="box">
<p>This box has 20px padding on all sides.</p>
</div>
</body>
</html>
output 📌
This box has 20px padding on all sides.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
This applies 20px of padding on top, right, bottom, and left.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
padding: 10px 30px;
background-color: #e0f7fa;
border: 1px solid #0097a7;
}
</style>
</head>
<body>
<div class="box">
<p>Padding: 10px top/bottom, 30px left/right.</p>
</div>
</body>
</html>
output 📌
Padding: 10px top/bottom, 30px left/right.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
The above example sets vertical (top & bottom) padding to 10px and horizontal (left & right) to 30px.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
padding: 10px 20px 30px;
background-color: #fff3e0;
border: 1px solid #ff9800;
}
</style>
</head>
<body>
<div class="box">
<p>Top: 10px, Left/Right: 20px, Bottom: 30px</p>
</div>
</body>
</html>
output 📌
Top: 10px, Left/Right: 20px, Bottom: 30px
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
In the above example
<!DOCTYPE html>
<html>
<head>
<style>
.box {
padding: 5px 10px 15px 20px;
background-color: #fce4ec;
border: 1px solid #d81b60;
}
</style>
</head>
<body>
<div class="box">
<p>Top: 5px, Right: 10px, Bottom: 15px, Left: 20px</p>
</div>
</body>
</html>
output 📌
Top: 5px, Right: 10px, Bottom: 15px, Left: 20px
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
This example follows the clockwise rule (top, right, bottom, left).
<!DOCTYPE html>
<html>
<head>
<style>
.container {
width: 400px;
background-color: #ede7f6;
}
.box {
padding: 10%;
background-color: #9575cd;
color: white;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
<p>Padding is 10% of the container's width (400px × 10% = 40px).</p>
</div>
</div>
</body>
</html>
output 📌
Padding is 10% of the container's width (400px × 10% = 40px).
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
The Padding is calculated as a percentage of the parent element's width, not height in the above example.
This box uses separate padding values for each side.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
padding-top: 10px;
padding-right: 15px;
padding-bottom: 20px;
padding-left: 25px;
background-color: #e8f5e9;
border: 1px solid #66bb6a;
}
</style>
</head>
<body>
<div class="box">
</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
This Demonstrates how to give individual padding like padding-top means padding in top side.
<!DOCTYPE html>
<html>
<head>
<style>
* {
box-sizing: border-box;
}
.box {
width: 200px;
padding: 20px;
background-color: #f1f8e9;
border: 2px solid #8bc34a;
}
</style>
</head>
<body>
<div class="box">
<p>Padding is included within the width due to box-sizing: border-box.</p>
</div>
</body>
</html>
output 📌
Padding is included within the width due to box-sizing: border-box.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Theabove example Demonstrates the total element width stays at 200px, including padding and border.
Padding helps add space 'inside elements', improving layout and readability. It's a core part of the CSS box model. Use it with individual or shorthand properties depending on your layout needs. Pair with `box-sizing: border-box` for better layout control.
Padding is the space inside an element, between its content and border. It helps create breathing room around the content.
Padding adds space inside the element, while margin adds space outside the element, between it and other elements.
Yes! You can use separate properties like `padding-top`, `padding-right`, `padding-bottom`, and `padding-left`, or use shorthand.
You can use `padding: top right bottom left;`. For example:
"padding: 10px 20px 15px 5px;"
Yes. When you use `%`, it calculates the padding based on the 'width of the parent element'.
By default, yes. But if you set `box-sizing: border-box`, the padding is included inside the total width/height.