Principal Software Engineer & Engineering Manager à Pivotal
26 Sep 2009•0 j'aime•11,590 vues
1 sur 153
The Cascade, Grids, Headings, and Selectors from an OOCSS Perspective, Ajax Experience 2009
26 Sep 2009•0 j'aime•11,590 vues
Signaler
Technologie
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.