One of the things that used to annoy me about working with CSS, is that I had a mental block about margin and padding. I could never recall which was which, because the two terms mean pretty much the same thing; an arbitrary space around text. Frankly I think the creators of CSS should have used have called them inner-margin and outer-margin, or inner-padding and outer-padding. I'm sure there is a good reason for using the terms margin and padding (I'm guessing its from typesetting nomenclature), but to me they feel arbitrary and may have well have been called bob and jane.

Fortunately I came up with a mnemonic that made it click. Just remember the phrase 'padded cell'. In a padded cell, the padding goes on the inside. So there you have it, you'll never again forget that the box model is text -> padding -> border -> margin.

This blog post was posted to It's All Geek to Me on Monday June 30th, 2008 at 3:46PM
 

4 Responses to "A CSS Mnemonic"

  • June 30th, 2008, 8:27 p.m.

    Great tip - simple - thanks

    Try out firebug for Firefox. This has a beautiful feature called layout which shows the size of an element, padding, border, margin and offset.

  • July 1st, 2008, 12:12 a.m.

    To make matters more confusing adjacent margins can be collapsed together, while padding cannot. Descendant boxes inside a box with no padding can also have their margins collapsed in such a way that the margin is actually outside of the parent box.

    These effects are actually pretty useful with things like paragraphs, but it can be pretty confusing when you're just dealing with divs or trying to style elements that have margins by default.

  • July 1st, 2008, 8:51 p.m.

    Do you know the one for the order of values for margin and/or padding?
    (If you do, my apologies.)

    margin:1px 2px 3px 4px;

    "Trouble" Top(1px) , Right(2px), Bottom(3px), Left(4px)

    It's the only way I remember.

  • July 2nd, 2008, 5:05 p.m.

    Heather, thats a good one! I've not had much trouble remember the order there. I think of it as a clock hand starting at 12 and moving round to 9.

Leave a Comment

You can use bbcode in the comment: e.g. [b]This is bold[/b], [url]http://www.willmcgugan.com[/url], [code python]import this[/code]
Preview Posting...
Previewing comment, please wait a moment...
Will McGugan

My name is Will McGugan. I am an unabashed geek, an author, a hacker and a Python expert – amongst other things!

You are reading my tech blog. See the homepage for my other blogs.

Search for Posts
Possibly related posts
Tags
 
Popular Tags
 
Archives
2010
 
Recent Comments
http://www.iclshoes.com/alexander-mcqueen-c-13.html [iclshoes.com] http://www.iclshoes.com/jimmy-choo-shoes-c-2.html [iclshoes.com] http://www.iclshoes.com/ [iclshoes.com] http://www.zentai-mart.com/Play-Costumes-c-5.html [zentai-mart.com] http://www.zentai-mart.com/Latex-Catsuits-Clothes-c-3.html [zentai-mart.com] http://www.zentai-mart.com/PVC-Catsuits-Clothes-c-6.html [zentai-mart.com] http://www.zentai-mart.com/ [zentai-mart.com] http://www.hereshoes.com/miu-miu-shoes-c-31.html [hereshoes.com] http://www.hereshoes.com/giuseppe-zanotti-c-43.html [hereshoes.com] http://www.hereshoes.com/lanvin-shoes-c-50.html [hereshoes.com] ...
- Christian Louboutin on Turning website favicons in to 3D
What are the charmings of?a href="http://www.iclshoes.com [iclshoes.com]cl shoes/a?They are quality,comfort and style.The a href=http://www.iclshoes.com/jimmy-choo-shoes-c-2.html [iclshoes.com]Jimmy Choo shoes/a are made from ...
- Christian Louboutin on Turning website favicons in to 3D
Thanks a lot for that: I had first tried sudo aptitude purge adobe-flashplugin then sudo aptitude install flashplugin-nonfree but that ...
Andre, the name is derived from the class name (camel case converted to lower case with underscores). But, you can ...
is this using some kind of name mangling to map sorethumb:rounded_corners_edged to the RoundedCornersEdged class? that seems a little unnecessary ...
 
© 2008 Will McGugan.

A technoblog blog, design by Will McGugan