| CSS Shorthand Guide |
| Wednesday, 27 February 2008 | |
CSS Shorthand GuideOk. Let’s set the record straight. There is no official guide for each and every CSS shorthand property value. So let’s work together and put one together shall we? Ok. Straight to the business. Anytime I’ve ran into a specification (besides the confusing mess at the W3C), it turns into showing off a couple of examples and you’re supposed to be set on your way. Well well. Over the years, I’ve found quite some interesting unknown quirky facts about these shorthands… hence this Guide was born. BackgroundBackgrounds can be tricky. Nevertheless, effective when condensed correctly. The syntax for declaring the background shorthand values are as follows: background properties
the background shorthand propertyThe UnknownOften times developers find themselves wondering By default, the background property will assume the following when you do not declare each value of the properties. default background property valuesLesson learned: be careful on what you don’t declare. By chosing to not declare a value on a shorthand property, you are explicitly declaring the above default settings. For example, let’s look at the following example. background shorthand example (unexplicit)This would be the same as declaring the following values: background shorthand example (explicit)FontFont is perhaps the trickiest. However, it follows the same rules as the background shorthand property. All that you do not declare will have unexplicit values. Here is the font shorthand specification: font propertiesThe default values for the font shorthand property are as follows: default font property valuesAnd of course without any further ado. The font shorthand property syntax: the font shorthand propertyHere is where it gets tricky. The fact that strong element styled with fontBy writing the above into your style sheet, you will be unexplicitly removing the font-weight:bold default browser style that is applied to strong elements. Last but not least (for -font- that is), a real world example: font shorthand property example (unexplicit)This would be the same as declaring the following properties: the font shorthand property (explicit)BorderLet’s not waste time discussing the warnings. The same rules apply from here on out. This is all you need to know border propertiesbecomes this: the border shorthand propertieDon’t ask me how that would look. The fact that “linen” is in there, things could get scary. Nevermind the matter, here is where ‘border’ gets funny. border examplesOne thing to specially take note about declaring a border without a color, the default will be ‘black’ unless otherwise noted through an explicit or inherited ‘color’ property. See the following examples: border color examplesGet it? Got it. Good! (isn’t that a song?) Anyway. On with this Margin and PaddingThese are by far the easiest. Just think about the hands of a clock starting at noon, and follow the hour. For the sake of brevity, we’ll be working with margin (since it’s a shorter word). So for all cases of margin, the same rules apply to padding. margin properties.… combined into the margin superpowers: the margin shorthand propertyOf course, you may declare your margin with one, two, three, or four values. Here is how each scenario will be played out: margin funUnderstood? Let’s keep going. This is fun isn’t it! (whatever, you like it). OutlineQuite frankly, this property has dropped off the existence of the design radar. Mainly because of lack of browsers supporting this CSS 2.1 standard (yep, it’s an actual property), but nonetheless, it too has a shorthand property. This property follows the exact same (or same exact - they mean the same thing) specification as the ‘border’ shorthand property. But, for purposes of this being a Guide, it must be here. So: outline propertiesOutline written as shorthand: outline shorthand propertyList-styleThis is it. The last one. It’s rarely used frequently. Hence rarely. That is why I kept it until the end (sorry, the best was first in my own opinion). Here is the list-style properties: list-style propertiesHere is the defaults: list-style property defaultsAnd for the sake of final brevity. Here is a simple example: list-style shorthand property example |