Home 〉 CSS Tutorials 〉 CSS Margin Explained with Examples - Master Spacing in Web Design
CSS Margin Explained with Examples - Master Spacing in Web Design !
How can you control the space around HTML elements using CSS?
This tutorial focuses on the `margin` property, showing you how to create space outside elements. Understand how margins differ from padding and how to apply them effectively in your layouts.
Margins in CSS are used to add 'space outside an element', pushing it away from other elements. It's a key part of the 'CSS box model' and is very useful when arranging layout and spacing between sections.
The 'margin' is the transparent space 'outside the border' of an HTML element. It helps control the distance between elements on a webpage.
Syntax ✍
margin: 10px; /* applies to all sides */
margin: 10px 20px; /* vertical | horizontal */
margin: 10px 20px 30px; /* top | horizontal | bottom */
margin: 10px 20px 30px 40px; /* top | right | bottom | left */
<!DOCTYPE html>
<html>
<head>
<style>
.box {
margin: 20px;
background-color: #e0f7fa;
padding: 10px;
border: 1px solid #0097a7;
}
</style>
</head>
<body>
<div class="box">This box has 20px margin on all sides.</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.
Adds equal spacing around the box on all sides.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
margin: 10px 30px;
background-color: #ffe0b2;
padding: 10px;
}
</style>
</head>
<body>
<div class="box">Top/Bottom: 10px, Left/Right: 30px</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.
Sets vertical (top and bottom) and horizontal (left and right) margin separately.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
margin: 5px 10px 15px 20px;
background-color: #fce4ec;
padding: 10px;
}
</style>
</head>
<body>
<div class="box">Top: 5px, Right: 10px, Bottom: 15px, Left: 20px</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.
Sets margin for each side individually using clockwise order.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 200px;
margin: 0 auto;
background-color: #f3e5f5;
text-align: center;
padding: 10px;
}
</style>
</head>
<body>
<div class="box">Centered with auto margin</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.
Using 'margin: 0 auto' centers a block element horizontally.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
margin-top: -10px;
background-color: #f1f8e9;
padding: 10px;
}
</style>
</head>
<body>
<div class="box">This box moves up using negative top margin.</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.
Negative margin pulls the element in the specified direction.
Feature | Margin | Padding |
---|---|---|
Location | Outside the border | Inside the border |
Affects | Space between elements | Space inside the element |
Can be Auto | Yes ('margin: auto') | No |
CSS margin gives you control over the 'spacing between elements'. By learning how to use shorthand and individual properties, you can design clean, well-spaced layouts. Try using 'auto' to center elements and experiment with different values to find what looks best.
Margin is the space outside an element. It creates distance between the element and others around it.
Margin adds space 'outside' the element. Padding adds space 'inside' between the content and border.
It is mostly used to 'center block elements horizontally' within their parent container.
Yes. Negative margins pull elements 'closer' or even 'overlapping' other elements.
It goes in 'clockwise' order: top, right, bottom, left.