SlideShare une entreprise Scribd logo
1  sur  31
Télécharger pour lire hors ligne
Sane CSS A modern approach to CSS
Lee Cheneler
What We’ll Tackle
• CSS Specificity
• Quick look at how CSS specificity works
• ITCSS
• A sane architecture for our CSS
• BEM
• CSS naming convention
• Top tips
• These will make your life a lot easier
CSS Specificity (╯°□°)╯︵ ┻━┻
A quick look at how CSS specificity works
Selectors
Specificity
Thanks CSS Tricks 
Gotcha
• Last in file wins
!important
• !important trumps all
• Even beats out styles set out in an elements style attribute
ITCSS
A sane architecture for our CSS
Who, What?
• Proposed by Harry Roberts, founder of CSS Wizardry and author of
inuit.css
•Inverted Triangle CSS
• Aims to bring order to the CSS within your project
• Outlines several layers for different parts of your CSS to sit
• Helps solve common CSS problems:
• Naturally reduces the likelihood of global namespace clashes (class names for instance)
• CSS specificity is less of an issue due to the layered approach
The Inverted Triangle
Settings
Tools
Resets
Elements
Objects
Components
Utilities
Settings
• Define variables to be consumed by lower layers
• Omitted if not using a pre-processor such as SASS
Tools
• Creates reusable mixins and functions to be consumed by lower
layers
• Omitted if not using a pre-processor like SASS
NO CSS IS GENERATED BY THE PREVIOUS LAYERS
… EVER
Resets
• Normalizing CSS goes here
• Make all browsers render consistently
• This essentially gives you a blank canvas that CSS in the lower layers can
build on top of
• Highly recommend normalize.css (available on npm, source on GitHub)
Elements
• Styling for HTML elements across your whole site
• Only uses CSS element selectors
• h1, p, nav, etc.
Objects
• Only defines layout and positioning, no visual styling
• This increases the reusability of classes in this layer
• Only use class based selectors
• .grid, .flag, etc.
• Common to start name spacing classes from this layer down
• .o-grid, .o-flag, etc.
Components
• First layer to introduce visual styling
• Only use class based selectors
• .c-card, .c-modal, etc.
Components Continued
• Best not to mix objects and components on the same elements in the
your HTML
Utilities
• Utility classes are not evil, but you will find yourself using them less
when applying ITCSS and BEM to a project (I did at least)
• Only use class based selectors
• .u-text-align-center, .u-1/2, etc.
• Ideally !important is contained within this layer
Compilation
• Order is important whether using ITCSS or not
• Remember CSS specificity from earlier?
• In ITCSS everything in a layer is available to all layers beneath it
globally
• Crucial for reusable mixins and variables without declaring imports
everywhere…
• Generally speaking, compile your CSS into a file top down following
the triangle
• Natural ordering to reduce specificity issues
• The lower your layer the more specific the CSS is
BEM
A descriptive CSS naming convention
Block, Element, Modifier
• Goal is to clearly describe the relationship between CSS and HTML
• Used to describe components/objects
• Three types of class selector
• Block
• Element
• Modifier
Example
Block
• Top level abstraction
• Applied to the outer most element of the component/object
• Examples
• button
• grid
• list
Element
• Children of Blocks
• Applied to HTML elements within block elements
• No bleeding
• Examples
• grid__item
• list__item
• button__icon
Modifiers
• Modifiers applied to block elements
• Used to apply themes or styles to the entire component
• Examples
• button—lg
• grid—trench
• button--primary
Example in HTML
Top Tips
These will make your life a lot easier
Super useful tools
• AutoPrefixer
• Applies prefixed properties based on the caniuse.com database
• StyleLint
• Provides linting for CSS/SASS/LESS
• CleanCSS
• This minifies your CSS
CSS Property Order
• Useful to order your CSS properties in a set groups
• I find this flow to be easy to remember and very useful
• Position
• Display & Box Model
• Color
• Text
• Other
• I don’t alphabetise or anything like that, that’s too granular and
nightmare to maintain… it’s all about efficiency
References
• Xcode.co https://www.xfive.co/blog/itcss-scalable-maintainable-css-architecture/
• CSS Tricks https://css-tricks.com/bem-101/ https://css-tricks.com/specifics-on-css-specificity/
• AutoPrefixer https://github.com/postcss/autoprefixer
• Normalize.css https://github.com/necolas/normalize.css/
• Code examples from easy-css https://github.com/LeeCheneler/easy-css

Contenu connexe

Tendances

Atlanta Drupal User Group (ADUG)
Atlanta Drupal User Group (ADUG) Atlanta Drupal User Group (ADUG)
Atlanta Drupal User Group (ADUG)
Mediacurrent
 
Html css crash course may 11th, atlanta
Html css crash course may 11th, atlantaHtml css crash course may 11th, atlanta
Html css crash course may 11th, atlanta
Thinkful
 

Tendances (20)

Less presentation
Less presentationLess presentation
Less presentation
 
Web 102 INtro to CSS
Web 102  INtro to CSSWeb 102  INtro to CSS
Web 102 INtro to CSS
 
CSS Grid
CSS GridCSS Grid
CSS Grid
 
Code & Design your first website 4/18
Code & Design your first website 4/18Code & Design your first website 4/18
Code & Design your first website 4/18
 
Sass - Getting Started with Sass!
Sass - Getting Started with Sass!Sass - Getting Started with Sass!
Sass - Getting Started with Sass!
 
Atlanta Drupal User Group (ADUG)
Atlanta Drupal User Group (ADUG) Atlanta Drupal User Group (ADUG)
Atlanta Drupal User Group (ADUG)
 
Html css crash course may 11th, atlanta
Html css crash course may 11th, atlantaHtml css crash course may 11th, atlanta
Html css crash course may 11th, atlanta
 
CSS
CSSCSS
CSS
 
CSS: a rapidly changing world
CSS: a rapidly changing worldCSS: a rapidly changing world
CSS: a rapidly changing world
 
Design todevelop
Design todevelopDesign todevelop
Design todevelop
 
Ppt ch11
Ppt ch11Ppt ch11
Ppt ch11
 
The web standards gentleman: a matter of (evolving) standards)
The web standards gentleman: a matter of (evolving) standards)The web standards gentleman: a matter of (evolving) standards)
The web standards gentleman: a matter of (evolving) standards)
 
Run around Chrome Inspector
Run around Chrome Inspector Run around Chrome Inspector
Run around Chrome Inspector
 
Ashish
AshishAshish
Ashish
 
LESS CSS
LESS CSSLESS CSS
LESS CSS
 
Wrangling the CSS Beast with Sass
Wrangling the CSS Beast  with SassWrangling the CSS Beast  with Sass
Wrangling the CSS Beast with Sass
 
Cascading style sheet an introduction
Cascading style sheet   an introductionCascading style sheet   an introduction
Cascading style sheet an introduction
 
Put A Map On It! Enhanced geolocation in WordPress with Geo Mashup
Put A Map On It! Enhanced geolocation in WordPress with Geo MashupPut A Map On It! Enhanced geolocation in WordPress with Geo Mashup
Put A Map On It! Enhanced geolocation in WordPress with Geo Mashup
 
Sass
SassSass
Sass
 
CSS 201
CSS 201CSS 201
CSS 201
 

Similaire à Sane CSS - A modern approach to CSS

Web design-workflow
Web design-workflowWeb design-workflow
Web design-workflow
Peter Kaizer
 
Cssbestpracticesjstyleguidejandtips 150830184202-lva1-app6892
Cssbestpracticesjstyleguidejandtips 150830184202-lva1-app6892Cssbestpracticesjstyleguidejandtips 150830184202-lva1-app6892
Cssbestpracticesjstyleguidejandtips 150830184202-lva1-app6892
Deepak Sharma
 

Similaire à Sane CSS - A modern approach to CSS (20)

Web Development - Lecture 5
Web Development - Lecture 5Web Development - Lecture 5
Web Development - Lecture 5
 
Getting Started With CSS
Getting Started With CSSGetting Started With CSS
Getting Started With CSS
 
CSS.pptx
CSS.pptxCSS.pptx
CSS.pptx
 
Css
CssCss
Css
 
basic programming language AND HTML CSS JAVApdf
basic programming language AND HTML CSS JAVApdfbasic programming language AND HTML CSS JAVApdf
basic programming language AND HTML CSS JAVApdf
 
Web design-workflow
Web design-workflowWeb design-workflow
Web design-workflow
 
CSS Introduction
CSS Introduction CSS Introduction
CSS Introduction
 
DRY CSS A don’t-repeat-yourself methodology for creating efficient, unified a...
DRY CSS A don’t-repeat-yourself methodology for creating efficient, unified a...DRY CSS A don’t-repeat-yourself methodology for creating efficient, unified a...
DRY CSS A don’t-repeat-yourself methodology for creating efficient, unified a...
 
Cssbestpracticesjstyleguidejandtips 150830184202-lva1-app6892
Cssbestpracticesjstyleguidejandtips 150830184202-lva1-app6892Cssbestpracticesjstyleguidejandtips 150830184202-lva1-app6892
Cssbestpracticesjstyleguidejandtips 150830184202-lva1-app6892
 
Css best practices style guide and tips
Css best practices style guide and tipsCss best practices style guide and tips
Css best practices style guide and tips
 
ADF - Layout Managers and Skinning
ADF - Layout Managers and SkinningADF - Layout Managers and Skinning
ADF - Layout Managers and Skinning
 
Cascading Style Sheets - CSS
Cascading Style Sheets - CSSCascading Style Sheets - CSS
Cascading Style Sheets - CSS
 
Css intro
Css introCss intro
Css intro
 
Introduction to Bootstrap: Design for Developers
Introduction to Bootstrap: Design for DevelopersIntroduction to Bootstrap: Design for Developers
Introduction to Bootstrap: Design for Developers
 
WordCamp NYC - DRY CSS
WordCamp NYC - DRY CSSWordCamp NYC - DRY CSS
WordCamp NYC - DRY CSS
 
uptu web technology unit 2 Css
uptu web technology unit 2 Cssuptu web technology unit 2 Css
uptu web technology unit 2 Css
 
Dangerous CSS
Dangerous CSSDangerous CSS
Dangerous CSS
 
BITM3730Week4.pptx
BITM3730Week4.pptxBITM3730Week4.pptx
BITM3730Week4.pptx
 
CSS workshop @ OutSystems
CSS workshop @ OutSystemsCSS workshop @ OutSystems
CSS workshop @ OutSystems
 
DW unit 3.pptx
DW unit 3.pptxDW unit 3.pptx
DW unit 3.pptx
 

Dernier

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
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
Safe Software
 

Dernier (20)

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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
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
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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
 
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
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
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
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
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
 
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
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
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...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 

Sane CSS - A modern approach to CSS

  • 1. Sane CSS A modern approach to CSS Lee Cheneler
  • 2. What We’ll Tackle • CSS Specificity • Quick look at how CSS specificity works • ITCSS • A sane architecture for our CSS • BEM • CSS naming convention • Top tips • These will make your life a lot easier
  • 3. CSS Specificity (╯°□°)╯︵ ┻━┻ A quick look at how CSS specificity works
  • 6. Gotcha • Last in file wins
  • 7. !important • !important trumps all • Even beats out styles set out in an elements style attribute
  • 9. Who, What? • Proposed by Harry Roberts, founder of CSS Wizardry and author of inuit.css •Inverted Triangle CSS • Aims to bring order to the CSS within your project • Outlines several layers for different parts of your CSS to sit • Helps solve common CSS problems: • Naturally reduces the likelihood of global namespace clashes (class names for instance) • CSS specificity is less of an issue due to the layered approach
  • 11. Settings • Define variables to be consumed by lower layers • Omitted if not using a pre-processor such as SASS
  • 12. Tools • Creates reusable mixins and functions to be consumed by lower layers • Omitted if not using a pre-processor like SASS
  • 13. NO CSS IS GENERATED BY THE PREVIOUS LAYERS … EVER
  • 14. Resets • Normalizing CSS goes here • Make all browsers render consistently • This essentially gives you a blank canvas that CSS in the lower layers can build on top of • Highly recommend normalize.css (available on npm, source on GitHub)
  • 15. Elements • Styling for HTML elements across your whole site • Only uses CSS element selectors • h1, p, nav, etc.
  • 16. Objects • Only defines layout and positioning, no visual styling • This increases the reusability of classes in this layer • Only use class based selectors • .grid, .flag, etc. • Common to start name spacing classes from this layer down • .o-grid, .o-flag, etc.
  • 17. Components • First layer to introduce visual styling • Only use class based selectors • .c-card, .c-modal, etc.
  • 18. Components Continued • Best not to mix objects and components on the same elements in the your HTML
  • 19. Utilities • Utility classes are not evil, but you will find yourself using them less when applying ITCSS and BEM to a project (I did at least) • Only use class based selectors • .u-text-align-center, .u-1/2, etc. • Ideally !important is contained within this layer
  • 20. Compilation • Order is important whether using ITCSS or not • Remember CSS specificity from earlier? • In ITCSS everything in a layer is available to all layers beneath it globally • Crucial for reusable mixins and variables without declaring imports everywhere… • Generally speaking, compile your CSS into a file top down following the triangle • Natural ordering to reduce specificity issues • The lower your layer the more specific the CSS is
  • 21. BEM A descriptive CSS naming convention
  • 22. Block, Element, Modifier • Goal is to clearly describe the relationship between CSS and HTML • Used to describe components/objects • Three types of class selector • Block • Element • Modifier
  • 24. Block • Top level abstraction • Applied to the outer most element of the component/object • Examples • button • grid • list
  • 25. Element • Children of Blocks • Applied to HTML elements within block elements • No bleeding • Examples • grid__item • list__item • button__icon
  • 26. Modifiers • Modifiers applied to block elements • Used to apply themes or styles to the entire component • Examples • button—lg • grid—trench • button--primary
  • 28. Top Tips These will make your life a lot easier
  • 29. Super useful tools • AutoPrefixer • Applies prefixed properties based on the caniuse.com database • StyleLint • Provides linting for CSS/SASS/LESS • CleanCSS • This minifies your CSS
  • 30. CSS Property Order • Useful to order your CSS properties in a set groups • I find this flow to be easy to remember and very useful • Position • Display & Box Model • Color • Text • Other • I don’t alphabetise or anything like that, that’s too granular and nightmare to maintain… it’s all about efficiency
  • 31. References • Xcode.co https://www.xfive.co/blog/itcss-scalable-maintainable-css-architecture/ • CSS Tricks https://css-tricks.com/bem-101/ https://css-tricks.com/specifics-on-css-specificity/ • AutoPrefixer https://github.com/postcss/autoprefixer • Normalize.css https://github.com/necolas/normalize.css/ • Code examples from easy-css https://github.com/LeeCheneler/easy-css