In CSS, the margin
property is used to create space around elements, outside of any defined borders.
The margin properties can be used to set the size of the white space around an element.
margin
The margin
shorthand property sets all four margins (top, right, bottom, and left) in one declaration.
Syntax:
margin: <value> [<value>] [<value>] [<value>];
Example:
<style> div { margin: 10px 20px 30px 40px; /* top, right, bottom, left */ } </style> <div> <p>This page has a light blue background and a dark red heading.</p> </div>
margin-top
The margin-top
property sets the top margin of an element.
Syntax:
margin-top: <value>;
Example:
<style> div { margin-top: 10px; } </style> <div> <p>This page has a light blue background and a dark red heading.</p> </div>
margin-right
The margin-right
property sets the right margin of an element.
Syntax:
margin-right: <value>;
Example:
<style> div { margin-right: 20px; } </style> <div> <p>This page has a light blue background and a dark red heading.</p> </div>
margin-bottom
The margin-bottom
property sets the bottom margin of an element.
Syntax:
margin-bottom: <value>;
Example:
<style> div { margin-bottom: 30px; } </style> <div> <p>This page has a light blue background and a dark red heading.</p> </div>
margin-left
The margin-left
property sets the left margin of an element.
Syntax:
margin-left: <value>;
Example:
<style> div { margin-left: 40px; } </style> <div> <p>This page has a light blue background and a dark red heading.</p> </div>
margin
(Shorthand with Fewer Values)
The margin
shorthand can also accept fewer than four values, in which case the values are applied as follows:
- One value: All four margins.
- Two values: Vertical (top and bottom) and horizontal (right and left).
- Three values: Top, horizontal (right and left), and bottom.
Example:
div { margin: 10px; /* All sides 10px */ margin: 10px 20px; /* Vertical 10px, Horizontal 20px */ margin: 10px 20px 30px; /* Top 10px, Horizontal 20px, Bottom 30px */ }
auto
Value
The auto
value can be used to horizontally center an element with a defined width within its container.
Example:
<style> div { width: 200px; margin: 0 auto; /* Center the element horizontally */ } </style> <div> <p>This page has a light blue background and a dark red heading.</p> </div>
Examples in Action
Here’s an example of how these properties can be used together:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CSS Margin Example</title> <style> .box { width: 200px; height: 100px; background-color: lightblue; margin: 10px 20px 30px 40px; /* top, right, bottom, left */ } .centered-box { width: 200px; height: 100px; background-color: lightgreen; margin: 20px auto; /* Vertical 20px, Horizontal auto (centered) */ } </style> </head> <body> <div class="box">Box with margins</div> <div class="centered-box">Centered box</div> </body> </html>
In this example:
- The
.box
class applies different margin values to each side of the box. - The
.centered-box
class centers the box horizontally within its container.
These properties give you control over the spacing around elements, which is crucial for creating visually appealing and well-structured web layouts.