Principal Software Engineer & Engineering Manager at Pivotal
May. 20, 2009•0 likes•43,029 views
1 of 68
What is Object Oriented CSS?
May. 20, 2009•0 likes•43,029 views
Report
Technology
More on Object Oriented CSS at the Silicon Valley JavaScript Meet Up. How to extend objects, avoiding location dependent styles, and best practices for fast sites.
17. OBJECT ORIENTED CSS
best practices for performance,
scalability, and most importantly,
easy to use.
18. 1. Create objects rather than pages or modules
2. Set good global defaults on content objects
3. Abstract reusable elements
4. Separate structure and skin (into two classes)
5. Separate container and content (open editable
zones)
6. Tame the cascade
7. Use multiple classes to simulate OO
21. ABSTRACT REUSABLE
ELEMENTS?
to allow maximum scalability, and less code
22. Contour blocks Background blocks Content Objects -
headings, paragraphs, lists, headers,
footers, buttons, etc.
Capital of the Canterbury region and the largest city
on the South Island (population just over 300,000)
exudes a palpable air of gentility and a connectedness
with the mother country.
Read more...
X X
23. TAME THE CASCADE
Apply classes to the object we wish to extend, rather than
random parent nodes. Predictability!
24. TAMING THE CASCADE
❖ Within any one object use the cascade fully
❖ All sub-nodes of an object should be prefaced with the object
class name. For example: .mymodule .inner{...}
❖ Avoid the cascade as much as possible between objects
26. 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.
image
or flash OPEN EDITABLE ZONE
29. 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.
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.
MEDIA
Extending objects, a simple
example.
38. WHAT BELONGS IN THE
SKIN?
Every value in the skin should be predictable, something that
can easily be calculated or measured.
Colors
Borders
Images
41. WHAT NOT TO DO!
inappropriate dependence on the cascade is not object
oriented.
51. A Heading should not become a Heading
in another part of the page.
52. AVOID
CODE 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
53. AVOID
CODE 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
54. CONSISTENCY
Writing more rules to
overwrite the crazy rules from
before.
e.g. Heading should behave
predictably in any module.
56. NEED MORE THAN 6
HEADINGS?
Really? Any duplicates? Any too similar?
57. STILL NEED MORE
HEADINGS?
.category{...}
.section{...}
.product{...}
.prediction{...}
❖ Extend the heading object via a class on the object itself
❖ Avoid using the cascade to place classes on parent objects
63. TEMPLATE
• 807 bytes
• 13 lines of code
• easily
extended to custom
page and column width
64. 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
65. MODULE
• 1.3K (structure)
• 22 lines of code
• Width and height agnostic
• Three structures
• 264 different combos of
skins
66. JS COMMUNITY
How to remain JS lib agnostic?
DHTML objects?
Easily adding behaviors?
Trigger parameters?
67. FLICKR PHOTO CREDITS
❖ “spaghetti con salsa de tomate y carne + albahaca” by pablovenegas
❖ “Jenga” by j3ku (3 photos, same title)
❖ “Sandbox Fun” by engelsrud
❖ “Golden Gate from Alcatraz” by Tolka Rover
❖ “Treasure Island / The Island / L'île Perdu / La Isla Maravillosa - Version II” by Aaron Escobar
❖ “Razor Wire (01)” by Amy
❖ “At the landfill” by digitalsadhu
❖ “Happy Day” by Daniel Gebhart
❖ “Finding Time to Laugh” by Leepack
❖ “Very Happy Baby” by Yogi
68. LET’S KEEP TALKING
❖ “Stubbornella” on the web. Twitter, doppler, everywhere...
❖ http://stubbornella.org
❖ nicole@stubbornella.org
Editor's Notes
After which he said that “CSS is broken”
Very common to say that CSS is broken when it is misunderstood.
“Emerging frameworks are a sign that CSS is broken.”
Java developers -- Math class
TRANSITION
On the other hand, I honestly do believe we are doing it wrong.
After which he said that “CSS is broken”
Very common to say that CSS is broken when it is misunderstood.
“Emerging frameworks are a sign that CSS is broken.”
Java developers -- Math class
TRANSITION
On the other hand, I honestly do believe we are doing it wrong.
Modular CSS is a great idea in theory, but in practice...
Nothing is shared between modules, each reinvents the CSS wheel. Wastefully file sizes and http requests grow and...
What is ... ?
lists, headings, etc should all be global
grids
contours X backgrounds X content objects = complexity
1 X foo
bar X 1
2 X 4 = 8 HTTP req
5 X 5 = 25 HTTP req
Classes on object we wish to extend.
media, media extended, wrap
Open editable zone
media, media extended, wrap
Open editable zone
media, media extended, wrap
Open editable zone
function created to return area that occasionally returns the diameter instead.
There is a general belief in CSS development that there are many ‘right’ ways. This isn’t always true.
Each time you analyze a solution, there are three main metrics that should be considered.