CSS Margin
CSS Margin
The margin is the space outside the HTML element. It gives external spacing and differentiates two or more elements.
Consider the image:
Here, the space outside the border is the margin.
Syntax:
The margin value can be used in any of the following methods:
Method 1
Here, the margin value will apply an equal margin to all sides (top, right, left, and bottom).
Example:
Note: Values can be specified in different units, such as pixels (px), em, rem, percentage (%), auto, etc.
Method 2
Here, value 1 is the vertical margin (y-axis), and value 2 is the horizontal margin (x-axis).
Example:
Note: You can use the inspect tool to verify.
Method 3
In this method, each value represents the margin of individual sides (top, right, bottom, left).
- top: value1
- right: value2
- bottom: value3
- left: value4
Example:
In this example, the margin on individual sides is
- top: 10px
- right: 20px
- bottom: 25px
- left: 50