Skip to main content

CSS Margins

The CSS margin properties create space around an element, outside of its border.

Margins set the distance between an element's border and the elements around it.

CSS lets you set the margin for each side of an element (top, right, bottom, left), and there is a shorthand property for setting them all at once.

Example

This element has a margin of 70px on every side.

Prompt
An HTML page that shows a single element with a large margin around it. In an internal <style> block, give a <div> a margin of 70px and a visible border and background so the space around it is clear. Put some text inside the div.
Output: complete HTML page

Margin - Individual Sides

CSS has a property for each side: margin-top, margin-right, margin-bottom, and margin-left.

All margin properties can take these values:

  • auto — the browser works out the margin.
  • a length — a margin in px, pt, cm, and so on.
  • a percent — a margin as a percent of the containing element's width.
  • inherit — the margin is taken from the parent element.

Tip: Negative values are allowed.

Example

Each side of the paragraph gets a different margin.

Prompt
An HTML page that sets a different margin on each side of an element. In an internal <style> block, give a <p> margin-top 100px, margin-bottom 100px, margin-right 150px, and margin-left 80px. Give it a border so the margins are visible. Add a paragraph.
Output: complete HTML page

Margin - Shorthand Property

To make the code shorter, you can set all the margins in one margin property, which stands for margin-top, margin-right, margin-bottom, and margin-left.

The number of values changes which sides they apply to:

  • Four values (25px 50px 75px 100px): top, right, bottom, left.
  • Three values (25px 50px 75px): top, then right and left, then bottom.
  • Two values (25px 50px): top and bottom, then right and left.
  • One value (25px): all four sides.

Four values

Sets the top, right, bottom, and left margins in order.

Prompt
An HTML page that uses the margin shorthand with four values. In an internal <style> block, give a <p> margin: 25px 50px 75px 100px (top, right, bottom, left) and a border so the margins show. Add a paragraph.
Output: complete HTML page

Three values

Sets the top, then the right and left, then the bottom.

Prompt
An HTML page that uses the margin shorthand with three values. In an internal <style> block, give a <p> margin: 25px 50px 75px (top, right and left, bottom) and a border so the margins show. Add a paragraph.
Output: complete HTML page

Two values

Sets the top and bottom, then the right and left.

Prompt
An HTML page that uses the margin shorthand with two values. In an internal <style> block, give a <p> margin: 25px 50px (top and bottom, then right and left) and a border so the margins show. Add a paragraph.
Output: complete HTML page

One value

Sets the same margin on all four sides.

Prompt
An HTML page that uses the margin shorthand with one value. In an internal <style> block, give a <p> margin: 25px (all four sides) and a border so the margins show. Add a paragraph.
Output: complete HTML page

The auto Value

Set margin to auto to center the element horizontally in its container. The element keeps its set width, and the leftover space is split evenly between the left and right margins.

Example

The 300px-wide div is centered using margin: auto.

Prompt
An HTML page that horizontally centers an element with margin auto. In an internal <style> block, give a <div> a width of 300px, margin: auto, and a 1px solid red border. The div should sit centered in its container. Put some text inside.
Output: complete HTML page

The inherit Value

Set margin to inherit to take the margin from the parent element.

Example

The paragraph's left margin is inherited from its parent div.

Prompt
An HTML page that shows the inherit value for margin. In an internal <style> block, give a <div> a 1px solid red border and margin-left 100px. Give a paragraph with class "ex1" margin-left: inherit, so it copies the left margin from the parent div. Put the paragraph inside the div.
Output: complete HTML page

All CSS Margin Properties

A summary of the margin properties:

  • margin — sets all the margins in one declaration.
  • margin-bottom — sets the bottom margin.
  • margin-left — sets the left margin.
  • margin-right — sets the right margin.
  • margin-top — sets the top margin.