Margin and padding explained; CSS tutorial

1 Star2 Stars3 Stars4 Stars5 Stars (1 ratings)
Loading ... Loading ...

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 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.

Stumble it!

6 Comments

  1. March 18, 2007 at 4:07 pm

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

  2. March 18, 2007 at 4:24 pm

    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!

  3. March 19, 2007 at 11:12 am

    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.

  4. March 21, 2007 at 8:47 pm

    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?

  5. May 27, 2008 at 5:01 am

    That solves a lot.

    Thanks!

  6. June 7, 2008 at 11:56 am

    Glad it helped.

Post a Comment

Your email is never shared. Required fields are marked *

*
*