SlideShare a Scribd company logo
1 of 67
Download to read offline
Scalable 
CSS 
(That both you and your ‘back-end’ coders can love.) 
! 
about.me/XML #xmlilley
UI DEV HAS GOTTEN COOL !!!?!
“… an anti-language, 
full of dark magic…” 
-my really smart colleague
WHYYYYYY????? 
And why do we care?
I asked them. And they said 
it’s all about … 
The whole ‘Cascading’ thing 
The ‘Specificity’ thing 
(see also: ‘the Cascading thing’) 
Selector Chaos 
(mostly re: Specificity & Cascading) 
Layout 
(old-school Layouts, that is…)
Hmmmmm….
Option 1: 
They must need better 
training about this stuff
Option 2: 
Maybe they’ve got a point.
“If all you’ve got 
is a hammer, 
everything looks 
like a nail.”
(See also: 
“Stockholm 
Syndrome”)
First Principles 
be DRY - ‘Don’t Repeat Yourself’ 
be Maintainable - write for updates & 
debugging 
be Predictable - Don’t keep hacking what’s 
broken (unless you have to) 
Don’t ‘Optimize’ Prematurely
Strategic Rules For CSS 
Name All The Things™ 
Stop Hoarding Classes 
& Rationing Letters 
Be a Lover, not a Fighter 
Automate or Die
Challenge 1: 
Use Layouts That 
Make Sense
The Super-Secret Key to 
Eliminating 99% of Layout Angst 
STOP 
USING 
FLOAT: 
ALREADY !!!!
Instead of this: 
Or this:
Or worse, this (which is unfixable in code): 
#3 #2 #1 
You get this:
Stop Fighting With 
Floats 
!
Love Display: Inline-Block 
Supported Since IE8 !!! 
Does what you expect 
vertical-align: a feature, not a bug :-) 
text-align: gets you left, right, or 
center
Love Display: Inline-Block 
(The whitespace thing *is* a bug. 
Easy, transparent fix: zero-sized 
fonts on the container. 
Use @mixin !)
Display: Inline-Block (Coda) 
Those deceptively-reasonable .clearfix 
classes that don’t need extra HTML? 
They need :after … 
… which requires CSS 2.1 … 
… which has display: inline-block
Bonus Confusing Layout Idol 
To Consider Burning 
Ems 
(Browser scaling works great) 
(In practice, people use a range of 
assistive technologies, and very 
rarely rely on custom stylesheets.)
Challenge 2: 
Make CSS Code 
Maintainable
Where we *think* 
we spend all our time: 
TYPING
Where we *actually* 
spend all our time: 
1. READING 
2. TRYING to understand 
3. RE-reading 
4. MIS-understanding 
5. Revert to: Step 1
“Maintainability” === 
OPTIMIZE FIRST FOR 
THE PROCESSOR 
INSIDE YOUR SKULL
“In God, we trust. 
All others: 
bring evidence.”
OK… Maintainability… How? 
1. AUTOMATE (OR DIE) 
2. “NAME ALL THE THINGS”… CLEARLY 
3. USE ELEMENT SELECTORS ONLY FOR RESETS 
4. STOP OVER-NESTING… OR MAYBE NESTING 
*AT ALL* 
5. AVOID UNNECESSARY COMPLEXITY
Maintainability… How? 
1. AUTOMATE (OR DIE) 
2. “NAME ALL THE THINGS”… CLEARLY 
3. USE ELEMENT SELECTORS ONLY FOR RESETS 
4. STOP OVER-NESTING… OR MAYBE NESTING 
*AT ALL* 
5. AVOID UNNECESSARY COMPLEXITY
AUTOMATION 
Enough with the FUD. It’s easier than you think. 
Just use a (pre-)compiler. Always. (See Yeoman for help.) 
A new age is upon us. 
There is literally not enough time in this session to detail 
all the ways in which a compiler will make your CSS 
better. 
But here’s a few: you’ve heard about ‘variables’ and 
‘mixins’, but not *why* you need them:
AUTOMATION Examples: 
Human-Readable Colors 
Stop working directly with color codes! Use human-readable 
references like $brand-primary-highlight 
or $ugly-greenish-blue. 
Lighten, Darken, Opacify, Transparentize, using a single 
original reference color. Make a whole chart w/: 
color: $myRed 
$redTwo: darken($myRed, 10%) 
$redThree: darken($myRed, 20%)… 
‘Theme’ a design with just a few variables, and @import
AUTOMATION Example: 
Easy Responsive 
#key-component__guide-text { 
@include apply-at-max-size { 
width:78%; 
} 
@include apply-at-med-size { 
width: 50%; 
} 
@include disappear-at-sm-size; 
}
AUTOMATION Example: 
Easy Responsive 
@mixin apply-at-max-size { 
@media (min-width: $screen-md-min + 1) { 
@content; 
}} 
@mixin apply-at-med-max-size { 
@media (min-width: $screen-sm-min) { 
@content; 
}} 
@mixin disappear-at-sm-size { 
@include apply-at-sm-size { 
display: none; 
}}
AUTOMATION Example: 
Easy Responsive 
$screen-xs-min: 320px; 
$screen-sm-min: 550px; 
$screen-md-min: 768px; 
…etc.
MOAR AUTOMATION 
Browser prefixes suck. Use either @mixin’s… or Grunt! 
Easy theming: take theme-specific variables & @mixin’s, 
then @import your default rules, and voilà!
Maintainability… How? 
1. AUTOMATE (OR DIE) 
2. “NAME ALL THE THINGS”… CLEARLY 
3. USE ELEMENT SELECTORS ONLY FOR RESETS 
4. STOP OVER-NESTING… OR MAYBE NESTING 
*AT ALL* 
5. AVOID UNNECESSARY COMPLEXITY
Who was it who told us that this: 
#nav-list li a { 
… stuff… 
} 
… was a good idea? Instead of this: 
.nav-list-link { 
… same stuff … 
} 
… or better… this: 
.main-header__nav-list__link { 
… same stuff … 
}
Stop Rationing Classes! 
It’s as if we were afraid that classes were radioactive: 
too many, too close together, and they’d go super-critical 
Or as if they were contaminating our pristine HTML 
We also like to feel clever. (Be afraid of that instinct.) 
Classes are as efficient as it gets, speed-wise.
Use Clear, Descriptive Classes 
It shouldn’t be necessary to hunt for things. Class names 
should tell us where to expect to find things. 
Sometimes, the hardest thing about using good names is 
just inventing ones that make sense. 
Use any system you like (BEM, OOCS, Suit), which is both 
highly descriptive, and which helps you design good class 
names. 
(Remember they’re helpful patterns, not religious faiths.)
A Note on Namespacing 
As a way of collecting a ‘module’ of related content or 
functionality, namespacing is super-cool. 
But it quickly goes fractal with automation. 
Consider whether using detailed, descriptive class 
names gets you what you were probably really after: 
the avoidance of collisions. 
(See the section on over-nesting.)
Maintainability… How? 
1. AUTOMATE (OR DIE) 
2. “NAME ALL THE THINGS”… CLEARLY 
3. USE ELEMENT SELECTORS ONLY FOR RESETS 
4. STOP OVER-NESTING… OR MAYBE NESTING 
*AT ALL* 
5. AVOID UNNECESSARY COMPLEXITY
About That Whole 
‘Cascade’ Thing 
No matter how much we explain it, the ‘back-end’ folks 
will never understand why we would allow five different 
user-created style declarations to apply to a single 
element 
They’re right: it’s not maintainable. 
The “browser is designed that way” isn’t a good enough 
reason. 
We *can* do that, but we *shouldn’t*.
About That Whole 
‘Cascade’ Thing 
Element selectors are the number one reason we end up 
fighting the cascade, and hoping that the Specificity 
algorithm is on our side today. 
You don’t need them. 
One good use: resets and global styles, like: 
a { 
text-decoration: none; 
color: red; 
}
About That Whole 
‘Cascade’ Thing 
Use of !important is always a “code-smell” 
Maybe use @extend or @include to create a narrower 
class, rather than fight the existing one 
“Be a lover, not a fighter”
Maintainability… How? 
1. AUTOMATE (OR DIE) 
2. “NAME ALL THE THINGS”… CLEARLY 
3. USE ELEMENT SELECTORS ONLY FOR RESETS 
4. STOP OVER-NESTING… OR MAYBE NESTING 
*AT ALL* 
5. AVOID UNNECESSARY COMPLEXITY
Descendant Selectors: 
The Problems 
General use of descendant selectors is anathema to real 
specificity, and guarantees conflicts and overrides: 
form label input 
#login-form input .name-field 
! 
Descendant Selectors lock your content into a single 
context, making them hard to re-use, or re-locate. 
Plus, you can’t relocate other code into yours: ever try to 
add a JQuery date-picker into code that’s styled with 
descendant selectors?
Pre-Compilers Gone Wrong 
body { 
font-size: 12px; 
#main-content: { 
width: 400px; 
height: 200px; 
! 
.left-nav { 
width: 200px; 
margin: 0 0 0 50px; 
! 
ul { 
li { 
display: block; 
list-style-type: none; 
… ∞ … 
}}}}} 
body #main .left-nav ul li … 
5 selectors and counting, 
where 1 would do it.
Descendant Selectors: 
Performance 
The most important thing you can know about selector 
optimization is this: they’re evaluated right-to-left. 
The most important piece of any selector is thus the last 
piece, not the first. Which is pretty much the opposite of 
what we usually do with descendant selectors: 
#very-specific-id .semi-specific-class li a 
The fact that we *can* nest and qualify selectors… 
doesn’t mean that we *should*.
Nesting Without Stacking 
A (pre-)compiler w/ BEM-like syntax gives us readability of 
related styles, without descendant-selector headaches: 
.main-content: { 
&__left-nav { 
&__item 
}}} 
compiles to: 
.main-content {} 
.main-content__left-nav {} 
.main-content__left-nav__item {} 
(If you don’t like those syntaxes, at least just use indentation 
instead of nesting.)
Nesting & Stacking 
Rule of thumb: nest selectors and stack classes when 
you *have to*, not just when you *can*. 
Rule of thumb: nest and stack your @mixin’s and 
@extend’s, not your selectors. 
@mixin body-text { 
color: green; 
font: { 
size: 14px; 
family: Arial; 
} } 
@mixin nav-list__item { 
line-spacing: 1; 
list-style-type: none; 
display: inline-block; 
vertical-align: top; 
} 
.header__nav-list__item { 
@include body-text; 
@include nav-list__item; 
padding: 5px; 
}
Nesting & Stacking 
Relying on @mixin and @extend with descriptive 
classes, rather than on stacked, generic classes means 
that refactoring is easy, and overrides (if necessary) 
happen where you can see them, and plan them: in your 
code. 
If it comes time to refactor for performance, your most-used 
mixins can easily convert to standalone classes. 
Going the other direction… not so much.
Stacking Classes: Pros & Cons 
Stacking (<div class=“class1 class2”) is great if you can’t 
re-write your classes (ie. Bootstrap, components) 
Stacking is declarative, at least in your HTML 
It’s not at all declarative back in the CSS, forcing us to use 
the Dev Tools’ Style Inspector to see what happens 
It puts you into ‘fighting’ mode: overriding the conflicts 
For Optimization: use tactically, not strategically
Stacking Classes: Pros & Cons 
A Thought: how many of the reasons why we stack our 
classes are because we didn’t have pre-compilers back 
when we started doing it?
Maintainability… How? 
1. AUTOMATE (OR DIE) 
2. “NAME ALL THE THINGS”… CLEARLY 
3. USE ELEMENT SELECTORS ONLY FOR RESETS 
4. STOP OVER-NESTING… OR MAYBE NESTING 
*AT ALL* 
5. AVOID UNNECESSARY COMPLEXITY
Don’t Fight Your Elements 
Do you really know if <section>, <article>, etc. do 
anything for you? (hint: they don’t) Use them only if 
they’re helpful in some human-readable way. 
Don’t feel guilty! The era of semantic *elements* for 
machine-readability is largely over, in favor of semantic 
*attributes*. See: WAI-ARIA, microformats
Don’t Fight Your Elements 
Use the most flexible elements available; ones you can 
rearrange and refactor without breaking them. 
Just because some content is vaguely ‘tabular’ in nature 
doesn’t mean you *have* to use a <table>. You can’t scroll 
the <tbody>, and styling is painful. So, don’t do it. 
Just because somebody once said that <ul> is more 
‘semantic’ than <div> for navigation items doesn’t mean it’s 
still worth fighting with it (particularly now that we have 
<nav>) Does it *look like* a list? No? Then don’t.
Pseudo-Helpful 
When you don’t control a template, and can’t assign 
classes, pseudo-selectors are great. Otherwise, maybe not. 
Expensive for what you get. 
Are there other options? Can Javascript help? For example, 
would using Angular’s $first and $last maybe work *at 
least* as well as :first-child/:last-child? 
(Remember: a new age is upon us.)
THANKS! 
(Now go teach those ‘back-end’ folks some tricks.) 
! 
about.me/XML #xmlilley

More Related Content

What's hot

REACT.JS : Rethinking UI Development Using JavaScript
REACT.JS : Rethinking UI Development Using JavaScriptREACT.JS : Rethinking UI Development Using JavaScript
REACT.JS : Rethinking UI Development Using JavaScriptDeepu S Nath
 
Building AngularJS Custom Directives
Building AngularJS Custom DirectivesBuilding AngularJS Custom Directives
Building AngularJS Custom DirectivesDan Wahlin
 
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...FalafelSoftware
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.jsBert Wijnants
 
jQuery Performance Tips and Tricks (2011)
jQuery Performance Tips and Tricks (2011)jQuery Performance Tips and Tricks (2011)
jQuery Performance Tips and Tricks (2011)Addy Osmani
 
Jquery tutorial-beginners
Jquery tutorial-beginnersJquery tutorial-beginners
Jquery tutorial-beginnersIsfand yar Khan
 
Custom AngularJS Directives
Custom AngularJS DirectivesCustom AngularJS Directives
Custom AngularJS Directivesyprodev
 
jQuery basics
jQuery basicsjQuery basics
jQuery basicsKamal S
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsEPAM Systems
 
An Introduction to ReactJS
An Introduction to ReactJSAn Introduction to ReactJS
An Introduction to ReactJSAll Things Open
 
Apache Wicket Web Framework
Apache Wicket Web FrameworkApache Wicket Web Framework
Apache Wicket Web FrameworkLuther Baker
 
jQuery Presentation
jQuery PresentationjQuery Presentation
jQuery PresentationRod Johnson
 
Difference between java script and jquery
Difference between java script and jqueryDifference between java script and jquery
Difference between java script and jqueryUmar Ali
 

What's hot (20)

ReactJS
ReactJSReactJS
ReactJS
 
REACT.JS : Rethinking UI Development Using JavaScript
REACT.JS : Rethinking UI Development Using JavaScriptREACT.JS : Rethinking UI Development Using JavaScript
REACT.JS : Rethinking UI Development Using JavaScript
 
Building AngularJS Custom Directives
Building AngularJS Custom DirectivesBuilding AngularJS Custom Directives
Building AngularJS Custom Directives
 
jQuery basics
jQuery basicsjQuery basics
jQuery basics
 
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.js
 
jQuery Performance Tips and Tricks (2011)
jQuery Performance Tips and Tricks (2011)jQuery Performance Tips and Tricks (2011)
jQuery Performance Tips and Tricks (2011)
 
Jquery tutorial-beginners
Jquery tutorial-beginnersJquery tutorial-beginners
Jquery tutorial-beginners
 
Knockoutjs
KnockoutjsKnockoutjs
Knockoutjs
 
jQuery
jQueryjQuery
jQuery
 
Custom AngularJS Directives
Custom AngularJS DirectivesCustom AngularJS Directives
Custom AngularJS Directives
 
Web&java. jsp
Web&java. jspWeb&java. jsp
Web&java. jsp
 
jQuery basics
jQuery basicsjQuery basics
jQuery basics
 
React
React React
React
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
 
jQuery Best Practice
jQuery Best Practice jQuery Best Practice
jQuery Best Practice
 
An Introduction to ReactJS
An Introduction to ReactJSAn Introduction to ReactJS
An Introduction to ReactJS
 
Apache Wicket Web Framework
Apache Wicket Web FrameworkApache Wicket Web Framework
Apache Wicket Web Framework
 
jQuery Presentation
jQuery PresentationjQuery Presentation
jQuery Presentation
 
Difference between java script and jquery
Difference between java script and jqueryDifference between java script and jquery
Difference between java script and jquery
 

Similar to Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

CSS Methodology
CSS MethodologyCSS Methodology
CSS MethodologyZohar Arad
 
The Cascade, Grids, Headings, and Selectors from an OOCSS Perspective, Ajax ...
The Cascade, Grids, Headings, and Selectors from an OOCSS Perspective,  Ajax ...The Cascade, Grids, Headings, and Selectors from an OOCSS Perspective,  Ajax ...
The Cascade, Grids, Headings, and Selectors from an OOCSS Perspective, Ajax ...Nicole Sullivan
 
Creative Web 02 - HTML & CSS Basics
Creative Web 02 - HTML & CSS BasicsCreative Web 02 - HTML & CSS Basics
Creative Web 02 - HTML & CSS BasicsLukas Oppermann
 
CSS pattern libraries
CSS pattern librariesCSS pattern libraries
CSS pattern librariesRuss Weakley
 
Prototype Utility Methods(1)
Prototype Utility Methods(1)Prototype Utility Methods(1)
Prototype Utility Methods(1)mussawir20
 
CSS - OOCSS, SMACSS and more
CSS - OOCSS, SMACSS and moreCSS - OOCSS, SMACSS and more
CSS - OOCSS, SMACSS and moreRuss Weakley
 
CSS Frameworks
CSS FrameworksCSS Frameworks
CSS FrameworksMike Crabb
 
Front-End Methodologies
Front-End MethodologiesFront-End Methodologies
Front-End MethodologiesArash Manteghi
 
Sass Essentials at Mobile Camp LA
Sass Essentials at Mobile Camp LASass Essentials at Mobile Camp LA
Sass Essentials at Mobile Camp LAJake Johnson
 
Structuring your CSS for maintainability: rules and guile lines to write CSS
Structuring your CSS for maintainability: rules and guile lines to write CSSStructuring your CSS for maintainability: rules and guile lines to write CSS
Structuring your CSS for maintainability: rules and guile lines to write CSSSanjoy Kr. Paul
 
iOS best practices
iOS best practicesiOS best practices
iOS best practicesMaxim Vialyx
 
On Coding Guidelines
On Coding GuidelinesOn Coding Guidelines
On Coding GuidelinesDIlawar Singh
 
Dependency Injection for PHP
Dependency Injection for PHPDependency Injection for PHP
Dependency Injection for PHPmtoppa
 

Similar to Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014 (20)

CSS Methodology
CSS MethodologyCSS Methodology
CSS Methodology
 
Pfnp slides
Pfnp slidesPfnp slides
Pfnp slides
 
The Cascade, Grids, Headings, and Selectors from an OOCSS Perspective, Ajax ...
The Cascade, Grids, Headings, and Selectors from an OOCSS Perspective,  Ajax ...The Cascade, Grids, Headings, and Selectors from an OOCSS Perspective,  Ajax ...
The Cascade, Grids, Headings, and Selectors from an OOCSS Perspective, Ajax ...
 
Creative Web 02 - HTML & CSS Basics
Creative Web 02 - HTML & CSS BasicsCreative Web 02 - HTML & CSS Basics
Creative Web 02 - HTML & CSS Basics
 
CSS_tutorial_2
CSS_tutorial_2CSS_tutorial_2
CSS_tutorial_2
 
CSS_tutorial_2
CSS_tutorial_2CSS_tutorial_2
CSS_tutorial_2
 
Aem best practices
Aem best practicesAem best practices
Aem best practices
 
CSS 101
CSS 101CSS 101
CSS 101
 
CSS pattern libraries
CSS pattern librariesCSS pattern libraries
CSS pattern libraries
 
Prototype Utility Methods(1)
Prototype Utility Methods(1)Prototype Utility Methods(1)
Prototype Utility Methods(1)
 
Dominate The Theme Layer
Dominate The Theme LayerDominate The Theme Layer
Dominate The Theme Layer
 
CSS - OOCSS, SMACSS and more
CSS - OOCSS, SMACSS and moreCSS - OOCSS, SMACSS and more
CSS - OOCSS, SMACSS and more
 
CSS Frameworks
CSS FrameworksCSS Frameworks
CSS Frameworks
 
Front-End Methodologies
Front-End MethodologiesFront-End Methodologies
Front-End Methodologies
 
Sass Essentials at Mobile Camp LA
Sass Essentials at Mobile Camp LASass Essentials at Mobile Camp LA
Sass Essentials at Mobile Camp LA
 
Structuring your CSS for maintainability: rules and guile lines to write CSS
Structuring your CSS for maintainability: rules and guile lines to write CSSStructuring your CSS for maintainability: rules and guile lines to write CSS
Structuring your CSS for maintainability: rules and guile lines to write CSS
 
iOS best practices
iOS best practicesiOS best practices
iOS best practices
 
On Coding Guidelines
On Coding GuidelinesOn Coding Guidelines
On Coding Guidelines
 
Dependency Injection for PHP
Dependency Injection for PHPDependency Injection for PHP
Dependency Injection for PHP
 
Fewd week6 slides
Fewd week6 slidesFewd week6 slides
Fewd week6 slides
 

Recently uploaded

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 

Recently uploaded (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 

Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

  • 1. Scalable CSS (That both you and your ‘back-end’ coders can love.) ! about.me/XML #xmlilley
  • 2. UI DEV HAS GOTTEN COOL !!!?!
  • 3.
  • 4.
  • 5.
  • 6.
  • 7. “… an anti-language, full of dark magic…” -my really smart colleague
  • 8.
  • 9. WHYYYYYY????? And why do we care?
  • 10.
  • 11.
  • 12.
  • 13. I asked them. And they said it’s all about … The whole ‘Cascading’ thing The ‘Specificity’ thing (see also: ‘the Cascading thing’) Selector Chaos (mostly re: Specificity & Cascading) Layout (old-school Layouts, that is…)
  • 15. Option 1: They must need better training about this stuff
  • 16.
  • 17. Option 2: Maybe they’ve got a point.
  • 18. “If all you’ve got is a hammer, everything looks like a nail.”
  • 19. (See also: “Stockholm Syndrome”)
  • 20. First Principles be DRY - ‘Don’t Repeat Yourself’ be Maintainable - write for updates & debugging be Predictable - Don’t keep hacking what’s broken (unless you have to) Don’t ‘Optimize’ Prematurely
  • 21. Strategic Rules For CSS Name All The Things™ Stop Hoarding Classes & Rationing Letters Be a Lover, not a Fighter Automate or Die
  • 22. Challenge 1: Use Layouts That Make Sense
  • 23. The Super-Secret Key to Eliminating 99% of Layout Angst STOP USING FLOAT: ALREADY !!!!
  • 24.
  • 25. Instead of this: Or this:
  • 26. Or worse, this (which is unfixable in code): #3 #2 #1 You get this:
  • 27. Stop Fighting With Floats !
  • 28. Love Display: Inline-Block Supported Since IE8 !!! Does what you expect vertical-align: a feature, not a bug :-) text-align: gets you left, right, or center
  • 29. Love Display: Inline-Block (The whitespace thing *is* a bug. Easy, transparent fix: zero-sized fonts on the container. Use @mixin !)
  • 30. Display: Inline-Block (Coda) Those deceptively-reasonable .clearfix classes that don’t need extra HTML? They need :after … … which requires CSS 2.1 … … which has display: inline-block
  • 31. Bonus Confusing Layout Idol To Consider Burning Ems (Browser scaling works great) (In practice, people use a range of assistive technologies, and very rarely rely on custom stylesheets.)
  • 32. Challenge 2: Make CSS Code Maintainable
  • 33. Where we *think* we spend all our time: TYPING
  • 34. Where we *actually* spend all our time: 1. READING 2. TRYING to understand 3. RE-reading 4. MIS-understanding 5. Revert to: Step 1
  • 35. “Maintainability” === OPTIMIZE FIRST FOR THE PROCESSOR INSIDE YOUR SKULL
  • 36. “In God, we trust. All others: bring evidence.”
  • 37. OK… Maintainability… How? 1. AUTOMATE (OR DIE) 2. “NAME ALL THE THINGS”… CLEARLY 3. USE ELEMENT SELECTORS ONLY FOR RESETS 4. STOP OVER-NESTING… OR MAYBE NESTING *AT ALL* 5. AVOID UNNECESSARY COMPLEXITY
  • 38. Maintainability… How? 1. AUTOMATE (OR DIE) 2. “NAME ALL THE THINGS”… CLEARLY 3. USE ELEMENT SELECTORS ONLY FOR RESETS 4. STOP OVER-NESTING… OR MAYBE NESTING *AT ALL* 5. AVOID UNNECESSARY COMPLEXITY
  • 39. AUTOMATION Enough with the FUD. It’s easier than you think. Just use a (pre-)compiler. Always. (See Yeoman for help.) A new age is upon us. There is literally not enough time in this session to detail all the ways in which a compiler will make your CSS better. But here’s a few: you’ve heard about ‘variables’ and ‘mixins’, but not *why* you need them:
  • 40. AUTOMATION Examples: Human-Readable Colors Stop working directly with color codes! Use human-readable references like $brand-primary-highlight or $ugly-greenish-blue. Lighten, Darken, Opacify, Transparentize, using a single original reference color. Make a whole chart w/: color: $myRed $redTwo: darken($myRed, 10%) $redThree: darken($myRed, 20%)… ‘Theme’ a design with just a few variables, and @import
  • 41. AUTOMATION Example: Easy Responsive #key-component__guide-text { @include apply-at-max-size { width:78%; } @include apply-at-med-size { width: 50%; } @include disappear-at-sm-size; }
  • 42. AUTOMATION Example: Easy Responsive @mixin apply-at-max-size { @media (min-width: $screen-md-min + 1) { @content; }} @mixin apply-at-med-max-size { @media (min-width: $screen-sm-min) { @content; }} @mixin disappear-at-sm-size { @include apply-at-sm-size { display: none; }}
  • 43. AUTOMATION Example: Easy Responsive $screen-xs-min: 320px; $screen-sm-min: 550px; $screen-md-min: 768px; …etc.
  • 44. MOAR AUTOMATION Browser prefixes suck. Use either @mixin’s… or Grunt! Easy theming: take theme-specific variables & @mixin’s, then @import your default rules, and voilà!
  • 45. Maintainability… How? 1. AUTOMATE (OR DIE) 2. “NAME ALL THE THINGS”… CLEARLY 3. USE ELEMENT SELECTORS ONLY FOR RESETS 4. STOP OVER-NESTING… OR MAYBE NESTING *AT ALL* 5. AVOID UNNECESSARY COMPLEXITY
  • 46. Who was it who told us that this: #nav-list li a { … stuff… } … was a good idea? Instead of this: .nav-list-link { … same stuff … } … or better… this: .main-header__nav-list__link { … same stuff … }
  • 47. Stop Rationing Classes! It’s as if we were afraid that classes were radioactive: too many, too close together, and they’d go super-critical Or as if they were contaminating our pristine HTML We also like to feel clever. (Be afraid of that instinct.) Classes are as efficient as it gets, speed-wise.
  • 48. Use Clear, Descriptive Classes It shouldn’t be necessary to hunt for things. Class names should tell us where to expect to find things. Sometimes, the hardest thing about using good names is just inventing ones that make sense. Use any system you like (BEM, OOCS, Suit), which is both highly descriptive, and which helps you design good class names. (Remember they’re helpful patterns, not religious faiths.)
  • 49. A Note on Namespacing As a way of collecting a ‘module’ of related content or functionality, namespacing is super-cool. But it quickly goes fractal with automation. Consider whether using detailed, descriptive class names gets you what you were probably really after: the avoidance of collisions. (See the section on over-nesting.)
  • 50. Maintainability… How? 1. AUTOMATE (OR DIE) 2. “NAME ALL THE THINGS”… CLEARLY 3. USE ELEMENT SELECTORS ONLY FOR RESETS 4. STOP OVER-NESTING… OR MAYBE NESTING *AT ALL* 5. AVOID UNNECESSARY COMPLEXITY
  • 51. About That Whole ‘Cascade’ Thing No matter how much we explain it, the ‘back-end’ folks will never understand why we would allow five different user-created style declarations to apply to a single element They’re right: it’s not maintainable. The “browser is designed that way” isn’t a good enough reason. We *can* do that, but we *shouldn’t*.
  • 52. About That Whole ‘Cascade’ Thing Element selectors are the number one reason we end up fighting the cascade, and hoping that the Specificity algorithm is on our side today. You don’t need them. One good use: resets and global styles, like: a { text-decoration: none; color: red; }
  • 53. About That Whole ‘Cascade’ Thing Use of !important is always a “code-smell” Maybe use @extend or @include to create a narrower class, rather than fight the existing one “Be a lover, not a fighter”
  • 54. Maintainability… How? 1. AUTOMATE (OR DIE) 2. “NAME ALL THE THINGS”… CLEARLY 3. USE ELEMENT SELECTORS ONLY FOR RESETS 4. STOP OVER-NESTING… OR MAYBE NESTING *AT ALL* 5. AVOID UNNECESSARY COMPLEXITY
  • 55. Descendant Selectors: The Problems General use of descendant selectors is anathema to real specificity, and guarantees conflicts and overrides: form label input #login-form input .name-field ! Descendant Selectors lock your content into a single context, making them hard to re-use, or re-locate. Plus, you can’t relocate other code into yours: ever try to add a JQuery date-picker into code that’s styled with descendant selectors?
  • 56. Pre-Compilers Gone Wrong body { font-size: 12px; #main-content: { width: 400px; height: 200px; ! .left-nav { width: 200px; margin: 0 0 0 50px; ! ul { li { display: block; list-style-type: none; … ∞ … }}}}} body #main .left-nav ul li … 5 selectors and counting, where 1 would do it.
  • 57. Descendant Selectors: Performance The most important thing you can know about selector optimization is this: they’re evaluated right-to-left. The most important piece of any selector is thus the last piece, not the first. Which is pretty much the opposite of what we usually do with descendant selectors: #very-specific-id .semi-specific-class li a The fact that we *can* nest and qualify selectors… doesn’t mean that we *should*.
  • 58. Nesting Without Stacking A (pre-)compiler w/ BEM-like syntax gives us readability of related styles, without descendant-selector headaches: .main-content: { &__left-nav { &__item }}} compiles to: .main-content {} .main-content__left-nav {} .main-content__left-nav__item {} (If you don’t like those syntaxes, at least just use indentation instead of nesting.)
  • 59. Nesting & Stacking Rule of thumb: nest selectors and stack classes when you *have to*, not just when you *can*. Rule of thumb: nest and stack your @mixin’s and @extend’s, not your selectors. @mixin body-text { color: green; font: { size: 14px; family: Arial; } } @mixin nav-list__item { line-spacing: 1; list-style-type: none; display: inline-block; vertical-align: top; } .header__nav-list__item { @include body-text; @include nav-list__item; padding: 5px; }
  • 60. Nesting & Stacking Relying on @mixin and @extend with descriptive classes, rather than on stacked, generic classes means that refactoring is easy, and overrides (if necessary) happen where you can see them, and plan them: in your code. If it comes time to refactor for performance, your most-used mixins can easily convert to standalone classes. Going the other direction… not so much.
  • 61. Stacking Classes: Pros & Cons Stacking (<div class=“class1 class2”) is great if you can’t re-write your classes (ie. Bootstrap, components) Stacking is declarative, at least in your HTML It’s not at all declarative back in the CSS, forcing us to use the Dev Tools’ Style Inspector to see what happens It puts you into ‘fighting’ mode: overriding the conflicts For Optimization: use tactically, not strategically
  • 62. Stacking Classes: Pros & Cons A Thought: how many of the reasons why we stack our classes are because we didn’t have pre-compilers back when we started doing it?
  • 63. Maintainability… How? 1. AUTOMATE (OR DIE) 2. “NAME ALL THE THINGS”… CLEARLY 3. USE ELEMENT SELECTORS ONLY FOR RESETS 4. STOP OVER-NESTING… OR MAYBE NESTING *AT ALL* 5. AVOID UNNECESSARY COMPLEXITY
  • 64. Don’t Fight Your Elements Do you really know if <section>, <article>, etc. do anything for you? (hint: they don’t) Use them only if they’re helpful in some human-readable way. Don’t feel guilty! The era of semantic *elements* for machine-readability is largely over, in favor of semantic *attributes*. See: WAI-ARIA, microformats
  • 65. Don’t Fight Your Elements Use the most flexible elements available; ones you can rearrange and refactor without breaking them. Just because some content is vaguely ‘tabular’ in nature doesn’t mean you *have* to use a <table>. You can’t scroll the <tbody>, and styling is painful. So, don’t do it. Just because somebody once said that <ul> is more ‘semantic’ than <div> for navigation items doesn’t mean it’s still worth fighting with it (particularly now that we have <nav>) Does it *look like* a list? No? Then don’t.
  • 66. Pseudo-Helpful When you don’t control a template, and can’t assign classes, pseudo-selectors are great. Otherwise, maybe not. Expensive for what you get. Are there other options? Can Javascript help? For example, would using Angular’s $first and $last maybe work *at least* as well as :first-child/:last-child? (Remember: a new age is upon us.)
  • 67. THANKS! (Now go teach those ‘back-end’ folks some tricks.) ! about.me/XML #xmlilley