Margin - Property CSS

margin
margin-block-start
margin-block-end
margin-inline-start
margin-inline-end

Summary of characteristics of the margin property

Quick description
Shorthand of the four margins.
Status
Standard
Usable for
HTML
Predefined values
auto
Percentages
Calculated relative to the width of the parent block.
Initial value
0
Inherited by default
No.
Animation type
Computed value : during an animation, the margin property gradually changes from one value to another.
W3C Module
CSS Box Model Module
References (W3C)
 🡇  
 🡅  
Document status: WD (Working Draft)

Document status: WD (Working Draft)

Document status: CR (Candidate Recommendation)

Document status: REC (Recommendation)

Document status: DEPR (Old specification, declared obsolete or superseded)

Syntax diagram of margin..

Margin property - Syntax diagramSyntax diagram of the margin CSS property. See stylescss.free.fr for details. auto auto length / % length / % {1,4} {1,4}margin:;margin:;
Syntax diagram of the margin property.
The links in the diagram provide access to more details. .

Description of the terms used in the diagram:

Description of the margin property.

margin defines the size of the margins, i.e. the placeholder around the element, outside its border. Not to be confused with padding that is handled by the padding property.

margin is a shorthand property that allows you to define the values of the four margins, i.e. the properties:

  • margin-top: top margin.
  • margin-right: right margin.
  • margin-bottom: bottom margin.
  • margin-left: left margin.

margin accepts from 1 to 4 values, but in all cases, all four margins are changed: even if only one value is specified, it will be applied to all four margins (see syntax details below).

Margins are counted from the parent element if the element is a single child (no other elements in the this parent), or from the previous and next elements when there are other elements in the parent.

CSS margin   CSS maregin

Margin merge.

The top and bottom margins between successive blocks are not added: the browser applies the margin that is the largest. This merge mechanism only occurs for margins in the vertical direction: if blocks are displayed side-by-side, their left and right margins do not merge.

No margin merge
If the margins were added
Margin merge
With the merging of margins

See also the margin-trim property for more information on managing margins between elements of the same type.

Centering of an element of the block type.

When the right and left margins are set to auto, they cause the element to center within the width of its parent.

Language support.

If text in a non-Latin language is envisaged (Arabic or CJK for example), it is better reason with the so-called "logical" properties, i.e. those that take into account the writing mode. For example, Arabic languages are written from right to left, which is the opposite of Latin (European) languages which are written from left to right. The logical properties for margins are as follows:

  • margin-block-start : margin at the beginning of the block, equivalent to margin-top for European languages.
  • margin-block-end: margin at the end of the block, equivalent to margin-bottom for European languages.
  • margin-inline-start: margin at the beginning of lines, equivalent to margin-left for European languages.
  • margin-inline-end: margin at the end of lines, equivalent to margin-right for European languages.

Two short hand properties must be added:

  • margin-block: accepts two values that correspond to margin-block-start and margin-block-end.
  • margin-inline: accepts two values that correspond to margin-inline-start and margin-inline-end.

Note: The short hand property margin only handles physical margins, so its effect will be independent of the writing mode.

The simulator below gives the equivalences for the main language families.

   

margin-top
margin-left
The direction and writing mode of this text adapt to the chosen language.
margin-right
margin-bottom

Values for margin.

  • margin: auto;

    Margins are determined by the browser. They can be different depending on the type of element (p, div...).
    Particularity: when the right and left margins are both set at the auto value, they result in the horizontal centering of the element. This feature is not valid in the vertical direction.

  • margin: 15px 10px 10px 0px; a b c d

    From one to four numbers that define the size of the margins.

    These are positive or negative numbers, followed by a unit of dimension (see CSS dimension units) and separated by spaces.
    Note: Negative values can shift the clip out of the visible area.

    Percentages are calculated relative to the width of the parent, which makes sense for margins horizontal (margin-left and margin-right), but may be surprising in the case of vertical margins (margin-top and margin-bottom).
    Calculating the vertical margins relative to the height of the parent element would lead to a circular calculation insoluble. Indeed, the height of a block is determined by its content (unless it is fixed with the height property). However, the height of the content depends on the margins of the internal elements. If these also depended on the height of the parent, we would have a big problem of circular calculation.

    In fact, the use of percentages for vertical margins is generally of little interest.

    Here's how the values are assigned based on the number of values shown:

    1. If only one value is specified, it applies to all four margins.
    2. If two values are specified,
          - the first defines the upper and lower margins,
          - The second value defines the left and right margins.
    3. When three values are specified, they define
          - the top margin,
          - the left and right margins,
          - the bottom margin.
    4. Finally, if the four values are specified, they define each of the margins, in the following order:
          - top margin,
          - right margin,
          - bottom margin,
          - left margin.
    Margin property with a single value
    margin:a;
    A single value.
    Margin property with two values
    margin:a b;
    Two values.
    Margin property with three values
    margin:a b c;
    Three values.
    Margin property with four values
    margin:a b c d;
    Four values.

Common values:

margin: initial (0) margin: inherit margin: revert margin: revertLayer margin: unset

These values ​​are described in more detail on their respective page: initial, inherit, revert, revert-layer, unset.

Animation of the margin property.

Margin animation is often used to give the impression of a block moving across the screen.

Demonstration

Manipulating the margin property programmatically.

The codes given below are given for margin-top but will be easily modified to fit the other properties relating to margins.

With Javascript, change the value of margin.

Javascript supports two syntaxes to change the value of margin-top. The first syntax uses the typical CSS notation (the kebab-case: a hyphen to separate words), while the second syntax is closer to the usual Javascript syntax, with camel-case notation (a capital letter to separate words).

Javascript
let el = document.getElementById('id'); el.style['margin'] = '2cm'; // or let el = document.getElementById('id'); el.style.margin = '2cm';

With Javascript, read the value of margin.

Rereading the value of margin-top in Javascript can also be done according to two syntaxes. The property must have been assigned directly to the element itself by its style attribute, and not to a class or other CSS selector.

Javascript
let el = document.getElementById('id'); let value = el.style['margin']; // or let el = document.getElementById('id'); let value = el.style.margin;

With Javascript, read the calculated value of margin.

To read the computed value of margin-top, use the following code. The margin is returned in pixels or in percentages, whatever the unit that was used to define it in the style sheet.

Javascript
let el = document.getElementById('id'); let value = window.getComputedStyle(el).getPropertyValue('margin');

With JQuery, change the value of margin.

With JQuery, two syntaxes are also possible to modify the value of the margin-top property of the el element:

JQuery

$('#id').css('margin', '2cm');

With JQuery, read the calculated value of margin.

As in Javascript, the margin is returned in pixels or percentages.

JQuery
let value = $('#id').css('margin');

Test it yourself.

The buttons below display either the value as applied for the first button or the computed value for the second button. This second option allows you to see how the value of margin is stored (serialized). In particular, we note that the percentages are well remembered as percentages (see the chapter on inheritance). All other units are converted to pixels.

Simulator.

The simulator below will help you familiarize yourself with the notion of the beginning of a block, the beginning of a line, etc. depending on language. Margins are shown in blue.


Writing mode:

margin-block-start :
margin-block-end :
margin-inline-start :
margin-inline-end :

Here's how margins are talked about in traditional typography textbooks.

"The margins are an aesthetic foundation of the layout, which anchors the place of the text by encircling it with a silent space. They bring out the text to give it all its thickness and install a tranquility conducive to reading. The margins also have an ergonomic role; large backgrounds allow thumbs to rest, small backgrounds avoid text to get lost in the binding."

Browsers compatibility with margin.

margin is probably one of the oldest properties of CSS: it is recognized by all browsers. The logical properties (taking into account the way the language is written) are more recent but are now well recognized as well.

Column 1
Support for margin, margin-top, margin-right, margin-bottom and margin-left physical properties.
Column 2
Support for the logical properties margin-inline, margin-inline-start and margin-inline-end that define the margins in relation to the language writing mode.
Column 3
Support for the logical properties margin-block, margin-block-start and margin-block-end that define the margins in relation to the language writing mode.
1
margin
property
2
margin-inline
property
3
margin-block
property
Estimated overall support.
97%
95%
95%

Browsers on computers :

Mobile browsers :

Outdated or marginal browsers :

Internet Explorer

QQ Browser

Safari sur IOS

Firefox pour Androïd

Opéra Mobile

Baidu Browser

KaiOS Browser

UC Browser pour Androïd

Opéra

Safari

Firefox

Chrome pour Androïd

Samsung Internet

Chrome

Edge

Androïd Brower

Opéra mini

Browsers compatibility with margin.

The definition of margins was already provided for in the very first CSS language specification. It has changed little since then. The biggest change is the addition of logical properties, which take into account the writing mode according to the the language: margin-block. and margin-inline.

  • Cascading Style Sheets specification - Version 1

    First specification of the CSS language.
    First definition of the margin property and the longhand properties for each edge.
    WD
    November 17, 1995
    Working Draft.
    PR
    November 12, 1996
    Proposed for recommendation.
    REC
    December 17, 1996
    Recommendation .
    DEPR
    September 13, 2018
    Old specification, declared obsolete.
  • Cascading Style Sheets Level 2 - Revision 1 Specification

    Changed the calculation of margins when expressed as percentages.
    The inherit value is accepted for margins.
    WD
    November 04, 1997
    Working Draft.
    PR
    March 24, 1998
    Proposed for recommendation.
    REC
    May 11, 1998
    Recommendation .
  • CSS Box Model Module Level 3

    Clarification regarding the calculation of margins in the case of a ruby annotation.
    WD
    July 26, 2001
    Working Draft.
    CR
    December 22, 2020
    Candidate Recommendation.
    PR
    February 16, 2023
    Proposed for recommendation.
    REC
    April 06, 2023
    Recommendation .
  • CSS Logical Properties and Values Level 1

    Introduction of logical properties (dependent on the writing mode) to define the margins.
    WD
    May 18, 2017
    Working Draft.
    CR
    PR
    REC
  • CSS Box Model Module Level 4

    No modification to the margin... properties themselves, but addition of the margin-trim property which interacts in the calculation of the margins.
    WD
    April 21, 2020
    Working Draft.
    CR
    PR
    REC

See also, on the calculation of margins.

Margins (inner and outer) are described in this module of the CSS specification: CSS Box Model Module. The following definitions are also described in this same module.

Properties:

margin-block-end
Defines the end of the block margin, given the writing mode of the text for this element.
margin-block-start
Defines the start of the block margin, given the writing mode of the text for this element.
margin-bottom
Bottom margin.
margin-inline-end
Defines the margin after the end of line, given the writing mode.
margin-inline-start
Defines the margin before the start of the lines of text, given the writing mode.
margin-left
Marge de gauche.
margin-right
Right margin.
margin-top
Top margin.
margin-trim
Langue française
Defines the management of successive margins within the same container.
padding
Interior margins on the four sides.
padding-block-end
Defines the inner margin at the end of the block, given the writing mode.
padding-block-start
Defines the inner margin at the start of the block, given the writing mode.
padding-bottom
Inner margin at the bottom.
padding-inline-end
Defines the inner margin on the side of the end of the lines of text, given the writing mode.
padding-inline-start
Sets the padding on the side of the beginning of lines of text, given the writing mode.
padding-left
Interior margin on the left.
padding-right
Inner margin on the right.
padding-top
Inner margin at the top.