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 SCALABLE CSS
Search
Sergio Álvarez
November 19, 2016
Programming
2
410
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
PHPはいつから死んでいるかの調査
chiroruxx
2
410
効率化に挑戦してみたらモバイル開発が少し快適になった話
ryunakayama
0
130
DMMプラットフォームがTiDB Cloudを採用した背景
pospome
9
4.2k
Scalable Customer Journey Orchestration (CJO)
lewuathe
0
400
0→1と1→10の狭間で Javaという技術選定を振り返る/Reflecting on the Decision to Choose Java Between Scaling from 0 to 1 and 1 to 10
jaguar_imo
2
390
雑に思考を整理する技術と効能
konifar
61
30k
Java 22 Overview
kishida
1
190
CREってこういうこと? 体験入社 - 提案資料 - / what-is-cre-trial-employment
shinden
0
250
サイコロで理解する統計的仮説検定の考え方
tatamiya
4
1k
Git Rebase
bkuhlmann
11
1.6k
Goのエラースタックトレースの歴史と今後
sonatard
9
1.7k
ADRを一年運用してみた/adr_after_a_year
hanhan1978
7
2.4k
Featured
See All Featured
Code Reviewing Like a Champion
maltzj
515
39k
Intergalactic Javascript Robots from Outer Space
tanoku
266
26k
10 Git Anti Patterns You Should be Aware of
lemiorhan
649
58k
Bootstrapping a Software Product
garrettdimon
PRO
302
110k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
11
1.5k
Optimising Largest Contentful Paint
csswizardry
12
2.4k
GraphQLとの向き合い方2022年版
quramy
33
12k
Build your cross-platform service in a week with App Engine
jlugia
226
17k
Producing Creativity
orderedlist
PRO
338
39k
In The Pink: A Labor of Love
frogandcode
138
21k
[RailsConf 2023] Rails as a piece of cake
palkan
27
4k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
222
21k
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