SlideShare a Scribd company logo
Structuring your CSS for
maintainability
Class-9: Rules and guile lines to write CSS
Sanjoy K. Paul
Lead Application Developer | Software Architect | DevOps | Trainer
www.SanjoyPaul.com | skpaul@DevsStation.com | +880-1511-927992
HTML or CSS First?
KISS - Keep It Simple & Short(hand).
DRY - Don’t Repeat Yourself.
IMP - Important! is not that important.
- Make It Readable
- Keep It Consistent
- Use a Reset
TDS - Top-Down Structure
- Comment is the savier
- No Inline
Organizing your CSS
As you start work on larger stylesheets and big project with a team, you will
discover that maintaining a huge css file can be challenging.
So, we will go through some best practices for writing css that will help us to
maintain the css project easily.
How to keep your stylesheet organized and tidy?
Does our project have a coding style guide?
- If we are working in a team or existing project? then
- First thing to check is whether the project has an existing style guide for CSS?
- The team style guide should always win over your own personal preferences.
- There often isn't a right or wrong way to do things, but consistency is
important.
Keep it consistent
- set the rules for the project or are working alone,
- then the most important thing to do is to keep things consistent.
- Consistency can be applied in all sorts of ways,
- Using the same naming conventions for classes,
- choosing one method of describing color,
- or maintaining consistent formatting
- for example will you use tabs or spaces to indent your code? If
spaces, how many spaces?
Keep it consistent
Pros:
- Having a set of rules you always follow
- Reduces the amount of mental overhead needed when writing
CSS, as some of the decisions are already made.
Cons:
- Sometime it’s hard to maintain the consistency if you are in a
tight deadline
Formatting readable CSS:
There are a couple of ways you will see CSS formatted.
Some developers put all of the rules onto a single line
.box { background-color: #567895; }
h2 { background-color: black; color: white; }
Other developers prefer to break everything onto a
new line:
.box {
background-color: #567895;
}
h2 {
background-color: black;
color: white;
}
CSS doesn't mind which one you use. I personally find it is more readable to have each property and value pair on a new line.
Comment your CSS
Adding comments to your CSS will help
- Any future developer work with your CSS file
- Also will help you when you come back to the project after a break.
/* This is a CSS comment
It can be broken onto multiple lines. */
Comment your CSS
/* || General styles */
...
/* || Typography */
...
/* || Header and Main
Navigation */
...
- Add a block of comments between logical
sections
- It will help to locate different sections quickly
when scanning through,
- Or even give you something to search for to
jump right into that part of the CSS.
- If you use a string which won't appear in the
code you can jump from section to section by
searching for it. We can use || or --start an. ..end
Comment your CSS
We don't need to comment every single thing in our code, as much of it is self-explanatory. We only comment on the things where we make a
particular decision for a reason.
Another example: Including reference tutorial/any links as a comment. It will help us to recall in future.
/** CSS Code guideline
https://developer.mozilla.org/en-US/docs/MDN/Guidelines/Code_guidelines/CSS
*/
Example: we may have used a CSS property in a specific way to get around older browser incompatibilities.
.box {
background-color : red; /* fallback for older browsers that don't support gradients */
background-image : linear-gradient (to right, #ff0000, #aa0000);
}
Create logical sections in your stylesheet
It is a good idea to have all of the common styling first in the stylesheet. This means all of the styles which will generally
apply unless you do something special with that element. You will typically have rules set up for:
● body
● p
● h1, h2, h3, h4, h5
● ul and ol
● The table properties
● Links
Lets see some code...
Using CSS Vendor Prefixed
Some of these CSS browser prefixes have been listed below:
iOS: -webkit-
Safari: -webkit-
Firefox: -moz-
Chrome: -webkit-
Opera: -o-
MS/IE/Edge: -ms-
Each browser have a set of specifications related to any style. This is one of the reasons why browser prefixed are
implemented in order to ensure that these browsers support any of the specific features or style that we would like to
implement.
If we are planning to add a CSS 3 transition to any of our CSS code, then
we will can use transition property and implement a vendor prefix with it.
Below is the code for the same:
-webkit-transition: all 1s ease;
-moz-transition: all 1s ease;
-ms-transition: all 1s ease;
-o-transition: all 1s ease;
transition: all 1s ease;
Do Not Be Too Specific & Chain Classes
article.main p.box {
border: 1px solid #ccc;
}
.box {
border: 1px solid #ccc;
}
.btn.submit {
border: 1px solid #ccc;
}
.btn{
border: 1px solid #ccc;
}
.submit {
Background: #FF0;
}
Break large stylesheets into multiple smaller ones!??
For example, you might have an online store as part of the site, with a lot of CSS
used only for styling the product listings and forms needed for the store. It would
make sense to have those things in a different stylesheet, only linked to on store
pages.
This can make it easier to keep your CSS organized, and also means that if multiple
people are working on the CSS you will have fewer situations where two people
need to work on the same stylesheet at once, leading to conflicts in source control.
CSS methodologies
- OOCSS (Object-Oriented CSS): Treats page elements as objects, giving all objects classes, treating the objects’ classes as single
entities in style sheets, and taking it from there.. https://github.com/stubbornella/oocss/wiki
- BEM (Block, Element, Modifier): Block Element Modifier is a methodology that helps you create reusable components and code
sharing in front-end development. - https://css-tricks.com/bem-101/
- SMACSS (Scalable and Modular Architecture for CSS): a flexible guide to developing sites small and large. Arguably
becoming one of the most useful contributions to front-end discussions in years. - http://smacss.com/
- Atomic CSS: is the approach to CSS architecture that favors small, single-purpose classes with names based on visual function. -
https://acss.io/
- ITCSS - Inverted Triangle CSS: A sane, scalable, managed CSS architecture achieved with mindful CSS code organization. -
https://itcss.io/
HTML!
What we do to write HTML code?
- Always Declare Document Type (<!DOCTYPE html>)
- Use Lowercase “Element” and “Attribute” Names. Don’t mix uppercase and lowercase (body, div, article)
- Always Quote Attribute Values
- Always Specify “alt” for Images (and also width and height)
- Close All HTML Elements
- Close Empty HTML Elements? (<meta charset="utf-8" />, required in XML and XHTML)
- Never Skip the <title> Element (SEO)
- Add the lang and charset attribute, both are important for SEO indexing (SEO, <html lang="en-us" />)
- Meta Datas (key, description, og and )
- Avoid Long Code Lines
- Spaces and Equal Signs
- Blank Lines and Indentation. Don’t use it unnecessarily.
- Omitting <html>, <body>, <head> tags?
- Viewport
- Create Your HTML First
- It will be updated and continued with time..
Summary
KISS - Keep It Simple & Short(hand).
DRY - Don’t Repeat Yourself.
IMP - Important! is not that important.
- Make It Readable
- Keep It Consistent
- Use a Reset??
TDS - Top-Down Structure
- The Comment is the savior
- No Inline
- Maintain the Spacificity (*, tag, class, id)
- ...
Validating your CSS!
You can always use the W3C free CSS validator to examine whether your CSS code has been organized and structured
appropriately.
One of the other benefits of using it is to help you find errors within your stylesheet.
This will save your time to troubleshooting comparing to doing it manually.
Markup Validation Tool: https://validator.w3.org/
CSS Validation Tool: https://jigsaw.w3.org/css-validator/
Browser Compatibility Testing Tool: https://app.crossbrowsertesting.com/ https://www.lambdatest.com/
Interested to know more?
- https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Organizing#create_logical_sections_in_your_stylesheet
- https://developer.mozilla.org/en-US/docs/MDN/Guidelines/Code_guidelines/CSS
- https://www.w3schools.com/html/html5_syntax.asp
- https://www.valoremreply.com/post/5_css_methodologies/
- https://cssguidelin.es/
- https://www.sitepoint.com/optimizing-css-performance/
Question?
Sanjoy K. Paul
Lead Application Developer | Software Architect | DevOps | Trainer
www.SanjoyPaul.com | skpaul@DevsStation.com | +880-1511-927992

More Related Content

PDF
CSS For Backend Developers
PDF
CSS Systems
PDF
6 Steps to Make Your CSS Code More Maintainable
PDF
Efficient, maintainable CSS
PPTX
Introduction to CSS
PPT
CSS Methodology
PDF
SMACSS Workshop
PDF
CSS Best practice
CSS For Backend Developers
CSS Systems
6 Steps to Make Your CSS Code More Maintainable
Efficient, maintainable CSS
Introduction to CSS
CSS Methodology
SMACSS Workshop
CSS Best practice

What's hot (20)

PPTX
Introduction to HTML and CSS
PDF
The Dark Arts of CSS
PPTX
Basics of Front End Web Dev PowerPoint
PPT
Spectrum 2015 going online with style - an intro to css
PDF
Front End Best Practices
PPTX
PDF
PDF
How to use CSS3 in WordPress
PDF
Fewd week2 slides
PPT
Css best practices style guide and tips
PDF
Girl Develop It Cincinnati: Intro to HTML/CSS Class 4
ODP
Cascading Style Sheets - Part 02
PPTX
Crafting Custom CSS @ PodCamp Toronto 2015 #PCTO15
PDF
Modern Front-End Development
PPTX
PDF
Intro to Sass for WordPress Developers
PDF
Minimalist Theming: How to Build a Lean, Mean Drupal 8 Theme
PDF
Teams, styles and scalable applications
PPTX
HTML/CSS for WordPress
Introduction to HTML and CSS
The Dark Arts of CSS
Basics of Front End Web Dev PowerPoint
Spectrum 2015 going online with style - an intro to css
Front End Best Practices
How to use CSS3 in WordPress
Fewd week2 slides
Css best practices style guide and tips
Girl Develop It Cincinnati: Intro to HTML/CSS Class 4
Cascading Style Sheets - Part 02
Crafting Custom CSS @ PodCamp Toronto 2015 #PCTO15
Modern Front-End Development
Intro to Sass for WordPress Developers
Minimalist Theming: How to Build a Lean, Mean Drupal 8 Theme
Teams, styles and scalable applications
HTML/CSS for WordPress
Ad

Similar to Structuring your CSS for maintainability: rules and guile lines to write CSS (20)

PDF
Creative Web 02 - HTML & CSS Basics
PPT
Css Best Practices
PPT
Css Best Practices
ODP
HTML5, CSS, JavaScript Style guide and coding conventions
PPT
CSS Basics
PDF
Highly Maintainable, Efficient, and Optimized CSS
PDF
A complete html and css guidelines for beginners
PPT
Html & CSS - Best practices 2-hour-workshop
PPT
Make Css easy(part:2) : easy tips for css(part:2)
KEY
The Cascade, Grids, Headings, and Selectors from an OOCSS Perspective, Ajax ...
PPTX
Cordova training - Day 2 Introduction to CSS 3
PDF
Advanced CSS Troubleshooting
PPTX
Embrace the Mullet: CSS is the 'Party in the Back' (a CSS How-to)
PDF
HTML and CSS Coding Standards
PDF
Advanced CSS Troubleshooting & Efficiency
PPT
Cssbestpracticesjstyleguidejandtips 150830184202-lva1-app6892
PDF
Pfnp slides
PPT
PDF
Maintainable CSS
DOCX
Css Introduction
Creative Web 02 - HTML & CSS Basics
Css Best Practices
Css Best Practices
HTML5, CSS, JavaScript Style guide and coding conventions
CSS Basics
Highly Maintainable, Efficient, and Optimized CSS
A complete html and css guidelines for beginners
Html & CSS - Best practices 2-hour-workshop
Make Css easy(part:2) : easy tips for css(part:2)
The Cascade, Grids, Headings, and Selectors from an OOCSS Perspective, Ajax ...
Cordova training - Day 2 Introduction to CSS 3
Advanced CSS Troubleshooting
Embrace the Mullet: CSS is the 'Party in the Back' (a CSS How-to)
HTML and CSS Coding Standards
Advanced CSS Troubleshooting & Efficiency
Cssbestpracticesjstyleguidejandtips 150830184202-lva1-app6892
Pfnp slides
Maintainable CSS
Css Introduction
Ad

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Advanced Soft Computing BINUS July 2025.pdf
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Sensors and Actuators in IoT Systems using pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Advanced IT Governance
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
KodekX | Application Modernization Development
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Empathic Computing: Creating Shared Understanding
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Reach Out and Touch Someone: Haptics and Empathic Computing
Dropbox Q2 2025 Financial Results & Investor Presentation
Advanced Soft Computing BINUS July 2025.pdf
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
GamePlan Trading System Review: Professional Trader's Honest Take
Sensors and Actuators in IoT Systems using pdf
Understanding_Digital_Forensics_Presentation.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Advanced IT Governance
Chapter 3 Spatial Domain Image Processing.pdf
Electronic commerce courselecture one. Pdf
KodekX | Application Modernization Development
NewMind AI Monthly Chronicles - July 2025
Empathic Computing: Creating Shared Understanding
CIFDAQ's Market Insight: SEC Turns Pro Crypto
“AI and Expert System Decision Support & Business Intelligence Systems”
Review of recent advances in non-invasive hemoglobin estimation
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows

Structuring your CSS for maintainability: rules and guile lines to write CSS

  • 1. Structuring your CSS for maintainability Class-9: Rules and guile lines to write CSS Sanjoy K. Paul Lead Application Developer | Software Architect | DevOps | Trainer www.SanjoyPaul.com | [email protected] | +880-1511-927992
  • 2. HTML or CSS First?
  • 3. KISS - Keep It Simple & Short(hand). DRY - Don’t Repeat Yourself. IMP - Important! is not that important. - Make It Readable - Keep It Consistent - Use a Reset TDS - Top-Down Structure - Comment is the savier - No Inline
  • 4. Organizing your CSS As you start work on larger stylesheets and big project with a team, you will discover that maintaining a huge css file can be challenging. So, we will go through some best practices for writing css that will help us to maintain the css project easily.
  • 5. How to keep your stylesheet organized and tidy?
  • 6. Does our project have a coding style guide? - If we are working in a team or existing project? then - First thing to check is whether the project has an existing style guide for CSS? - The team style guide should always win over your own personal preferences. - There often isn't a right or wrong way to do things, but consistency is important.
  • 7. Keep it consistent - set the rules for the project or are working alone, - then the most important thing to do is to keep things consistent. - Consistency can be applied in all sorts of ways, - Using the same naming conventions for classes, - choosing one method of describing color, - or maintaining consistent formatting - for example will you use tabs or spaces to indent your code? If spaces, how many spaces?
  • 8. Keep it consistent Pros: - Having a set of rules you always follow - Reduces the amount of mental overhead needed when writing CSS, as some of the decisions are already made. Cons: - Sometime it’s hard to maintain the consistency if you are in a tight deadline
  • 9. Formatting readable CSS: There are a couple of ways you will see CSS formatted. Some developers put all of the rules onto a single line .box { background-color: #567895; } h2 { background-color: black; color: white; } Other developers prefer to break everything onto a new line: .box { background-color: #567895; } h2 { background-color: black; color: white; } CSS doesn't mind which one you use. I personally find it is more readable to have each property and value pair on a new line.
  • 10. Comment your CSS Adding comments to your CSS will help - Any future developer work with your CSS file - Also will help you when you come back to the project after a break. /* This is a CSS comment It can be broken onto multiple lines. */
  • 11. Comment your CSS /* || General styles */ ... /* || Typography */ ... /* || Header and Main Navigation */ ... - Add a block of comments between logical sections - It will help to locate different sections quickly when scanning through, - Or even give you something to search for to jump right into that part of the CSS. - If you use a string which won't appear in the code you can jump from section to section by searching for it. We can use || or --start an. ..end
  • 12. Comment your CSS We don't need to comment every single thing in our code, as much of it is self-explanatory. We only comment on the things where we make a particular decision for a reason. Another example: Including reference tutorial/any links as a comment. It will help us to recall in future. /** CSS Code guideline https://developer.mozilla.org/en-US/docs/MDN/Guidelines/Code_guidelines/CSS */ Example: we may have used a CSS property in a specific way to get around older browser incompatibilities. .box { background-color : red; /* fallback for older browsers that don't support gradients */ background-image : linear-gradient (to right, #ff0000, #aa0000); }
  • 13. Create logical sections in your stylesheet It is a good idea to have all of the common styling first in the stylesheet. This means all of the styles which will generally apply unless you do something special with that element. You will typically have rules set up for: ● body ● p ● h1, h2, h3, h4, h5 ● ul and ol ● The table properties ● Links Lets see some code...
  • 14. Using CSS Vendor Prefixed Some of these CSS browser prefixes have been listed below: iOS: -webkit- Safari: -webkit- Firefox: -moz- Chrome: -webkit- Opera: -o- MS/IE/Edge: -ms- Each browser have a set of specifications related to any style. This is one of the reasons why browser prefixed are implemented in order to ensure that these browsers support any of the specific features or style that we would like to implement. If we are planning to add a CSS 3 transition to any of our CSS code, then we will can use transition property and implement a vendor prefix with it. Below is the code for the same: -webkit-transition: all 1s ease; -moz-transition: all 1s ease; -ms-transition: all 1s ease; -o-transition: all 1s ease; transition: all 1s ease;
  • 15. Do Not Be Too Specific & Chain Classes article.main p.box { border: 1px solid #ccc; } .box { border: 1px solid #ccc; } .btn.submit { border: 1px solid #ccc; } .btn{ border: 1px solid #ccc; } .submit { Background: #FF0; }
  • 16. Break large stylesheets into multiple smaller ones!?? For example, you might have an online store as part of the site, with a lot of CSS used only for styling the product listings and forms needed for the store. It would make sense to have those things in a different stylesheet, only linked to on store pages. This can make it easier to keep your CSS organized, and also means that if multiple people are working on the CSS you will have fewer situations where two people need to work on the same stylesheet at once, leading to conflicts in source control.
  • 17. CSS methodologies - OOCSS (Object-Oriented CSS): Treats page elements as objects, giving all objects classes, treating the objects’ classes as single entities in style sheets, and taking it from there.. https://github.com/stubbornella/oocss/wiki - BEM (Block, Element, Modifier): Block Element Modifier is a methodology that helps you create reusable components and code sharing in front-end development. - https://css-tricks.com/bem-101/ - SMACSS (Scalable and Modular Architecture for CSS): a flexible guide to developing sites small and large. Arguably becoming one of the most useful contributions to front-end discussions in years. - http://smacss.com/ - Atomic CSS: is the approach to CSS architecture that favors small, single-purpose classes with names based on visual function. - https://acss.io/ - ITCSS - Inverted Triangle CSS: A sane, scalable, managed CSS architecture achieved with mindful CSS code organization. - https://itcss.io/
  • 18. HTML! What we do to write HTML code? - Always Declare Document Type (<!DOCTYPE html>) - Use Lowercase “Element” and “Attribute” Names. Don’t mix uppercase and lowercase (body, div, article) - Always Quote Attribute Values - Always Specify “alt” for Images (and also width and height) - Close All HTML Elements - Close Empty HTML Elements? (<meta charset="utf-8" />, required in XML and XHTML) - Never Skip the <title> Element (SEO) - Add the lang and charset attribute, both are important for SEO indexing (SEO, <html lang="en-us" />) - Meta Datas (key, description, og and ) - Avoid Long Code Lines - Spaces and Equal Signs - Blank Lines and Indentation. Don’t use it unnecessarily. - Omitting <html>, <body>, <head> tags? - Viewport - Create Your HTML First - It will be updated and continued with time..
  • 19. Summary KISS - Keep It Simple & Short(hand). DRY - Don’t Repeat Yourself. IMP - Important! is not that important. - Make It Readable - Keep It Consistent - Use a Reset?? TDS - Top-Down Structure - The Comment is the savior - No Inline - Maintain the Spacificity (*, tag, class, id) - ...
  • 20. Validating your CSS! You can always use the W3C free CSS validator to examine whether your CSS code has been organized and structured appropriately. One of the other benefits of using it is to help you find errors within your stylesheet. This will save your time to troubleshooting comparing to doing it manually. Markup Validation Tool: https://validator.w3.org/ CSS Validation Tool: https://jigsaw.w3.org/css-validator/ Browser Compatibility Testing Tool: https://app.crossbrowsertesting.com/ https://www.lambdatest.com/
  • 21. Interested to know more? - https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Organizing#create_logical_sections_in_your_stylesheet - https://developer.mozilla.org/en-US/docs/MDN/Guidelines/Code_guidelines/CSS - https://www.w3schools.com/html/html5_syntax.asp - https://www.valoremreply.com/post/5_css_methodologies/ - https://cssguidelin.es/ - https://www.sitepoint.com/optimizing-css-performance/
  • 22. Question? Sanjoy K. Paul Lead Application Developer | Software Architect | DevOps | Trainer www.SanjoyPaul.com | [email protected] | +880-1511-927992