In CSS, the marginproperty 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.
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.
Example
div {
width: 300px;
margin: 0 auto; /* Vertically no margin, horizontally centered */
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.
We use cookies and similar technologies to enhance your browsing experience, serve personalized content and advertisements, and analyze our traffic.
By clicking "Accept All", you consent to our use of cookies and the processing of your personal data for these purposes.
You can manage your preferences or learn more in our
Privacy Policy and
Cookie Policy.