
- CSS - Home
- CSS - Roadmap
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Types
- CSS - Measurement Units
- CSS - Selectors
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS - Cascading
- CSS - Universal Selectors
- CSS - ID Selectors
- CSS - Group Selectors
- CSS - Class Selectors
- CSS - Child Selectors
- CSS - Element Selectors
- CSS - Descendant Selectors
- CSS - General Sibling Selectors
- CSS - Adjacent Sibling Selectors
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Advanced Features
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS Interview Questions
- CSS Online Quiz
- CSS Online Test
- CSS Mock Test
- CSS - Quick Guide
- CSS - Cheatsheet
- CSS - Properties References
- CSS - Functions 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-block Property
CSS margin-block property sets the vertical margins of an element, which are the margins applied to the block-start and block-end sides of the element. It is a shorthand property for defining the values of properties: margin-block-start and margin-block-end in one single statement. The direction of the margin is affected by writing-mode property.
Syntax
margin-block: auto | length | percentage | initial | inherit;
Property Values
Value | Description |
---|---|
auto | The browser sets the margin for block-start and block-end edges automatically. |
length | The width of the margin-block is set using length units (e.g. px, em, rem etc.). Negative values are allowed. |
percentage | The width of the margin-block is set using percentage values (e.g. 10%) relative to the containing element. |
initial | It sets the property to its default value. |
inherit | It inherits the property from the parent element. |
Examples of CSS Margin Block Property
The following examples explain the margin-block property with different values.
Margin Block Property with Auto Value
To allow the browser to automatically calculate the margin based on the available space, we use the auto value. This is shown in the following example.
Example
<!DOCTYPE html> <html> <head> <style> .container { height: 200px; padding: 20px; } .demo { background-color: lightgreen; width: 50%; padding: 15px; text-align: center; } .auto { margin-block: auto; background-color: lightblue; border-top: 4px solid red; border-bottom: 4px solid red; } </style> </head> <body> <h2> CSS margin-block property </h2> <h4> margin-block: auto </h4> <div class="container"> <div class="demo"> top box </div> <div class="auto demo"> This box has both block-start and block-end edges margin = auto. </div> <div class="demo"> bottom box </div> </div> </body> </html>
Margin Block Property with Length Values
To set the margin at the block-start and block-end edges of an element, we can specify the margin size using length units (e.g. px, em, rem etc.). It accepts upto two values. Depending on the number of values specified, the margin size will be applied to the edges. This is shown in the following example.
Example
<!DOCTYPE html> <html> <head> <style> .container { height: 240px; padding: 20px; } .demo { background-color: lightgreen; width: 50%; padding: 15px; border: 3px dashed red; text-align: center; } .item { background-color: lightblue; border-top: 4px solid red; border-bottom: 4px solid red; } .single-value { margin-block: 16px; } .double-value { margin-block: 30px 60px; } </style> </head> <body> <h2> CSS margin-block property </h2> <h4> margin-block: 16px </h4> <div class="container"> <div class="demo"> top box </div> <div class="single-value demo item"> This box has both block-start and block-end edges margin = 16px </div> <div class="demo"> bottom box </div> </div> <h4> margin-block: 30px 60px </h4> <div class="container"> <div class="demo"> top box </div> <div class="double-value demo item"> This box has block-start margin = 30px and and block-end margin = 60px </div> <div class="demo"> bottom box </div> </div> </body> </html>
Margin Block Property with Percentage Values
To set the margin at the block-start and block-end edges of an element, we can specify the margin size using percentage values(e.g. 10% (of the width of the containing element)). It accepts upto two values. Depending on the number of values specified, the margin size will be applied to the edges. This is shown in the following example.
Example
<!DOCTYPE html> <html> <head> <style> .container { height: 240px; padding: 20px; } .demo { background-color: lightgreen; width: 50%; padding: 15px; border: 3px dashed red; text-align: center; } .item { background-color: lightblue; border-top: 4px solid red; border-bottom: 4px solid red; } .single-value { margin-block: 6%; } .double-value { margin-block: 4% 10%; } </style> </head> <body> <h2> CSS margin-block property </h2> <h4> margin-block: 6% </h4> <div class="container"> <div class="demo"> top box </div> <div class="single-value demo item"> This box has both block-start and block-end edges margin = 6% of the container height. </div> <div class="demo"> bottom box </div> </div> <h4> margin-block: 4% and 10% </h4> <div class="container"> <div class="demo"> top box </div> <div class="double-value demo item"> This box has block-start margin = 4% and and block-end margin = 10% of the container height. </div> <div class="demo"> bottom box </div> </div> </body> </html>
Margin Block Property with Writing Mode
The margin-block property can be used in combination with the writing-mode property which determines the direction of the block. In horizontal mode (like horizontal-lr), block-start is top, block-end is bottom. In vertical-mode (like like vertical-rl), block-start is right and block-end is left. These are shown in the following example.
Example
<!DOCTYPE html> <html> <head> <style> .container { height: 300px; padding: 20px; } .demo { background-color: lightgreen; width: 50%; padding: 15px; border: 3px dashed red; text-align: center; } .item { background-color: lightblue; margin-block: 60px; } .horizontal-value { border-top: 4px solid red; border-bottom: 4px solid red; writing-mode: horizontal-lr; } .vertical-value { border-left: 4px solid red; border-right: 4px solid red; writing-mode: vertical-rl; } </style> </head> <body> <h2> CSS margin-block property </h2> <h4> margin-block: 60px; writing-mode: horizontal-lr; </h4> <div class="container"> <div class="demo"> top box </div> <div class="horizontal-value demo item"> This box has both block-start and block-end edges margin = 60px with writing-mode: horizontal-lr. </div> <div class="demo"> bottom box </div> </div> <h4> margin-block: 60px; writing-mode: vertical-rl; </h4> <div class="container"> <div class="demo"> top box </div> <div class="vertical-value demo item"> This box has both block-start margin = 60px with writing-mode: vertical-rl. </div> <div class="demo"> bottom box </div> </div> </body> </html>
Supported Browsers
Property | ![]() | ![]() | ![]() | ![]() | ![]() |
---|---|---|---|---|---|
margin-block | 87.0 | 87.0 | 66.0 | 14.1 | 73.0 |