Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
A quick guide for writing MAINTAINABLE and SCAL...
Search
Sergio Álvarez
November 19, 2016
Programming
2
420
A quick guide for writing MAINTAINABLE and SCALABLE CSS
Presented @ Codemotion Madrid 2016
Sergio Álvarez
November 19, 2016
Tweet
Share
Other Decks in Programming
See All in Programming
「天気予報があなたに届けられるまで」 - NIFTY Tech Talk #22
niftycorp
PRO
0
110
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
7
1.8k
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
as(型アサーション)を書く前にできること
marokanatani
10
2.8k
CSC509 Lecture 12
javiergs
PRO
0
160
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
260
React CompilerとFine Grained Reactivityと宣言的UIのこれから / The next chapter of declarative UI
ssssota
6
1.7k
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
270
@nifty天気予報:フルリニューアルの挑戦 - NIFTY Tech Talk #22
niftycorp
PRO
0
100
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
120
受け取る人から提供する人になるということ
little_rubyist
0
260
Jakarta EE meets AI
ivargrimstad
0
430
Featured
See All Featured
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Writing Fast Ruby
sferik
627
61k
Music & Morning Musume
bryan
46
6.2k
The Language of Interfaces
destraynor
154
24k
Documentation Writing (for coders)
carmenintech
65
4.4k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
Embracing the Ebb and Flow
colly
84
4.5k
What's new in Ruby 2.0
geeforr
343
31k
Transcript
A QUICK GUIDE FOR WRITING MAINTAINABLE AND SCALABLE CSS
Sergio Álvarez · @sergioalvz_ Software Engineer Breakfast-Driven Development practitioner #CleanCode
#MaintainableCode #CodeForHumans
None
Asturias Hacking
What’s MAINTAINABLE CSS?
What’s SCALABLE CSS?
Any fool can write code that a computer can understand.
Good programmers write code that humans can understand. — MARTIN FOWLER
WHAT’S THE PROBLEM WITH CSS?
It’s global by default
It’s easy to learn, but hard to master
It’s browser dependent
It has a complex specificity system
HOW DOES SPECIFICITY WORK?
button#submit { background-color: red; } button[id="submit"] { background-color: blue; }
What background-color is finally applied to button?
button#submit { background-color: red; } button[id="submit"] { background-color: blue; }
What background-color is finally applied to button?
None
1 #ID 2 Classes & Attributes 3 Elements
1 #ID 2 Classes & Attributes 3 Elements
STRATEGIES FOR DEALING WITH SPECIFICITY
Avoid #selectors. Use nesting wisely. Don’t over qualify.
.nav { /* ... */ } .nav li { /*
... */ } .nav ul { /* ... */ } .nav li ul { /* ... */ } .nav > li { /* ... */ } .nav ul li { /* ... */ } Item 1 Item 2 Item 3 Sub Item 1.1 Sub Item 1.2 Sub Item 1.3
.nav { /* ... */ } .nav-item { /* ...
*/ } .nav-submenu { /* ... */ } .nav-submenu-item { /* ... */ } Item 1 Item 2 Item 3 Sub Item 1.1 Sub Item 1.2 Sub Item 1.3
WRITING MODULAR CSS
Components are bits of User Interface
Encapsulation. Composition. Documentation.
OOCSS. SMACSS. BEM. ITCSS.
OOCSS. SMACSS. BEM. ITCSS.
It’s a full-featured framework for front-end development powered by Yandex
We’ll only talk about its naming conventions
Feo. Fuerte. Formal.
Block Element Modifier
Block .dropdown { /* ... */ } Edit Remove Update
Element .dropdown__item { /* ... */ } .dropdown__icon { /*
... */ } .dropdown__text { /* ... */ } Edit Remove Update
Modifier .dropdown__item--is-selected { /* ... */ } .dropdown__item--is-disabled { /*
... */ } .dropdown__item--has-only-text { /* ... */ } Edit Remove Update
Edit Remove Update .dropdown { /* ... */ } .dropdown__item
{ /* ... */ } .dropdown__item--is-selected { /* ... */ } .dropdown__item--has-only-text { /* ... */ } .dropdown__text { /* ... */ }
Edit Remove Update <div class="dropdown"> <div> <div class="dropdown__item dropdown__item--has-only-text dropdown__item--is-selected">
<span class="dropdown__text"> Update </span> </div> <div class="dropdown__item dropdown__item--has-only-text"> <span class="dropdown__text"> Edit </span> </div> <div class="dropdown__item dropdown__item--has-only-text"> <span class="dropdown__text"> Remove </span> </div> </div> </div>
Edit Remove Update <ul class="dropdown"> <li class="dropdown__item dropdown__item--has-only-text dropdown__item--is-selected"> <p
class="dropdown__text"> Update </p> </li> <li class="dropdown__item dropdown__item--has-only-text"> <p class="dropdown__text"> Edit </p> </li> <li class="dropdown__item dropdown__item--has-only-text"> <p class="dropdown__text"> Remove </p> </li> </ul>
├── base │ ├── font.css │ └── reset.css ├── components
│ ├── avatar.css │ ├── button.css │ └── dropdown.css ├── modules │ ├── home.css │ └── sidebar.css ├── utilities │ ├── mixins │ └── variables.css ├── vendor │ ├── bootstrap.css │ └── datepicker.css └── main.css Best project organization
BEM… improves semantics. avoids inheritance. reduces specificity.
Prefer duplication over the wrong abstraction — SANDI METZ
DECOUPLING CSS FROM EVERYTHING
BEM has already decoupled our CSS from HTML
What about JavaScript?
<button class="button button--is-secondary"> Cancel </button> document.querySelector( '.button--is-secondary' );
<button class=" js-button button button--is-secondary"> Cancel </button> document.querySelector( ' .js-button
' ); JavaScript hooks!
CUSTOM PROPERTIES THE CSS BUILT-IN VARIABLES
None
.button { font-size: 16px; } .page__text { font-size: 16px; }
.dropdown__item { font-size: 16px; }
:root { --base-font-size: 16px; } .button { font-size: var(--base-font-size); }
.page__text { font-size: var(--base-font-size); } .dropdown__item { font-size: var(--base-font-size); }
.button { background-color: #fc3952; } .dropdown__item--is-selected { background-color: #fc3952; }
.link { color: #fc3952; }
:root { --primary-color: #fc3952; } .button { background-color: var(--primary-color); }
.dropdown__item--is-selected { background-color: var(--primary-color); } .link { color: var(--primary-color); }
:root { --primary-color: #fc3952; } .button { background-color: var(--primary-color); }
.dropdown__item--is-selected { background-color: var(--primary-color); } .link { color: var(--primary-color); } This can be dynamically updated!
FLEXBOX A NEW LAYOUT MODEL
None
Declarative. Dynamic. Direction-agnostic.
Vertical centering. Sticky footer. Full-height columns. Holy Grail layout.
.box { align-items: center; display: flex; } .box
.page { display: flex; flex-direction: column; min-height: 100vh; } .header
{ height: 4vh; } .content { flex-grow: 1; } .footer { height: 10vh; } .page .header .content .footer
.page .column .column .column .page { display: flex; min-height: 100vh;
} .column { flex-basis: 33%; }
THE FUTURE CODEMOTION 2017
Web Components. CSS Modules. Grid Layout.
RESOURCES
CSS Specificity: Things you should know (Smashing Magazine) · http://bit.ly/2fQpC2R
Hacks for dealing with specificity (Harry Roberts) · http:// bit.ly/2fqUOVy MindBEMding – getting your head ’round BEM syntax (Harry Roberts) · http://bit.ly/2foR5W4 Code smells in CSS (Harry Roberts) · http://bit.ly/2fQnc41
Battling BEM (Extended Edition): 10 Common Problems And How To
Avoid Them (Smashing Magazine) · http:// bit.ly/2fuvjPH CSS at BBC Sport (Part 1) · http://bit.ly/2eLDHuc CSS for Software Engineers for CSS Developers (Harry Roberts) · http://bit.ly/2fI4g8l MaintainableCSS (Adam Silver) · http://bit.ly/2fsrdcw
CSS Flexbox Toolbox - Learning Guides, Tools & Frameworks (speckyboy)
· http://bit.ly/2fr1Gm5 @LeaVerou · http://bit.ly/2ecWx0X @SaraSoueidan · http://bit.ly/2fqZIBT @csswizardry · http://bit.ly/2f6Mvxx
¡GRACIAS! @sergioalvz_ Saturday, November 19th Codemotion Madrid 2016