CSS Basic Tutorial
CSS Flexbox
In CSS, the margin
property controls the amount of space outside an element's border. It determines the space between the element and other elements on the page. Margins are transparent and can be set for all sides of an element or individually.
margin: <value>;
Value: Can be a length (e.g., px
, em
, %
), auto, or inherit.
You can specify margins for each side of the element using these properties:
margin-top
margin-right
margin-bottom
margin-left
Try it yourself
You can also use shorthand to specify margins for different sides in one declaration:
margin: <top> <right> <bottom> <left>;
One powerful feature is margin: auto;
, which can be used to center block-level elements (like div
) horizontally within their container when they have a set width.
Try it yourself
In CSS, vertical margins (top and bottom) between block elements may collapse, meaning the larger of the two margins is used instead of adding them together.