Advertisement
Google Ad Slot: content-top
CSS Margin
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.
Syntax:
margin: <value>;
Value: Can be a length (e.g., px, em, %), auto, or inherit.
Individual Margins:
You can specify margins for each side of the element using these properties:
margin-topmargin-rightmargin-bottommargin-left
Shorthand for Margins:
You can also use shorthand to specify margins for different sides in one declaration:
Syntax:
margin: <top> <right> <bottom> <left>;
Auto Margins (Centering Elements):
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.
Margin Collapsing:
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.