November 4, 2003

CSS Shorthand

Real quick tip here... something that I constantly forget. CSS Shorthand. In particular, when applied to margin or padding.

Say you have the following code:
p {
margin: 100px 20px 100px 20px;
}

This shorthand reads clockwise from the top. Margin of 100px from the TOP, then 20px on the RIGHT, then BOTTOM then LEFT.

What I always forget is that you can shorthand this shorthand.

p { margin: 20px 100px; }

What happens here is that the horizontal is read, then the vertical. So with this block of code you have a top and bottom margin of 20px, and a right and left margin of 100px.

If there is only one value, it applies to all sides. If there are two values, the top and bottom margins are set to the first value and the right and left margins are set to the second. If there are three values, the top is set to the first value, the left and right are set to the second, and the bottom is set to the third. If there are four values, they apply to the top, right, bottom, and left, respectively.

Anyways, I figured since I always forget that, you might as well. So here it is.

I talk more about CSS Shorthand elsewhere as well. More CSS Shorthand and More about the clock are two more entries that you might find interesting.

Post Info

Tagged As CSS, Reference

Comments are Open (12)

Posted at 11:06 AM

Comments

Ahmed k

Hum, NO!

I think you have a typo. Its margin: top-bottom left-right :)
ref: zvon.org

hope this helps

Ahmed k

sorry, just to be clear:

p {
margin: 20px 100px;
}

Is same as > margin:20px 100px 20px 100px;

Tony

Oh, you're absolutely correct. I did write that in wrong. Meant to write the other way.

Which is probably why I can never remember it. I corrected that, and added in the relevant quote from the W3C.

I hate typing in wrong information. I'm halfway tempted to delete your comments and just pretned that I was right all along. Oh well.

Ahmed k

Bah, its ok.

I?d do many more mistakes if my site actually had any content in it.

Jason

Introduction to CSS shorthand properties
http://home.no.net/junjun/html/shorthand.html

Anne

You can try to remember it like TRouBLe => TopRightBottomLeft. Trick/tip from Eric Meyer.

Tony

The using all four in a row doesn't mess me up. It's using the two that I always forget or switch.

Tony

Don't be an idiot Apocalypse. What part of the opening sentence did you not understand?

"Real quick tip hereƖ something that I constantly forget. CSS Shorthand. In particular, when applied to margin or padding."

Not a guide. A tip. Something that I tend to forget. Not for all of the items. But just margin or padding.

Read before you post.

Tate

Such polite spam.

That kid probably stumbled on this page through cssvault.com - this entry is listed as "CSS Shorthand."

I always had to double check a reference when I tried the short method. Or I resorted to padding-left, etc... That is until I started thinking of it as clockwise, starting from 12 o'clock.

Also, to add to your tip:

p { margin: 2px 3px 0 10px }

I believe is also valid.

Tony

Yeah, that spam just slipped through. I hate comment spammers. There really isn't a punishment brutal enough. I'll have to go delete him now.

Tate

Are you running MT Blacklist? Never tried it myself, but it might help keep the place nice and tidy.

Also the newer versions of MT encrypt the URL's provided by commenters.

Tony

Yeah, I'm running Blacklist. It still has limitations though. Waiting for v3, see if TypeKey solves some of these problems.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Search
Comments on this post
Categories
Info
Tony Stephens
Copyright © 1995-2005
Site Version:
10
Licensed:
Creative Commons
Validate:
XHTML, CSS, 508, RSS
Subscribe
Blog-Fu, Link-Fu