Smal SEO Tool

CSS Margins

Margins are used to create space around elements, outside of any defined borders.

CSS has properties for specifying the margin for each side of an element: margin-top, margin-right, margin-bottom, and margin-left.

Example

css
p {
  margin-top: 100px;
  margin-bottom: 100px;
  margin-right: 150px;
  margin-left: 80px;
}

Margin Shorthand Property

To shorten the code, it is possible to specify all the margin properties in one property. The margin property is a shorthand property for this.

Four values:

css
p {
  margin: 25px 50px 75px 100px; /* top, right, bottom, left */
}

Three values:

css
p {
  margin: 25px 50px 75px; /* top, horizontal, bottom */
}

Two values:

css
p {
  margin: 25px 50px; /* vertical, horizontal */
}

One value:

css
p {
  margin: 25px; /* all four sides */
}

Test Yourself with an Exercise

In the shorthand `margin: 10px 20px;`, what does the `20px` value represent?