Margin and padding explained; CSS tutorial
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;
}

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;
}

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;
}

Zero (0) shortcut can be shortened to
0px0; 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;
About the article
This article was written on 2007-03-17 at 20:11:36+0000, filed under Uncategorized and tagged Internet, Technology.
Enjoy this article? Get future updates sent to you for free! Join by subscribing to the Visser Labs RSS feed.
Did this article solve that killer problem you've been working hard at for the last day?
Give back by making a small donation, it's quick, easy and a great thank you! Make a small donation today »










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
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
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
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
That solves a lot.
Thanks!
Comment by Andy — May 27, 2008 @ 5:01 am
Glad it helped.
Comment by Michael — June 7, 2008 @ 11:56 am