margin
Quick Summary for margin
The margin CSS shorthand property sets the margin area on all four sides of an element.
Code Usage for margin
/* Apply to all four sides */ margin: 1em; margin: -3px;  /* vertical | horizontal */ margin: 5% auto;  /* top | horizontal | bottom */ margin: 1em auto 2em;  /* top | right | bottom | left */ margin: 2px 1em 0 auto;  /* Global values */ margin: inherit; margin: initial; margin: revert; margin: unset; 
More Details for margin

margin

The margin CSS shorthand property sets the margin area on all four sides of an element.

Constituent properties

This property is a shorthand for the following CSS properties:

margin-bottom margin-left margin-right margin-top

Syntax

/* Apply to all four sides */ margin: 1em; margin: -3px;  /* vertical | horizontal */ margin: 5% auto;  /* top | horizontal | bottom */ margin: 1em auto 2em;  /* top | right | bottom | left */ margin: 2px 1em 0 auto;  /* Global values */ margin: inherit; margin: initial; margin: revert; margin: unset; 

The margin property may be specified using one, two, three, or four values. Each value is a <length>, a <percentage>, or the keyword auto. Negative values draw the element closer to its neighbors than it would be by default.

When one value is specified, it applies the same margin to all four sides. When two values are specified, the first margin applies to the top and bottom, the second to the left and right. When three values are specified, the first margin applies to the top, the second to the right and left, the third to the bottom. When four values are specified, the margins apply to the top, right, bottom, and left in that order (clockwise).

Values

<length>

The size of the margin as a fixed value.

<percentage>

The size of the margin as a percentage, relative to the width of the containing block.

auto

The browser selects a suitable margin to use. For example, in certain cases this value can be used to center an element.

Description

This property can be used to set a margin on all four sides of an element. Margins create extra space around an element, unlike padding, which creates extra space within an element.

The top and bottom margins have no effect on non-replaced inline elements, such as <span> or <code>.

Horizontal centering

To center something horizontally in modern browsers, you can use display: flex; justify-content: center;.

However, in older browsers like IE8-9 that do not support Flexible Box Layout, these are not available. In order to center an element inside its parent, use margin: 0 auto;.

Margin collapsing

Elements' top and bottom margins are sometimes collapsed into a single margin that is equal to the larger of the two margins. See Mastering margin collapsing for more information.

Formal definition

Initial valueas each of the properties of the shorthand:margin-bottom: 0margin-left: 0margin-right: 0margin-top: 0
Applies toall elements, except elements with table display types other than table-caption, table and inline-table. It also applies to ::first-letter and ::first-line.
Inheritedno
Percentagesrefer to the width of the containing block
Computed valueas each of the properties of the shorthand:margin-bottom: the percentage as specified or the absolute lengthmargin-left: the percentage as specified or the absolute lengthmargin-right: the percentage as specified or the absolute lengthmargin-top: the percentage as specified or the absolute length
Animation typea length

Formal syntax

[ <length> | <percentage> | auto ]{1,4}

Examples

Simple example

HTML
<div class="center">This element is centered.</div>  <div class="outside">This element is positioned outside of its container.</div> 
CSS
.center {   margin: auto;   background: lime;   width: 66%; }  .outside {   margin: 3rem 0 0 -3rem;   background: cyan;   width: 66%; } 

More examples

margin: 5%;                 /* All sides: 5% margin */  margin: 10px;               /* All sides: 10px margin */  margin: 1.6em 20px;         /* top and bottom: 1.6em margin */                             /* left and right: 20px margin  */  margin: 10px 3% -1em;       /* top:            10px margin */                             /* left and right: 3% margin   */                             /* bottom:         -1em margin */  margin: 10px 3px 30px 5px;  /* top:    10px margin */                             /* right:  3px margin  */                             /* bottom: 30px margin */                             /* left:   5px margin  */  margin: 2em auto;           /* top and bottom: 2em margin   */                             /* Box is horizontally centered */  margin: auto;               /* top and bottom: 0 margin     */                             /* Box is horizontally centered */ 

Specifications

Specification
CSS Box Model Module Level 3 # margin

See also

Introduction to the CSS basic box model Margin collapsing margin-top, margin-right, margin-bottom, and margin-left The mapped logical properties: margin-block-start, margin-block-end, margin-inline-start, and margin-inline-end and the shorthands margin-block and margin-inline Select your preferred language English (US)DeutschEspañolFrançais日本語한국어PolskiPortuguês (do Brasil)Русский中文 (简体) Change language

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Categories in CSS
css
Search CSS
Search CSS by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote

"Olivia, my eldest daughter, caught measles when she was seven years old. As the illness took its usual course I can remember reading to her often in bed and not feeling particularly alarmed about it. Then one morning, when she was well on the road to recovery, I was sitting on her bed showing her how to fashion little animals out of coloured pipe-cleaners, and when it came to her turn to make one herself, I noticed that her fingers and her mind were not working together and she couldn’t do anything. 'Are you feeling all right?' I asked her. 'I feel all sleepy,' she said. In an hour, she was unconscious. In twelve hours she was dead. The measles had turned into a terrible thing called measles encephalitis and there was nothing the doctors could do to save her. That was...in 1962, but even now, if a child with measles happens to develop the same deadly reaction from measles as Olivia did, there would still be nothing the doctors could do to help her. On the other hand, there is today something that parents can do to make sure that this sort of tragedy does not happen to a child of theirs. They can insist that their child is immunised against measles. ...I dedicated two of my books to Olivia, the first was ‘James and the Giant Peach’. That was when she was still alive. The second was ‘The BFG’, dedicated to her memory after she had died from measles. You will see her name at the beginning of each of these books. And I know how happy she would be if only she could know that her death had helped to save a good deal of illness and death among other children."

I just checked google books for BFG, and the dedication is there. 

https://www.google.com.au/books/edition/_/quybcXrFhCIC?hl=en&gbpv=1 


Roald Dahl, 1986
Random CSS Property

font-variant-east-asian

The font-variant-east-asian CSS property controls the use of alternate glyphs for East Asian scripts, like Japanese and Chinese.
font-variant-east-asian css reference