(How Not To Shoot Yourself In The Foot)
These strategies work best on
large, long-lived project that need to
change over time
"Why does this rule exist?"
"The CSS file keeps getting bigger!"
"How are there 4000 lines of CSS for so little styling?"
"I have no idea whether I can modify/delete this rule."
"What are these classes for? Styling? Javascript? Are they even doing anything?"
"Do they relate to each other?"
"I need to refactor this structure. Will it break styling or javascript if I move this class to another element?"
"I feel overwhelmed by the sheer size of these files and the thousands of classes in them!"
"I don't look forward to writing styles for my new feature."
"The thought of refactoring CSS makes me sick in the pit of my stomach."
(It's CSS's fault!)
Easy to make mistakes (and never know it until it's too late)
Everything is global
Highly order-sensitive
Difficult to communicate intent
Specificity can ruin everything
High-level disciplines to create
order out of chaos
To know:
They're just collections of methods!
Single-class selectors
Additive Cascade
Namespacing
Layers of intent
Separation of structure and skin
Components
Source: Specificity Calculator
Specificity Graph
0,0,6,0
0,0,2,0
0,1,0,0
Layout/Structure
// layers 1 and 2 (Settings and Tools) are imported into each file that needs it.
// _variables.less
// _mixins.less
// layer 3 (Generic)
@import "generic/reset.less";
// layer 4 (Elements)
@import "element/_element.less";
// layer 5 (Objects)
@import "layout/grid.less";
@import "layout/grid-fifths.less";
@import "layout/alignment.less";
@import "layout/pagesection.less";
@import "layout/flex.less";
// layer 6 (Components)
@import "component/Nav.less";
@import "component/Tab.less";
@import "component/Card.less";
@import "component/Form.less";
@import "component/Tile.less";
@import "component/List.less";
@import "component/Badge.less";
@import "component/Label.less";
@import "component/Dollar.less";
@import "component/Footer.less";
@import "component/Buttons.less";
@import "component/Example.less";
@import "component/Spinner.less";
@import "component/Tooltip.less";
@import "component/Parallax.less";
@import "component/OrderForm.less";
@import "component/VerticalNav.less";
// layer 7 (Trumps)
@import "trumps/utility.less";
@import "trumps/integrations.less";
@import "shame/prettyPhoto.less";
@import "theme/brandcolors.less";
<div class="media user premium">
<img class="img photo avatar" ... />
<p class="body bio">...</p>
</div>
<div class="media user user--premium">
<img class="media__img user__photo avatar" ... />
<p class="media__body user__bio">...</p>
</div>
<div class="o-media c-user c-user--premium">
<img class="o-media__img c-user__photo c-avatar" ... />
<p class="o-media__body c-user__bio">...</p>
</div>
An example of non-separation
A specific part of the UI. Includes HTML, CSS, JS, and other assets required to render the component in any context.
SMACSS
BEM
SUIT CSS
<div class="Tile Tile--centered u-pad">
<div class="Tile-header u-color-gray">
...
</div>
<div class="Tile-body">
<div class="Media">
<div class="Media-left"></div>
<div class="Media-body">
...
<input class="Tile-body-field is-selected">
...
</div>
</div>
</div>
</div>
<div class="tile tile--centered pad">
<div class="tile__header color-gray">
...
</div>
<div class="tile__body">
<div class="media">
<div class="media__left"></div>
<div class="media__body">
...
<input class="tile__body__field selected">
...
</div>
</div>
</div>
</div>
<div class="tile tile-centered pad">
<div class="tile-header color-gray">
...
</div>
<div class="tile-body">
<div class="l-media">
<div class="l-media-left"></div>
<div class="l-media-body">
...
<input class="tile-body-field is-selected">
...
</div>
</div>
</div>
</div>
Like Changing a Tire at 70mph
(One method at a time)
Create a new set of styles.
Some pages use the old styles and some pages use the new styles.
Users have to download two sets of CSS.
Adds complexity before simplifying.
References
More References
ivan@jonas.ninja
@i2nj3s
Ivan Jonas Gomes