Visser Labs

Margin and padding explained; CSS tutorial

By Michael Visser.

Let’s step back and start at the basics, in order to understand these element properties it’s best to remove any background confusion. Internet browsers (eg. Internet Explorer, Firefox, Camino, …) typically apply their own default values to page elements, this is done to create a standard experience across non-styled pages. We need to disable these default values as they differ across browsers, then I can teach you how these properties influence how elements are rendered.

To remove the default browser styles from the red and blue boxes below you would type in CSS:

.red {
margin:0;
padding:0;
}
.blue {
margin:0;
padding:0;
}

Default state of elements

Margin
The larger the margin the further apart an element will be positioned in that given direction.

Example: In the below illustration a margin-bottom style of 2 blocks has been applied to the top (red) element, this creates a space of 2 blocks below the bottom (blue) element.

To apply a bottom margin to the red element you would type in CSS:

.red {
margin-bottom:20px;
}

Bottom margin applied to the top (blue) element.

Padding
The larger the padding the greater expansion of that element in that given direction.

Example: In the below illustration a padding-bottom style of 2 blocks has been applied to the top (red) element, this expands the red element vertically 2 blocks in turn pushing the the bottom (blue) element down 2 blocks.

To apply a bottom padding to the red element you would type in CSS:

.red {
padding-bottom:20px;
}

Bottom padding applied to the top (blue) element.

Zero (0) shortcut
0px
can be shortened to 0; however all ‘real’ numbers must be followed by a metric, for example:

  • px
  • em
  • %

Squish properties together
margin-top: 50px;
margin-right: 4em;
margin-bottom: 10%;
margin-left: 0;

can be shortened to…

margin: 50px 4em 10% 0;

Getting lazy? Maybe productive
padding:1.2em 5px;

is interpreted as…

padding:1.2em 5px 1.2em 5px;

Social Bookmarks

  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • StumbleUpon
  • Technorati

About the article

This article was written on 2007-03-17 at 20:11:36+0000, filed under Uncategorized and tagged , .

Margin and padding explained; CSS tutorial is rated:
1 Star2 Stars3 Stars4 Stars5 Stars (1 ratings)
Loading ... Loading ...

About the author

Michael Visser is the founder of Visser Labs. He works as a professional Front-end Web Developer, loves Wordpress, Joomla and helping people get more out of their sites.

Suggest a future article or if you want to chat, get in touch today »


6 Comments

  1. Biscuitrat said:

    Very nice! I’m gonna recommend this quick guide to a CSS beginner; hope to see more :)

    Comment by Biscuitrat — March 18, 2007 @ 4:07 pm

  2. Michael said:

    Thanks Ranjani, thought it was about time to explain it! I only recently figured it out myself and had a collegue ask the same thing.

    I hope to later elaborate on the correct times to use padding vs margin, that’ll be a real stir.

    I’m putting an application in to Mint Pages!

    Comment by Michael — March 18, 2007 @ 4:24 pm

  3. Michael said:

    I’m not totally sure whether I should make this comment or not, but I think I’m the beginner that Ranjani reffered this article to. Although I haven’t mastered basic layouts yet, I have this page bookmarked for future reading.

    BTW, I’m also at Mint Pages, along with Ranjani.

    Comment by Michael — March 19, 2007 @ 11:12 am

  4. Michael said:

    Thanks for coming Michael.

    I’m about to start writing a howto article on the effect of the CSS float property effects page elements. Hopefully it can help you as you’re learning the HTML/CSS ropes.

    What articles would you like to read and for me to write?

    Comment by Michael — March 21, 2007 @ 8:47 pm

  5. Andy said:

    That solves a lot.

    Thanks!

    Comment by Andy — May 27, 2008 @ 5:01 am

  6. Michael said:

    Glad it helped.

    Comment by Michael — June 7, 2008 @ 11:56 am

RSS feed for comments on this page.

Sorry, the comment form is closed at this time.