CSS margins control the space outside an element, creating spacing between elements on a webpage. Margins do not affect an element’s size but help position elements properly.
Property | Description |
---|---|
margin |
Shorthand for setting all four margins (top, right, bottom, left). |
margin-top |
Sets the top margin of an element. |
margin-right |
Sets the right margin of an element. |
margin-bottom |
Sets the bottom margin of an element. |
margin-left |
Sets the left margin of an element. |
margin: auto; |
Centers an element horizontally (if width is defined). |
margin
)The margin
property defines space around an element in one line.
div {
margin: 20px; /* Adds 20px space on all sides */
}
💡 This is the same as: