Order of the Day: CSS Properties
- In Summary
-
My preferred method for ordering properties within a CSS declaration block.
- Contents
- Headline h3 Lorem
- Ipsum Dolor
- Some Other Sub Headline
- In Conclusion
I’ve posted my preferred way of ordering CSS properties within a declaration block to its own page in the Code section. It’s evolved over time, but what I decided on goes something like this:
- Display & Flow
- Positioning
- Dimensions
- Margins, Padding, Borders, Outline
- Typographic Styles
- Backgrounds
- Opacity, Cursors, Generated Content
Here’s the code example:
el { display: ; visibility: ; float: ; clear: ; position: ; top: ; right: ; bottom: ; left: ; z-index: ; width: ; min-width: ; max-width: ; height: ; min-height: ; max-height: ; overflow: ; margin: ; margin-top: ; margin-right: ; margin-bottom: ; margin-left: ; padding: ; padding-top: ; padding-right: ; padding-bottom: ; padding-left: ; border: ; border-top: ; border-right: ; border-bottom: ; border-left: ; border-width: ; border-top-width: ; border-right-width: ; border-bottom-width: ; border-left-width: ; border-style: ; border-top-style: ; border-right-style: ; border-bottom-style: ; border-left-style: ; border-color: ; border-top-color: ; border-right-color: ; border-bottom-color: ; border-left-color: ; outline: ; list-style: ; table-layout: ; caption-side: ; border-collapse: ; border-spacing: ; empty-cells: ; font: ; font-family: ; font-size: ; line-height: ; font-weight: ; text-align: ; text-indent: ; text-transform: ; text-decoration: ; letter-spacing: ; word-spacing: ; white-space: ; vertical-align: ; color: ; background: ; background-color: ; background-image: ; background-repeat: ; background-position: ; opacity: ; cursor: ; content: ; quotes: ; }
The somewhat subjective idea is to apply properties in the order that they affect the box model. Did I mention subjective? Perhaps it is, but it makes sense to me and I’m sticking with it. This is more of a self-imposed guideline than it is any kind of outward recommendation.
There is no right or wrong way to order CSS properties, but I’m a strong proponent of picking some kind of method and sticking with it for the sake of consistency.
Why Not Alphabetical?
Let me first say that while I have my own preference, my next choice would be to alphabetize. What I don’t like about alphabetization is that certain properties – in my mind – have inherent logical connections and should not be separated from each other. Probably the best example is width and height (and min-width and min-height). Another example is position, top, right, bottom, and left. To me, these properties are too interdependent to separate.
Typographically, I really like font-family, font-size, and line-height grouped together – and in that order. I also think that other typographic properties like letter-spacing, text-transform, and color all group with each other conceptually and I’d rather not have them separated in the alphabetical shuffle.
class=“advice” rel=“nofollow”
If the approach I take doesn’t suit you, then by all means go with what works for you. My only recommendation is to adopt some kind of order rather than none.
What About CSS3?
You may have noticed a lack of CSS3 properties. I’ve only worked on a few projects where it was practical to use CSS3 properties (and/or their browser-specific implementations). And these have mostly been limited to border-radius, text-shadow, and box-shadow. I found myself putting border-radius and box-shadow near the end with the ‘Background’ and ‘Opacity et al’ groups; and I put text-shadow in with the Typographic Styles group. While ‘border-radius’ sounds like it might logically group with ‘border’, I don’t think it does as the box model is not impacted by ‘border-radius’ in the way it is with ‘border’ (increase in overall width and/or height). To me, it’s a visual effect more akin to something like ‘background’.
Further Reading
I couldn’t find a lot that has been written on this subject. Perhaps it’s absolute minutiae. Perhaps CSS developers have more important things to worry about.
Doug Bowman spent a paragraph on it 4 years ago. Makes me wonder if his approach has evolved or stayed mostly the same.
CSS Property Order Convention by Neatly Sliced recently advocated alphabetization.
I started this thread on SitePoint a few weeks ago.
A recent article on NETTUTS recommended alphabetization and (as is not uncommon for these types of matters) an all out holy war ensued in the comments.
An article at Perishable Press advocates shares one man’s personal preference for ordering property/value pairs based on their character count from longest to shortest. No seriously, I’m not making that up – check it out.
So How Do You Handle CSS Property Ordering?
I’m interested to hear from my fellow CSS developers on this. How do you order CSS properties and what’s your reasoning behind your approach?
end
Comments, Quips & Protestations
1 February 19, 2009 10:27 am Bruno Augusto
2 February 19, 2009 11:04 am Allison Richmond
3 February 19, 2009 11:15 am Bruno Augusto
4 February 19, 2009 1:03 pm Denis Korneev =)
5 February 19, 2009 1:42 pm Tony Dunsworth
6 February 19, 2009 2:33 pm Andy Ford
7 February 19, 2009 2:43 pm Ordenación del CSS | aNieto2K
8 February 19, 2009 4:22 pm Ordenación del CSS : Blogografia
9 February 19, 2009 8:35 pm Zach S.
10 February 19, 2009 8:40 pm CSS???????? - ????
11 February 19, 2009 9:07 pm Andy Ford
12 February 20, 2009 4:18 am Bruno Augusto
13 February 20, 2009 7:43 am oliver
14 February 20, 2009 8:09 am Andy Ford
15 February 20, 2009 5:30 pm The Rollsteady Network » Blog Archive » links for 2009-02-20
16 February 21, 2009 6:04 pm Jeff Starr
17 February 22, 2009 8:53 am CSS???????? — qybaby Blog Life
18 February 22, 2009 2:37 pm Andy Ford
19 March 12, 2009 9:40 am Ordem de declaração das propriedades CSS » CSS no Lanche
20 April 7, 2009 5:28 am La forma correcta de estructurar nuestras CSS | Mareos de un geek