
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS Margin Properties
The CSS margin property is a shorthand for margin-top, margin-right, margin-bottom, and margin-left. It enables us to specify the space around elements. We can also set margins for individual sides.
Syntax
The syntax of CSS margin property is as follows −
Selector { margin: /*value*/ }
Example
The following examples illustrate CSS margin property −
<!DOCTYPE html> <html> <head> <style> div { margin: 10px 50px 20px; padding: 1rem; border: dashed; } p { text-align: center; } #demo { margin-left: -80px; box-shadow: inset 0 0 10px brown; } </style> </head> <body> <div></div> <p>This is demo text.</p> <p id="demo">Another demo text.</p> <p>This is new demo text.</p> </body> </html>
Output
This gives the following output −
Example
<!DOCTYPE html> <html> <head> <style> ul { margin: auto; background-image: linear-gradient(to right, khaki, aliceblue, skyblue, rebeccapurple); width: 20em; } li { margin-top: 3px; } li + li { margin-left: -20px; } li:last-child { margin-left: 75px; } </style> </head> <body> <h2>Demo Heading</h2> <ul> <li>This is demo list item.</li> <li>This is another list item with different style.</li> <li>This is our third demo text with another style. </li> </ul> </body> </html>
Outline
This gives the following output −
- Related Articles
- How to set the CSS margin properties in one declaration?
- Animate CSS margin property
- Animate CSS margin-bottom property
- Animate CSS margin-left property
- Perform Animation on CSS margin-top
- Usage of margin property with CSS
- The margin Shorthand Property in CSS
- Usage of margin-top property with CSS
- Usage of margin-bottom property with CSS
- Perform Animation on CSS margin-right property
- Usage of margin-right property with CSS
- Setting Margin Space around elements using CSS
- CSS Border Properties
- CSS Background Properties
- CSS Dimension Properties

Advertisements