CSS Padding and Margin

Written by Michael Gearon on the

Last updated 26

The most two popular CSS properties are margin and padding, they are used for spacing-out elements. Simply put the padding is the space inside something whilst the margin is the space outside of something.

Margin

The margin property defines the space around elements. Without the use of margins the elements would be right next to each other which could make a website look terrible (don’t forget about whitespace). There is four properties you can define:

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

You can also use the shorthand property to condense this:

  • margin: 25px 75px 25px 110px;
    • top margin is 25px
    • right margin is 75 px
    • bottom is 25px
    • left is 110px
  • margin: 25px 75px 100px
    • top margin is 25px
    • right and left margins are 75px
    • bottom margin is 100px
  • margin:25px 75px
    • top and bottom margins are 25px
    • right and left are 75px
  • margin:20px
    • all four sides are 20px

Where possible use the shorthand properties for better performance and readability.

Padding

The padding property defines the space inside an element. There is four properties you can define:

  • padding-top
  • padding-bottom
  • padding-right
  • padding-left

You can also use the shorthand property to condense this:

  • padding: 25px 75px 25px 110px;
    • top padding is 25px
    • right padding is 75 px
    • bottom padding is 25px
    • left padding is 110px
    • padding: 25px 75px 100px
    • top padding is 25px
    • right and left padding are 75px
    • bottom padding is 100px
    • padding:25px 75px
    • top and bottom padding are 25px
    • right and left padding are 75px
    • padding:20px
    • all four sides are 20px

Where possible use the shorthand properties for better performance and readability.

What is the box model?

The box model is a combination of margins, padding and borders.

Written by Michael Gearon

Michael Gearon is a UX Designer at BrandContent. Specialising in voice assistants and CRO, I have a wealth of experience in UX, UI and website design. Based in Cardiff, South Wales Mike can help local businesses with their online presence.

So now what?

Let's chat, and get things going