CSS Margin and Padding Get Certified
What are CSS Margin and Padding?
CSS Margin and Padding are used to control the spacing around and inside HTML elements. Margin creates space outside an element, while padding creates space inside an element between its content and its border.
Why Use Margin and Padding?
Proper spacing improves readability, creates clean layouts, and makes websites look professional and easy to read.
Basic Margin Syntax
What this code does: Applies 30px of margin around a blue box.
<!DOCTYPE html>
<html>
<head>
<style>
.demo-box{
margin:30px;
background:#007BFF;
color:white;
padding:20px;
}
</style>
</head>
<body>
<div class="demo-box">
Margin Example
</div>
</body>
</html>
Step 1: Using Margin
The margin property creates empty space outside an HTML element. It pushes the element away from other elements around it.
What this code does: Adds 30px of space between two colored boxes using margin-top.
<!DOCTYPE html>
<html>
<head>
<style>
.demo-box1{
background:#28a745;
color:white;
padding:20px;
}
.demo-box2{
background:#007BFF;
color:white;
padding:20px;
margin-top:30px;
}
</style>
</head>
<body>
<div class="demo-box1">
First Box
</div>
<div class="demo-box2">
Second Box with Margin
</div>
</body>
</html>
Types of Margin Values
CSS allows you to set margin individually for each side using top, right, bottom, and left.
What this code does: Applies different margin values on each side of an orange box.
<!DOCTYPE html>
<html>
<head>
<style>
.demo-box{
margin-top:20px;
margin-right:40px;
margin-bottom:20px;
margin-left:40px;
background:#ff9800;
padding:20px;
color:white;
}
</style>
</head>
<body>
<div class="demo-box">
Different Margin Sides
</div>
</body>
</html>
Basic Padding Syntax
What this code does: Adds 30px of padding inside a purple box.
<!DOCTYPE html>
<html>
<head>
<style>
.demo-box{
padding:30px;
background:#6f42c1;
color:white;
}
</style>
</head>
<body>
<div class="demo-box">
Padding Example
</div>
</body>
</html>
Step 2: Using Padding
The padding property creates space inside an element between its content and border. It makes text and other content more comfortable to read.
What this code does: Creates a card with a blue border and 25px of internal padding.
<!DOCTYPE html>
<html>
<head>
<style>
.demo-card{
width:250px;
border:2px solid #007BFF;
padding:25px;
}
</style>
</head>
<body>
<div class="demo-card">
Padding creates space inside this box.
</div>
</body>
</html>
Step 3: Margin and Padding Together
Margin and Padding are often used together. Padding controls the inner spacing, while margin controls the outer spacing between elements.
What this code does: Combines outer margin and inner padding on a light gray card.
<!DOCTYPE html>
<html>
<head>
<style>
.demo-card{
width:300px;
margin:40px;
padding:25px;
background:#f1f1f1;
border:2px solid #333;
}
</style>
</head>
<body>
<div class="demo-card">
<h2>CSS Box</h2>
<p>
Margin is outside and padding is inside.
</p>
</div>
</body>
</html>
Complete CSS Margin and Padding Example
What this code does: Builds a clean card layout with outer container margin, inner card padding, and a properly spaced button.
<!DOCTYPE html>
<html>
<head>
<style>
body{
font-family:Arial;
background:#f5f9ff;
}
.demo-container{
margin:50px;
}
.demo-card{
background:white;
padding:25px;
margin-bottom:20px;
border-radius:10px;
border:1px solid #ddd;
}
.demo-btn{
padding:12px 25px;
margin-top:15px;
background:#007BFF;
color:white;
border:none;
border-radius:5px;
}
</style>
</head>
<body>
<div class="demo-container">
<div class="demo-card">
<h2>Website Card</h2>
<p>
CSS spacing improves website design.
</p>
<button class="demo-btn">
Learn More
</button>
</div>
</div>
</body>
</html>
Advantages of CSS Margin and Padding
Common Mistakes
Beginners often confuse margin and padding, forget CSS units, use too much spacing, or create layouts that break on smaller screens.
What this code does: Shows the common mistake of forgetting units, and the correct version with px.
<!DOCTYPE html>
<html>
<head>
<style>
/* Incorrect */
.demo-box{
padding:20;
margin:30;
}
/* Correct */
.demo-box{
padding:20px;
margin:30px;
}
</style>
</head>
<body>
<div class="demo-box">Spacing Example</div>
</body>
</html>
Best Practices
- Use margin for spacing between elements.
- Use padding for spacing inside elements.
- Always include CSS units like px, %, rem, or em.
- Avoid excessive spacing values.
- Use consistent spacing throughout your website.
- Use shorthand properties when possible.
- Test layouts on different screen sizes.
- Use responsive units for modern websites.
Web Resources on CSS Margin and Padding
1. MDN Web Docs : Logical properties for margins, borders, and padding
2. MDN Web Docs : margin CSS property
3. Mail Chimp.com : What Is the Difference Between Padding vs. Margin?
4. Dev.to : Don’t Interchange Margin and Padding.
Click “Run Code” to see the result here.
CSS Syntax Example
The CSS syntax consists of a selector and a declaration block.
h1 { color: blue; font-size: 36px;}Selector: h1 – targets all <h1> elements
Property: color and font-size
Value: blue and 36px
Your CSS Applied Below:
This is an H1 heading with your CSS
This is an H2 heading (not affected)
This paragraph is not affected by the h1 selector.
Welcome to CSS
This page is styled using CSS.
The heading and paragraph use your CSS rules.
- List item with your styles
- Another list item
🧪 Test Your CSS Code
Edit the CSS code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.