SlideShare a Scribd company logo
6 Steps to Make Your CSS Code More Maintainable
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

More Related Content

PPT
How to manage a big scale HTML/CSS project
PDF
CSS For Backend Developers
PDF
CSS Best practice
PDF
Maintainable CSS
PDF
Efficient, maintainable CSS
PDF
CSS Systems
PPT
Css best practices style guide and tips
PDF
CSS Reset
How to manage a big scale HTML/CSS project
CSS For Backend Developers
CSS Best practice
Maintainable CSS
Efficient, maintainable CSS
CSS Systems
Css best practices style guide and tips
CSS Reset

What's hot (20)

PDF
Structuring your CSS for maintainability: rules and guile lines to write CSS
PPS
Introduction to Bootstrap: Design for Developers
PPTX
Basics of Front End Web Dev PowerPoint
PDF
How to use CSS3 in WordPress - Sacramento
PDF
Intro to Sass for WordPress Developers
PDF
How to use CSS3 in WordPress
PPTX
PDF
Front End Best Practices
PDF
SMACSS Workshop
PDF
Responsive Web Design (April 18th, Los Angeles)
PDF
Organize Your Website With Advanced CSS Tricks
KEY
Web Design, Lesson Plan: Classes and IDs
PDF
Css border examples
PDF
Introduction to Bootstrap
PPTX
Arunan Skanthan - Roll Your own Style Guide
PPTX
Introduction to Bootstrap
PDF
PSD to Theme: The SMACSS Way
PPTX
Design for Developers: Introduction to Bootstrap 3
PPTX
Crafting Custom CSS @ PodCamp Toronto 2015 #PCTO15
Structuring your CSS for maintainability: rules and guile lines to write CSS
Introduction to Bootstrap: Design for Developers
Basics of Front End Web Dev PowerPoint
How to use CSS3 in WordPress - Sacramento
Intro to Sass for WordPress Developers
How to use CSS3 in WordPress
Front End Best Practices
SMACSS Workshop
Responsive Web Design (April 18th, Los Angeles)
Organize Your Website With Advanced CSS Tricks
Web Design, Lesson Plan: Classes and IDs
Css border examples
Introduction to Bootstrap
Arunan Skanthan - Roll Your own Style Guide
Introduction to Bootstrap
PSD to Theme: The SMACSS Way
Design for Developers: Introduction to Bootstrap 3
Crafting Custom CSS @ PodCamp Toronto 2015 #PCTO15
Ad

Viewers also liked (11)

PPTX
Data dictionary
PPTX
Data Dictionary
PPTX
Data dictionary
KEY
Object Oriented CSS
PDF
From Scratch to Launch #2 - Design phase
PDF
The benefits of BEM CSS
PPTX
Systems Analyst and Design - Data Dictionary
PDF
jQuery Essentials
PPT
Data dictionary
PDF
Atomic design
PDF
The Great State of Design with CSS Grid Layout and Friends
Data dictionary
Data Dictionary
Data dictionary
Object Oriented CSS
From Scratch to Launch #2 - Design phase
The benefits of BEM CSS
Systems Analyst and Design - Data Dictionary
jQuery Essentials
Data dictionary
Atomic design
The Great State of Design with CSS Grid Layout and Friends
Ad

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

PDF
The CSS Handbook
PDF
Introduction to css
PPTX
SCSS Implementation
PDF
Css masterclass book
PDF
Design and CSS
PPTX
Sass:-Syntactically Awesome Stylesheet by Shafeeq
PDF
CSS Best Practices
PPTX
SPS Oslo - Stop your SharePoint CSS becoming a di-sass-ter today!
PPTX
Lesson 110 24 aug13-1400-ay
PDF
Architecting with Style
PPT
Spectrum 2015 going online with style - an intro to css
PPTX
SASS - Syntactically Awesome Stylesheet
PPT
An Introduction to CSS Frameworks
PDF
The World of Stylesheet Languages
PPT
Object oriented css. Graeme Blackwood
PPT
Object oriented css graeme blackwood
PPTX
Styling your projects! leveraging css and r sass in r projects
PPTX
Your CSS is Awesome
PDF
CSS_GUIDE_Intro
PDF
CSS_GUIDE_Intro
The CSS Handbook
Introduction to css
SCSS Implementation
Css masterclass book
Design and CSS
Sass:-Syntactically Awesome Stylesheet by Shafeeq
CSS Best Practices
SPS Oslo - Stop your SharePoint CSS becoming a di-sass-ter today!
Lesson 110 24 aug13-1400-ay
Architecting with Style
Spectrum 2015 going online with style - an intro to css
SASS - Syntactically Awesome Stylesheet
An Introduction to CSS Frameworks
The World of Stylesheet Languages
Object oriented css. Graeme Blackwood
Object oriented css graeme blackwood
Styling your projects! leveraging css and r sass in r projects
Your CSS is Awesome
CSS_GUIDE_Intro
CSS_GUIDE_Intro

Recently uploaded (20)

PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Approach and Philosophy of On baking technology
PDF
Empathic Computing: Creating Shared Understanding
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Modernizing your data center with Dell and AMD
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
Building Integrated photovoltaic BIPV_UPV.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation_ Review paper, used for researhc scholars
Spectral efficient network and resource selection model in 5G networks
NewMind AI Monthly Chronicles - July 2025
Review of recent advances in non-invasive hemoglobin estimation
NewMind AI Weekly Chronicles - August'25 Week I
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation theory and applications.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Approach and Philosophy of On baking technology
Empathic Computing: Creating Shared Understanding
Mobile App Security Testing_ A Comprehensive Guide.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Modernizing your data center with Dell and AMD
Diabetes mellitus diagnosis method based random forest with bat algorithm
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Dropbox Q2 2025 Financial Results & Investor Presentation

6 Steps to Make Your CSS Code More Maintainable

  • 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