TECH

Vol.58

author

Engineer

M.S.

Preventing CSS Bloat: Sustainable Code Architecture and Introducing Sass

#WEB#AI#programming#開発
When developing large-scale websites, as opposed to small websites or single-page projects, have you ever experienced CSS becoming bloated and the overall structure eventually falling apart without realizing it? This time, we’ll introduce key points for building an efficient workflow aimed at keeping CSS as minimal and maintainable as possible.
stuffstuff

Five Key Points for Preventing CSS Bloat

  • Make Use of Sass
  • Consider CSS Specificity
  • Standardize and Group Styles
  • Split Files Using SCSS
  • Class Naming Conventions

Using Sass

What Is Sass?

Sass is a “meta-language” that extends the CSS language.
Based on standard CSS syntax, it adds various features that allow you to build code in a way that feels closer to programming. Depending on how it is written, Sass offers a great deal of flexibility, making it possible to significantly improve readability, maintainability, and scalability through thoughtful structuring.

There are several CSS meta-languages besides Sass, such as Less, but the “SCSS syntax” used in Sass is particularly easy to understand.
Because it follows standard CSS writing conventions, it feels more like an extension of CSS with additional functionality, allowing developers to write styles intuitively without departing far from familiar CSS syntax.

What You Can Do with Sass

Here are some of the Sass features that are most commonly used.

  • Variables
    You can store values as variables.
// Variables
$color1 : #ffffff;

.hoge {
  color : $color1;
}
  • Nesting
    You can write properties in a nested structure.
// Nesting
body {
  nav {
    ul {
      li {
        color : #ffffff;
      }
    }
  }
}
  • @mixin
    You can use arguments within reusable style definitions.
// Mixin
@mixin font($fontColor:#ffffff) {
  color : $fontColor;
}

.hoge {
  @include font;
}
  • @extend
    You can inherit styles from another selector.
// Extend
.box {
  width : 300px;
  height : 240px;
}

.hoge {
  @extend .box;
  display : inline-block;
}

Be Careful with CSS Specificity

CSS Specificity

CSS has a concept called specificity. The more selectors are nested, the higher the specificity of the rule becomes.
Care must be taken to avoid excessive nesting, which can result in highly specific rules conflicting with each other and becoming difficult to override.

The following three points are especially important to keep in mind.

  • !important
    This forces a rule to take top priority regardless of normal specificity rules.
    In responsive design, for example, styles are often overridden using media queries. However, once !important is introduced, overriding those styles may require using !important again, potentially combined with even higher specificity. Depending on the structure, this can lead to extremely difficult-to-maintain code.
  • Inline CSS in HTML
    Nowadays, CSS is usually managed in separate stylesheet files.
    Inline CSS written directly in HTML takes priority over external stylesheet rules. As a result, overriding inline styles often requires using !important, which can trigger the same maintenance problems mentioned above.
    It also makes responsive site development more difficult because styles become harder to override. Unless there is a very specific reason, it is best to avoid inline CSS.
  • ID Selectors
    ID selectors have stronger specificity than class selectors.
    Depending on the site architecture, situations may arise later where unexpected additions or reusable components require styles to be converted from IDs to classes.
    It is recommended to establish clear rules for how IDs are used during the early stages of development and avoid using them beyond those defined purposes.

Avoid Excessively Deep Nesting (Nested Selectors)

When using nesting, it is easy to write styles as though you are grouping content structures together, but this often leads to unnecessary selectors being added.
As a result, even longer selectors are eventually needed to override those styles, creating CSS that becomes extremely difficult to maintain and read.

It is a good idea to establish a rule such as limiting nesting to no more than three levels.

.contents1{
  .box{
    ul{
      li{
        a{
          font-size : 12px;
          color : #ff0000;
        }
      }
    }
  }
}

Standardize and Reuse Style Designs

Use the Same Classes and Styles Repeatedly

For sections that share the same layout, try turning them into reusable components so that the same CSS classes and styles can be shared across multiple areas.
“Componentization” simply means treating parts of the UI as reusable building blocks.

As shown in the earlier “bad nesting example,” if styles are written in a way that only works in one specific place, you will have to rewrite the same styles again whenever you want to apply them elsewhere.
By looking at the overall site structure and turning common styles into reusable components, you can avoid duplicating the same code repeatedly.

Be Careful Not to Over-Generalize

On the other hand, overusing shared classes just because elements “look similar” can actually increase the amount of code needed for overrides and resets in areas that are not truly shared.
This becomes even more important in responsive design.
A good approach is to separate only the truly common properties into shared styles, and then use techniques such as @extend appropriately to balance reuse and separation.

Splitting SCSS Files

The goal of preventing CSS bloat is to keep the final compiled CSS as minimal as possible. However, if the files you work on during development become bloated themselves, that defeats the purpose.
To avoid this, SCSS files should be split into separate modules and then imported into a master SCSS file.

reset.scss
animation.scss
responsive.scss

In this example, the files are separated into categories such as “reset styles,” “animations,” and “responsive styles.”
By splitting editing files into smaller parts, each file contains less code, making the structure easier to understand.
It also helps prevent issues such as slow rendering or sluggish editor performance when working with very large files.
By importing them into a master file such as style.scss, everything can ultimately be compiled into a single CSS file.

// How to import external SCSS files
// (Do not include the .scss extension)

@import 'reset', 'animation', 'responsive';

About Class Naming Conventions

There are several different naming conventions for class names. The following three are among the most commonly used.

Camel Case mainTitle
Snake Case main_title
Pascal Case MainTitle

Personally, I do not think there is a major difference between these naming conventions.
Camel case and Pascal case tend to prioritize brevity, while snake case emphasizes readability.
It is also common to choose a naming convention based on the language being used alongside CSS (such as JavaScript), or to follow the conventions that a development team is already familiar with in order to maintain consistency.

About camel case, snake case, and Pascal case
“Variable Naming Conventions” You Might Be Too Afraid to Ask About

Fundamental Concepts Behind CSS Coding Rules

There are several different approaches to naming conventions and CSS architecture.
Using these concepts as a foundation can help when designing CSS structure and preventing CSS bloat.
A detailed explanation is beyond the scope of this article, but here are some representative methodologies and their key concepts.

  • BEM (Block Element Modifier)
    Classes are divided into three categories: Block, Element, and Modifier.
    Class names tend to become longer, but it becomes much easier to understand where a style belongs.
    Example: block__element-modifier
  • SMACSS (Scalable and Modular Architecture for CSS)
    Styles are designed using five categories: Base, Layout, Module, State, and Theme.
    This methodology is known for its strong maintainability through systematic organization.
  • OOCSS (Object Oriented CSS)
    CSS is named and structured using object-oriented concepts.
    The idea is to separate structure from visual appearance when writing styles.

About OOCSS
Understand the Basics of OOCSS — A CSS Design Methodology You Can Apply Right Away

Summary

What did you think?
This time, we focused mainly on Sass and introduced key points for preventing CSS bloat. However, even when you understand the causes and solutions, applying them effectively in real projects is not always easy — and I am still learning through trial and error myself.
In the next article, we will begin by introducing how to install and set up Sass, which is one of the most important tools discussed in this TIPS series, so that these practices can be applied more effectively.
I hope to continue implementing these ideas one by one in order to create CSS that is both easier to read and easier to use.

PREV
Vol.57 Installing Ubuntu MATE on a Ras…
NEXT
Vol.59Improving UI Design Quality Thro…

MORE FOR YOU