SlideShare uma empresa Scribd logo
1 de 23
Baixar para ler offline
6 STEPS TO MAKE YOUR CSS
CODE MORE MAINTAINABLE
FRONT-END DEVELOPER
MARTA
SZTYBOR
CSS is a fairly simple language to learn on a basic level and to kick off with the coding.
But when your stylesheets start to grow, it becomes more and more difficult to untangle
messed up classes.
Here I collected a couple of tips to help you write more manageable
code. They’ll be useful no matter if you’re working on a project’s
styles on your own or in a team.
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
MODULARISE
YOUR STYLES
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
Begin treating your UI
as a set of repeatable
modules.
For most webpages it’s
possible to break up the
layout for smaller components,
eg. header with navigation,
footer, article, or even the
smallest building blocks, eg.
buttons and links.
Preprocessors give you the
power to write stylesheets
separately, and then @import
them into the main file.
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
MODULARISE YOUR STYLES
DECIDE
ON A NAMING CONVENTION
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
Naming things is hard I’d say, especially if you try to name CSS classes.
Your first thought could be “Let’s name things semantically!”. Yeah, it’s perfectly right! When
the graphic designer changes colours, you don’t want to grep HTML files to change .blue-box
to .green-box (to say the least, there could also be a hundred classes named that way). So, you
decide to choose to name things like .nav-main instead of .large-emerald-shiny-tabs.
But don’t hold on too tightly, the main point is to take the most sensible
approach about naming classes as possible – all of the selector names
don’t have to be semantic. Just follow a coherent convention and don’t
use names describing an element’s appearance.
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
DECIDE ON A NAMING CONVENTION
DEAL
WITH SPECIFICITY
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
In general, try to stick to this simple rule of thumb: always try
to use the least specific selectors as possible.
Very specific selectors, like
are hard to override.
In any case, when you have to code a similar element, but with slightly different
styles, you would write a more specific selector, or even be tempted to use
!important. This leads straight to hard-to-maintain code.
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
DEAL WITH SPECIFICITY
I would recommend using explicit selectors for an element, like classes:
It looks better, right? You should also avoid using IDs, because they are almost
as specific as inline styles. They also have another drawback: they aren’t
reusable, and this is something we really don’t want to have in our modular CSS.
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
DEAL WITH SPECIFICITY
USE PREPROCESSORS
THE RIGHT WAY
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
Preprocessors can really be a double-edged sword. They cannot
guarantee you a clean, well-maintainable code if you don’t use
their features properly.
You should avoid nesting CSS rules, unless it’s really necessary. This is probably
the most common mistake made by beginners. Follow the inception rule:
Don’t go more than four levels deep.
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
USE PREPROCESSORS THE RIGHT WAY
Consider following HTML example:
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
USE PREPROCESSORS THE RIGHT WAY
It’s tempting to recreate this HTML structure in styles and write in SCSS:
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
USE PREPROCESSORS THE RIGHT WAY
It then compiles to:
If you needed a similar menu in another page, but with just a little tweaked
color of the link, you’d have to override the last selector, and probably end up
with this:
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
USE PREPROCESSORS THE RIGHT WAY
Not even this creates overly qualified selectors and causes
problems with specificity. With tightly coupled HTML, it be-
comes hard to maintain. When overused, it may cause per-
formance problems and cause your stylesheets to grow in an
uncontrolled manner.
Overly nested rules are also less readable.
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
USE PREPROCESSORS THE RIGHT WAY
AVOID
UNDOING STYLES
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
CSS is about inheritance, so why undo values declared in
another selector?
Let’s have styles for headings:
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
AVOID UNDOING STYLES
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
If styles are being resetted in
.header-no-border
class, why declare them for base class
.heading? This would be a simpler and more maintainable way:
AVOID UNDOING STYLES
DOCUMENT YOUR CODE
AND CREATE A STYLE GUIDE
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
CSS is a declarative language, so if you are using meaningful class names
documenting code can seem unnecessary. But thinking about the future and
other people who are or will be involved in your project, comments are an
invaluable thing.
It’s worth to create your own or choose existing code
guidelines, which include naming conventions, methodology
used, commenting style and file structure. This will benefit all
the people who ever touch the code.
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
DOCUMENT YOUR CODE AND CREATE A STYLE GUIDE
This presentation just scratches the surface. If you are interested in this topic, I
can recommend you some excellent resources in the web, which cover the issues
of modular CSS, code maintainability and various methodologies:
•	 Nicolle Sullivan’s Object Oriented CSS (OOCSS)
•	 Jonathan Snook’s Scalable and Modular Architecture for CSS
•	 Mark D. Otto’s code guide
•	 Harry Roberts’ CSS Guidelines
•	 Creating style guides on A List Apart
6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
FURTHER READING
THANK YOU FOR READING
FRONT-END DEVELOPER
MARTA
SZTYBOR

Mais conteúdo relacionado

Mais procurados

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
 
Introduction to Bootstrap: Design for Developers
Introduction to Bootstrap: Design for DevelopersIntroduction to Bootstrap: Design for Developers
Introduction to Bootstrap: Design for DevelopersMelvin John
 
Basics of Front End Web Dev PowerPoint
Basics of Front End Web Dev PowerPointBasics of Front End Web Dev PowerPoint
Basics of Front End Web Dev PowerPointSahil Gandhi
 
How to use CSS3 in WordPress - Sacramento
How to use CSS3 in WordPress - SacramentoHow to use CSS3 in WordPress - Sacramento
How to use CSS3 in WordPress - SacramentoSuzette Franck
 
Intro to Sass for WordPress Developers
Intro to Sass for WordPress DevelopersIntro to Sass for WordPress Developers
Intro to Sass for WordPress DevelopersSuzette Franck
 
How to use CSS3 in WordPress
How to use CSS3 in WordPressHow to use CSS3 in WordPress
How to use CSS3 in WordPressSuzette Franck
 
Front End Best Practices
Front End Best PracticesFront End Best Practices
Front End Best PracticesHolger Bartel
 
Responsive Web Design (April 18th, Los Angeles)
Responsive Web Design (April 18th, Los Angeles)Responsive Web Design (April 18th, Los Angeles)
Responsive Web Design (April 18th, Los Angeles)Thinkful
 
Organize Your Website With Advanced CSS Tricks
Organize Your Website With Advanced CSS TricksOrganize Your Website With Advanced CSS Tricks
Organize Your Website With Advanced CSS TricksAndolasoft Inc
 
Web Design, Lesson Plan: Classes and IDs
Web Design, Lesson Plan: Classes and IDsWeb Design, Lesson Plan: Classes and IDs
Web Design, Lesson Plan: Classes and IDsSteve Kinney
 
Introduction to Bootstrap
Introduction to BootstrapIntroduction to Bootstrap
Introduction to BootstrapRon Reiter
 
Arunan Skanthan - Roll Your own Style Guide
Arunan Skanthan - Roll Your own Style GuideArunan Skanthan - Roll Your own Style Guide
Arunan Skanthan - Roll Your own Style GuideWeb Directions
 
Design for Developers: Introduction to Bootstrap 3
Design for Developers: Introduction to Bootstrap 3Design for Developers: Introduction to Bootstrap 3
Design for Developers: Introduction to Bootstrap 3John Bertucci
 
Crafting Custom CSS @ PodCamp Toronto 2015 #PCTO15
Crafting Custom CSS @ PodCamp Toronto 2015 #PCTO15Crafting Custom CSS @ PodCamp Toronto 2015 #PCTO15
Crafting Custom CSS @ PodCamp Toronto 2015 #PCTO15Andy McIlwain
 

Mais procurados (20)

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
 
Introduction to Bootstrap: Design for Developers
Introduction to Bootstrap: Design for DevelopersIntroduction to Bootstrap: Design for Developers
Introduction to Bootstrap: Design for Developers
 
Basics of Front End Web Dev PowerPoint
Basics of Front End Web Dev PowerPointBasics of Front End Web Dev PowerPoint
Basics of Front End Web Dev PowerPoint
 
How to use CSS3 in WordPress - Sacramento
How to use CSS3 in WordPress - SacramentoHow to use CSS3 in WordPress - Sacramento
How to use CSS3 in WordPress - Sacramento
 
Intro to Sass for WordPress Developers
Intro to Sass for WordPress DevelopersIntro to Sass for WordPress Developers
Intro to Sass for WordPress Developers
 
How to use CSS3 in WordPress
How to use CSS3 in WordPressHow to use CSS3 in WordPress
How to use CSS3 in WordPress
 
Css home
Css   homeCss   home
Css home
 
Front End Best Practices
Front End Best PracticesFront End Best Practices
Front End Best Practices
 
SMACSS Workshop
SMACSS WorkshopSMACSS Workshop
SMACSS Workshop
 
Responsive Web Design (April 18th, Los Angeles)
Responsive Web Design (April 18th, Los Angeles)Responsive Web Design (April 18th, Los Angeles)
Responsive Web Design (April 18th, Los Angeles)
 
Lecture2 CSS1
Lecture2  CSS1Lecture2  CSS1
Lecture2 CSS1
 
Organize Your Website With Advanced CSS Tricks
Organize Your Website With Advanced CSS TricksOrganize Your Website With Advanced CSS Tricks
Organize Your Website With Advanced CSS Tricks
 
Web Design, Lesson Plan: Classes and IDs
Web Design, Lesson Plan: Classes and IDsWeb Design, Lesson Plan: Classes and IDs
Web Design, Lesson Plan: Classes and IDs
 
Css border examples
Css border examplesCss border examples
Css border examples
 
Introduction to Bootstrap
Introduction to BootstrapIntroduction to Bootstrap
Introduction to Bootstrap
 
Arunan Skanthan - Roll Your own Style Guide
Arunan Skanthan - Roll Your own Style GuideArunan Skanthan - Roll Your own Style Guide
Arunan Skanthan - Roll Your own Style Guide
 
Introduction to Bootstrap
Introduction to BootstrapIntroduction to Bootstrap
Introduction to Bootstrap
 
PSD to Theme: The SMACSS Way
PSD to Theme: The SMACSS WayPSD to Theme: The SMACSS Way
PSD to Theme: The SMACSS Way
 
Design for Developers: Introduction to Bootstrap 3
Design for Developers: Introduction to Bootstrap 3Design for Developers: Introduction to Bootstrap 3
Design for Developers: Introduction to Bootstrap 3
 
Crafting Custom CSS @ PodCamp Toronto 2015 #PCTO15
Crafting Custom CSS @ PodCamp Toronto 2015 #PCTO15Crafting Custom CSS @ PodCamp Toronto 2015 #PCTO15
Crafting Custom CSS @ PodCamp Toronto 2015 #PCTO15
 

Destaque

From Scratch to Launch #2 - Design phase
From Scratch to Launch #2 - Design phaseFrom Scratch to Launch #2 - Design phase
From Scratch to Launch #2 - Design phaseStacy Kvernmo
 
Systems Analyst and Design - Data Dictionary
Systems Analyst and Design -  Data DictionarySystems Analyst and Design -  Data Dictionary
Systems Analyst and Design - Data DictionaryKimberly Coquilla
 
The Great State of Design with CSS Grid Layout and Friends
The Great State of Design with CSS Grid Layout and FriendsThe Great State of Design with CSS Grid Layout and Friends
The Great State of Design with CSS Grid Layout and FriendsStacy Kvernmo
 

Destaque (11)

Data dictionary
Data dictionaryData dictionary
Data dictionary
 
Data Dictionary
Data DictionaryData Dictionary
Data Dictionary
 
Data dictionary
Data dictionaryData dictionary
Data dictionary
 
Object Oriented CSS
Object Oriented CSSObject Oriented CSS
Object Oriented CSS
 
From Scratch to Launch #2 - Design phase
From Scratch to Launch #2 - Design phaseFrom Scratch to Launch #2 - Design phase
From Scratch to Launch #2 - Design phase
 
The benefits of BEM CSS
The benefits of BEM CSSThe benefits of BEM CSS
The benefits of BEM CSS
 
Systems Analyst and Design - Data Dictionary
Systems Analyst and Design -  Data DictionarySystems Analyst and Design -  Data Dictionary
Systems Analyst and Design - Data Dictionary
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
 
Data dictionary
Data dictionaryData dictionary
Data dictionary
 
Atomic design
Atomic designAtomic design
Atomic design
 
The Great State of Design with CSS Grid Layout and Friends
The Great State of Design with CSS Grid Layout and FriendsThe Great State of Design with CSS Grid Layout and Friends
The Great State of Design with CSS Grid Layout and Friends
 

Semelhante a 6 Steps to Make Your CSS Code More Maintainable

The CSS Handbook
The CSS HandbookThe CSS Handbook
The CSS Handbookjackchenvlo
 
SCSS Implementation
SCSS ImplementationSCSS Implementation
SCSS ImplementationAmey Parab
 
Css masterclass book
Css masterclass bookCss masterclass book
Css masterclass bookPhoenix
 
Design and CSS
Design and CSSDesign and CSS
Design and CSSnolly00
 
Sass:-Syntactically Awesome Stylesheet by Shafeeq
Sass:-Syntactically Awesome Stylesheet by ShafeeqSass:-Syntactically Awesome Stylesheet by Shafeeq
Sass:-Syntactically Awesome Stylesheet by ShafeeqDignitasDigital1
 
CSS Best Practices
CSS Best PracticesCSS Best Practices
CSS Best Practicesnolly00
 
SPS Oslo - Stop your SharePoint CSS becoming a di-sass-ter today!
SPS Oslo - Stop your SharePoint CSS becoming a di-sass-ter today!SPS Oslo - Stop your SharePoint CSS becoming a di-sass-ter today!
SPS Oslo - Stop your SharePoint CSS becoming a di-sass-ter today!Stefan Bauer
 
Lesson 110 24 aug13-1400-ay
Lesson 110 24 aug13-1400-ayLesson 110 24 aug13-1400-ay
Lesson 110 24 aug13-1400-ayCodecademy Ren
 
Architecting with Style
Architecting with StyleArchitecting with Style
Architecting with StyleTimothy Knight
 
Spectrum 2015 going online with style - an intro to css
Spectrum 2015   going online with style - an intro to cssSpectrum 2015   going online with style - an intro to css
Spectrum 2015 going online with style - an intro to cssNeil Perlin
 
SASS - Syntactically Awesome Stylesheet
SASS - Syntactically Awesome StylesheetSASS - Syntactically Awesome Stylesheet
SASS - Syntactically Awesome StylesheetNeha Sharma
 
An Introduction to CSS Frameworks
An Introduction to CSS FrameworksAn Introduction to CSS Frameworks
An Introduction to CSS FrameworksAdrian Westlake
 
The World of Stylesheet Languages
The World of Stylesheet LanguagesThe World of Stylesheet Languages
The World of Stylesheet LanguagesAndrea Tino
 
Object oriented css. Graeme Blackwood
Object oriented css. Graeme BlackwoodObject oriented css. Graeme Blackwood
Object oriented css. Graeme BlackwoodPVasili
 
Object oriented css graeme blackwood
Object oriented css graeme blackwoodObject oriented css graeme blackwood
Object oriented css graeme blackwooddrupalconf
 
Styling your projects! leveraging css and r sass in r projects
Styling your projects! leveraging css and r sass in r projectsStyling your projects! leveraging css and r sass in r projects
Styling your projects! leveraging css and r sass in r projectsAppsilon Data Science
 

Semelhante a 6 Steps to Make Your CSS Code More Maintainable (20)

The CSS Handbook
The CSS HandbookThe CSS Handbook
The CSS Handbook
 
Introduction to css
Introduction to cssIntroduction to css
Introduction to css
 
SCSS Implementation
SCSS ImplementationSCSS Implementation
SCSS Implementation
 
Css masterclass book
Css masterclass bookCss masterclass book
Css masterclass book
 
Design and CSS
Design and CSSDesign and CSS
Design and CSS
 
Sass:-Syntactically Awesome Stylesheet by Shafeeq
Sass:-Syntactically Awesome Stylesheet by ShafeeqSass:-Syntactically Awesome Stylesheet by Shafeeq
Sass:-Syntactically Awesome Stylesheet by Shafeeq
 
CSS Best Practices
CSS Best PracticesCSS Best Practices
CSS Best Practices
 
SPS Oslo - Stop your SharePoint CSS becoming a di-sass-ter today!
SPS Oslo - Stop your SharePoint CSS becoming a di-sass-ter today!SPS Oslo - Stop your SharePoint CSS becoming a di-sass-ter today!
SPS Oslo - Stop your SharePoint CSS becoming a di-sass-ter today!
 
Lesson 110 24 aug13-1400-ay
Lesson 110 24 aug13-1400-ayLesson 110 24 aug13-1400-ay
Lesson 110 24 aug13-1400-ay
 
Architecting with Style
Architecting with StyleArchitecting with Style
Architecting with Style
 
Spectrum 2015 going online with style - an intro to css
Spectrum 2015   going online with style - an intro to cssSpectrum 2015   going online with style - an intro to css
Spectrum 2015 going online with style - an intro to css
 
SASS - Syntactically Awesome Stylesheet
SASS - Syntactically Awesome StylesheetSASS - Syntactically Awesome Stylesheet
SASS - Syntactically Awesome Stylesheet
 
An Introduction to CSS Frameworks
An Introduction to CSS FrameworksAn Introduction to CSS Frameworks
An Introduction to CSS Frameworks
 
The World of Stylesheet Languages
The World of Stylesheet LanguagesThe World of Stylesheet Languages
The World of Stylesheet Languages
 
Object oriented css. Graeme Blackwood
Object oriented css. Graeme BlackwoodObject oriented css. Graeme Blackwood
Object oriented css. Graeme Blackwood
 
Object oriented css graeme blackwood
Object oriented css graeme blackwoodObject oriented css graeme blackwood
Object oriented css graeme blackwood
 
Styling your projects! leveraging css and r sass in r projects
Styling your projects! leveraging css and r sass in r projectsStyling your projects! leveraging css and r sass in r projects
Styling your projects! leveraging css and r sass in r projects
 
Your CSS is Awesome
Your CSS is AwesomeYour CSS is Awesome
Your CSS is Awesome
 
CSS_GUIDE_Intro
CSS_GUIDE_IntroCSS_GUIDE_Intro
CSS_GUIDE_Intro
 
CSS_GUIDE_Intro
CSS_GUIDE_IntroCSS_GUIDE_Intro
CSS_GUIDE_Intro
 

Último

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 

Último (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

6 Steps to Make Your CSS Code More Maintainable

  • 1.
  • 2. 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE FRONT-END DEVELOPER MARTA SZTYBOR
  • 3. CSS is a fairly simple language to learn on a basic level and to kick off with the coding. But when your stylesheets start to grow, it becomes more and more difficult to untangle messed up classes. Here I collected a couple of tips to help you write more manageable code. They’ll be useful no matter if you’re working on a project’s styles on your own or in a team. 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
  • 4. MODULARISE YOUR STYLES 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
  • 5. Begin treating your UI as a set of repeatable modules. For most webpages it’s possible to break up the layout for smaller components, eg. header with navigation, footer, article, or even the smallest building blocks, eg. buttons and links. Preprocessors give you the power to write stylesheets separately, and then @import them into the main file. 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE MODULARISE YOUR STYLES
  • 6. DECIDE ON A NAMING CONVENTION 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
  • 7. Naming things is hard I’d say, especially if you try to name CSS classes. Your first thought could be “Let’s name things semantically!”. Yeah, it’s perfectly right! When the graphic designer changes colours, you don’t want to grep HTML files to change .blue-box to .green-box (to say the least, there could also be a hundred classes named that way). So, you decide to choose to name things like .nav-main instead of .large-emerald-shiny-tabs. But don’t hold on too tightly, the main point is to take the most sensible approach about naming classes as possible – all of the selector names don’t have to be semantic. Just follow a coherent convention and don’t use names describing an element’s appearance. 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE DECIDE ON A NAMING CONVENTION
  • 8. DEAL WITH SPECIFICITY 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
  • 9. In general, try to stick to this simple rule of thumb: always try to use the least specific selectors as possible. Very specific selectors, like are hard to override. In any case, when you have to code a similar element, but with slightly different styles, you would write a more specific selector, or even be tempted to use !important. This leads straight to hard-to-maintain code. 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE DEAL WITH SPECIFICITY
  • 10. I would recommend using explicit selectors for an element, like classes: It looks better, right? You should also avoid using IDs, because they are almost as specific as inline styles. They also have another drawback: they aren’t reusable, and this is something we really don’t want to have in our modular CSS. 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE DEAL WITH SPECIFICITY
  • 11. USE PREPROCESSORS THE RIGHT WAY 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
  • 12. Preprocessors can really be a double-edged sword. They cannot guarantee you a clean, well-maintainable code if you don’t use their features properly. You should avoid nesting CSS rules, unless it’s really necessary. This is probably the most common mistake made by beginners. Follow the inception rule: Don’t go more than four levels deep. 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE USE PREPROCESSORS THE RIGHT WAY
  • 13. Consider following HTML example: 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE USE PREPROCESSORS THE RIGHT WAY
  • 14. It’s tempting to recreate this HTML structure in styles and write in SCSS: 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE USE PREPROCESSORS THE RIGHT WAY
  • 15. It then compiles to: If you needed a similar menu in another page, but with just a little tweaked color of the link, you’d have to override the last selector, and probably end up with this: 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE USE PREPROCESSORS THE RIGHT WAY
  • 16. Not even this creates overly qualified selectors and causes problems with specificity. With tightly coupled HTML, it be- comes hard to maintain. When overused, it may cause per- formance problems and cause your stylesheets to grow in an uncontrolled manner. Overly nested rules are also less readable. 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE USE PREPROCESSORS THE RIGHT WAY
  • 17. AVOID UNDOING STYLES 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
  • 18. CSS is about inheritance, so why undo values declared in another selector? Let’s have styles for headings: 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE AVOID UNDOING STYLES
  • 19. 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE If styles are being resetted in .header-no-border class, why declare them for base class .heading? This would be a simpler and more maintainable way: AVOID UNDOING STYLES
  • 20. DOCUMENT YOUR CODE AND CREATE A STYLE GUIDE 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE
  • 21. CSS is a declarative language, so if you are using meaningful class names documenting code can seem unnecessary. But thinking about the future and other people who are or will be involved in your project, comments are an invaluable thing. It’s worth to create your own or choose existing code guidelines, which include naming conventions, methodology used, commenting style and file structure. This will benefit all the people who ever touch the code. 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE DOCUMENT YOUR CODE AND CREATE A STYLE GUIDE
  • 22. This presentation just scratches the surface. If you are interested in this topic, I can recommend you some excellent resources in the web, which cover the issues of modular CSS, code maintainability and various methodologies: • Nicolle Sullivan’s Object Oriented CSS (OOCSS) • Jonathan Snook’s Scalable and Modular Architecture for CSS • Mark D. Otto’s code guide • Harry Roberts’ CSS Guidelines • Creating style guides on A List Apart 6 STEPS TO MAKE YOUR CSS CODE MORE MAINTAINABLE FURTHER READING
  • 23. THANK YOU FOR READING FRONT-END DEVELOPER MARTA SZTYBOR