The cascade is a poker game, but we've been playing our cards all wrong. Here Nicole suggests we stop trying to play to win to prevent code bloat, and simplify the cascade, using the order of the rulesets to allow overrides.
13. FROM THE SPEC:
❖ Count 1 if the declaration is from is a 'style' attribute rather
than a rule with a selector, 0 otherwise (= a) (In HTML, values
of an element's "style" attribute are style sheet rules. These
rules have no selectors, so a=1, b=0, c=0, and d=0.)
❖ Count the number of ID attributes in the selector (= b)
❖ Count the number of other attributes and pseudo-classes in
the selector (= c)
❖ Count the number of element names and pseudo-elements in
the selector (= d)
14. IT GOES ON...
❖ The specificity is based only on the form of the selector. In
particular, a selector of the form "[id=p33]" is counted as an
attribute selector (a=0, b=0, c=1, d=0), even if the id attribute
is defined as an "ID" in the source document's DTD.
❖ Concatenating the four numbers a-b-c-d (in a number system
with a large base) gives the specificity.
64. A Heading should not become a Heading
in another part of the page.
65. AVOID
EXAMPLE
#weatherModule h3{color:red;}
#tabs h3{color:blue}
❖ Global color undefined for h3, so it will be
‣ unstyled in new modules,
‣ developers forced to write more CSS for the same style
66. AVOID
EXAMPLE
h3{color:black;}
#weatherModule h3{color:red;}
#tabs h3{color:blue}
❖ Global color defined (better!)
❖ Weather and tabs override default h3
‣ three styles for h3 cannot co-exist in the same module
‣ default style cannot be used in weather and tabs without costly
overrides
❖ Weather and tabs h3 can never be used outside of those
modules
67. CONSISTENCY
Writing more rules to
overwrite the crazy rules from
before.
e.g. Heading should behave
predictably in any module.
69. NEED MORE THAN 6
HEADINGS?
Really? Any duplicates? Any too similar?
70. STILL NEED MORE
HEADINGS?
.category{...}
.section{...}
.product{...}
.prediction{...}
❖ Extend the heading object via a class on the object itself
❖ Avoid using the cascade to change display of nested objects
73. TEMPLATE
• 807 bytes
• 13 lines of code
• easily
extended to custom
page and column width
74. GRIDS
• 574 bytes
• 14 lines of code
• Percentage widths adapt to
different page sizes
• Includes halfs, thirds, fourths, and
fifths
• No gutters
• Infinite nesting and stacking
92. div{...}
X
ul{...}
p{..}
AVOID STYLING ELEMENTS
unless defining defaults
.error{...}
.section{...}
.products{...}
93. html body .myModule div .hd{...}
.myModule2 .hd {...}
GIVE RULES THE
SAME STRENGTH
Use cascade order to overwrite previous rules
94. X
html body .myModule div .hd{...}
.myModule2 .hd {...}
GIVE RULES THE
SAME STRENGTH
Use cascade order to overwrite previous rules
.myModule .hd{...}
.myModule2 .hd{...}
95. .mod .hd{...}
.ie .mod .hd{...}
.weatherMod .hd {...}
USE HACKS SPARINGLY
And don’t let them change your specificity
96. .mod .hd{...}
X
.ie .mod .hd{...}
.weatherMod .hd {...}
USE HACKS SPARINGLY
And don’t let them change your specificity
.mod .hd{color: red; _zoom:1;}
.weatherMod .hd{...}
100. AVOID OVERLY SPECIFIC
CLASSES
they’re all semantic, but limiting
.vehicle{...}
.motorcycle{...}
.ducati{...}
.ducatiMonster620{...}
.nicolesDucatiMonster620{...}
101. AVOID OVERLY SPECIFIC
CLASSES
they’re all semantic, but limiting
.vehicle{...}
.motorcycle{...}
.ducati{...}
X
.ducatiMonster620{...}
.nicolesDucatiMonster620{...}
102. #myUniqueIdentifier{...}
#myUniqueIdentifier2{...}
AVOID SINGLETONS
ids can only be used once in any given page
Source: Chris Griego
103. X
#myUniqueIdentifier{...}
#myUniqueIdentifier2{...}
AVOID SINGLETONS
ids can only be used once in any given page
Source: Chris Griego
104. Media
Subheading
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis
aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
USE MIXINS
to avoid repeating code
Media Extended
Subheading
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis
aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
105. .inner{...} .weatherMod .inner{...}
.tr{...} .weatherMod .tr{...}
.bl{...} .weatherMod .bl{...}
ENCAPSULATION
don’t access sub-nodes of an object directly
inner
106. .inner{...}
X
.weatherMod .inner{...}
.tr{...} .weatherMod .tr{...}
.bl{...} .weatherMod .bl{...}
ENCAPSULATION
don’t access sub-nodes of an object directly
inner
107. OOCSS: A REAL LIFE
EXAMPLE
Gonzalo Cordero
Front-End Engineer @Yahoo! inc.
127. WEB COMPROMISES
❖ Why does the “easy” turn so complicated?
❖ Why do we need to compromise?
128. WEB COMPROMISES
❖ Why does the “easy” turn so complicated?
❖ Why do we need to compromise?
❖ Why can’t we rely on a set of solid rules and structures? Like
other platforms do?
131. OOCSS SAVES THE DAY
❖ Single simple markup structure
❖ Cross browser support (even IE 5.5!)
132. OOCSS SAVES THE DAY
❖ Single simple markup structure
❖ Cross browser support (even IE 5.5!)
❖ Very minimum CSS
133. OOCSS SAVES THE DAY
❖ Single simple markup structure
❖ Cross browser support (even IE 5.5!)
❖ Very minimum CSS
❖ Scalable, easy to apply to multiple designs
147. CSSBUILDR
❖ If we said we could reuse the same structure over and over
again, what’s stopping us from doing so?
❖ Different modules and page grids can be easily built.
❖ Plug and Play, Mix and Match.
148. CSSBUILDR: TECH SPECS
❖ Create and modify module contours, backgrounds, font-
colors, etc.
❖ In two to three clicks you get a plug-and-play ready module
that works cross browser.
❖ Built on top YUI.
149. THANK YOU
DEMO TIME
❖ ygonzalocordero@me.com
❖ Twitter: goonieiam
150. PHOTO CREDITS
Bernese Mountain Dog in a Suitcase by Sig20171
Family Outing by Jimbob
The fast and the furious Car Show by Rob Young
Young man and woman taking pictures of each other by RalphBijker
Motion Gears - team force by RalphBijker
Weighing In by Smig44_uk
151. PHOTO CREDITS
• “South Carolina” by Army.Mil
• “So Happy Together” by kalandrakas
• “sometimes, a hug is all what we need” by kalandrakas
• “Blueprint background” by Ricketts Fish
• Spaghetti Monster
• Food Photography - Spaghetti Tossed with Veg Oil
• Spaghetti alla bolognese
152. PHOTO CREDITS
• “You Crack Me Up” by http://flickr.com/photos/nickwheeleroz/2474196275/in/photostream/
• “red lego” by http://flickr.com/photos/niznoz/5753993/
• “Pablo’s cubism period began at three” by http://flickr.com/photos/wwworks/2475349116/in/set-72157608035966422/
• “Kuwait water tower” by http://flickr.com/photos/asam/327911794/
• idigit_teddy: http://www.flickr.com/photos/design_inspiration/238542495/
• lucianvenutian: http://www.flickr.com/photos/lucianvenutian/1142630637/
• Gimli_36: http://www.flickr.com/photos/navillot/1878124531/
• NathanFromDeVryEET: http://www.flickr.com/photos/thatguyfromcchs08/2300190277/
• Stabilo Boss: http://flickr.com/photos/stabilo-boss/101793494/in/set-72057594060779001/
• Dead Man's Hand
• Poker
• Spaghetti & Meatballs
153. LET’S KEEP TALKING...
http://stubbornella.org
nicole@stubbornella.org
“stubbornella” on the web...
twitter, dopplr, slideshare, everywhere...
http://github.com/stubbornella/oocss/
http://groups.google.com/group/object-oriented-css
Editor's Notes
Who am I?
I am a front-end architecture consultant, working on w3c beta, facebook. My focus is building building scalable CSS. That might mean millions of visitors, thousands of pages, or any site like a blog where content creation goes on long after the look and feel has stabilized.
Even Faster Websites, Layers Magazine, Smush it, Yslow.
Speaking the same language... avoid lingo, focus on what matters to design
Which of these will be applied in case of conflict?
No idea, the order of the classes makes no difference.
The color will be blue, no problem to declare things multiple times.
The last property value pair will win
Blue is ignored because the browser doesn’t understand the british spelling. Red will be applied.
Foo will be blue, you can define the same thing as many times as you like, the last definition will win. Much like variables.
If the same values are declared in both stylesheets, moreStyles.css will win because it comes after styles.
The paragraph will be red. IDs win over classes.
Inline styles win over external stylesheets and styles in the head
Important wins over everything, including inline styles
Certain properties are passed to child nodes. If you set a font size for example, child nodes will have the same size font unless they have a more specific rule applied.
If you apply two classes to an element, it will have the property value pairs of both. The cascade resolves conflicts. This allows you to aggregate more abstract classes with more specific, so you don’t repeat code.
Flawed implementations,
Perfectly accessible or high performance website, and then the first newbie to touch it, ruins it. Our code should be robust enough that newbies can contribute while maintaining the standards we’ve set.
couple years coding in the basement by yourself before you are remotely useful.
Profession needs to accommodate entry level, mid level, and architect level developers.
Frankly, I’m tired of writing rounded corner boxes. I’ve done it 1000 times already. What I want is a system that allows newbies to do that part so I can focus on the architect level challenges.
And for good reason. Currently there is no consistency or predictability.
New (different) html pages should be able to be built without modifying the CSS.
CSS classes - some are like classes, others like properties, and some are objects
Its about taking programming best practices and applying them to writing CSS.
Encourage reuse
If we build new HTML pages from a component library, new pages won’t require new css.
So what goes into a component library. First up, content objects.
Anything else that should be consistent site-wide.
Consistency & Semantics
You might be asking “what are location dependent styles?”, lets look at a programming parallel.
Designing site-wide legos first, then pages can help.
(better for flexible section) YDN homepage module, trying to use it on a 2 column layout
Not because you can't do it, anyone at this conf can position something to the left and something else to the right.
Demo template and grids.
.mod{defines what it means to be a module}
skins become really simple
.mod{defines what it means to be a module}
skins become really simple
.mod{defines what it means to be a module}
skins become really simple
.mod{defines what it means to be a module}
skins become really simple
avoid the specificity game
avoid the specificity game
Need hacks for browsers other than IE5.5, 6, 7? You are doing something wrong.
Need hacks for browsers other than IE5.5, 6, 7? You are doing something wrong.
YUI contributor
Work for Yahoo Home Page
CSS buildr contributor
YUI contributor
Work for Yahoo Home Page
CSS buildr contributor
YUI contributor
Work for Yahoo Home Page
CSS buildr contributor
I was recently given the task of creating this module
The challenges: Cross browser, limited amount of time, high quality markup, accessible and of course only with css.
The challenges: Cross browser, limited amount of time, high quality markup, accessible and of course only with css.
The challenges: Cross browser, limited amount of time, high quality markup, accessible and of course only with css.