SC header logo

    Clean SCSS

    As developers, it is crucial to prioritize the creation of clean, maintainable, and well-documented code. By adhering to established methodologies, principles, conventions, and architecture, we can ensure that our CSS code is clean, simple, and easy to maintain. In this blog post, we will explore some essential practices for writing clean SCSS code.

    Author

    Matej

    CategoryDevelopment
    Last updated

    17.02.2025.

    Utilizing Best Practices

    To achieve clean code, we are using the following best practices:

    1. DRY (Don't Repeat Yourself): The DRY principle in software development emphasizes the reduction of code repetition. By avoiding duplications, we can improve maintainability and reduce the chances of introducing bugs.

    2. 7-1 Sass Architecture: The 7-1 Sass Architecture is a widely used approach that organizes CSS files into seven folders and one main file. This structure helps to effectively sort and structure the codebase, making it easier to navigate and maintain.

    3. BEM (Block Element Modifier): BEM is a naming convention and methodology that promotes clear, self-documenting CSS code. By defining blocks, elements, and modifiers, we can create meaningful class names that improve code readability and maintainability.

    4. Careful Namespacing: Careful namespacing involves choosing appropriate and descriptive names for CSS classes. By using prefixes such as 'u-' for utility classes, 't-' for theme-related classes, and 'is-' or 'has-' for temporary or state-based classes, we can enhance code clarity and avoid naming conflicts.

    5. "Outside In" Ordering of CSS Properties: The "Outside In" approach suggests ordering CSS properties based on their impact on the element from the outside to the inside. Following this order, we prioritize layout properties, box model properties, visual properties, typography properties, and miscellaneous properties. This ordering improves code readability and makes it easier to locate specific properties.

    guy coding on a laptop

    Applying the Best Practices

    To demonstrate the application of these best practices, let's consider an example component. Keep in mind that this example is not designed or responsive and may not adhere to best coding practices.

    Example Component:

    HTML file:

    <div id=”accordion” class="accordion">
      <h2 id=”accordion-title” class="accordion-headline">
        <button class="accordion-button”>
    <span>What is Flowbite?</span>
    <span class=" icon”>X</span>
        </button>
      </h2>
      <div id="accordion-body" class="body-accordion">
        <div class="body-accordion-wrapper">
          <p class="">Content part 1 …</p>
          <p class="-">Content part 2 …
            <a href="https://example" class="link">Link Example</a>
          </p>
        </div>
      </div>
    </div>

    SCSS file:

    .accordion {
      background-color: black;
      color: white;
      border: 1px solid green;
      margin: 5rem auto;
      max-width: 50%;
    }
    .accordion-title {
      color: red;
      pointer: cursor;
      margin-right: 1rem;
      padding: 2rem;
    }
    .accordion-button {
      font-size: 1.5rem;
      justify-content: space-between;
      align-content: center;
      display: flex;
      .icon {
        font-size: 1.25rem;
        height: 1.5rem;
        width: 1.5rem;
      }
    }
    .accordion-body {
      overflow: hidden;
      max-height: 0;
      .is-open-accordion & {
        max-height: 100%;
      }
    }
    .accordion-content {
      padding: 2rem;
    }

    Improvement Steps

    1. Replace IDs with classes: Instead of using IDs for element selection, it is better to use classes with a 'js-' prefix to indicate their purpose for JavaScript functionality. This approach promotes separation of concerns and improves code maintainability.

    2. Use BEM structure and naming convention: Apply BEM to define blocks and elements, taking advantage of SCSS nesting for improved readability and easy design adjustments. Ensure that the SCSS elements are ordered according to their importance and appearance.

    3. Extract reusable components: Identify frequently used components and extract them into separate SCSS files. For example, a button component can be defined and styled independently, making it reusable throughout the project.

    4. Improve namespacing: Add appropriate prefixes to classes based on their nature. Use 'c-' for components, 'u-' for utility classes, and follow BEM's naming convention for blocks, elements, and modifiers.

    5. Apply the "Outside In" approach: Order the CSS properties within each class based on the "Outside In" principle, starting with layout properties, followed by box model properties, visual properties, typography properties, and miscellaneous properties. This ordering enhances readability and makes it easier to locate specific properties.

    Result of Applying Best Practices

    After applying these best practices, the code will look more professional and maintainable:

    HTML file:

    <div class="accordion">
      <h2 class="accordion__headline" data-accordion-heading>
        <button class="accordion__button">
          <span>What is Flowbite?</span>
          <span class="accordion__icon">X</span>
        </button>
      </h2>
      <div class="accordion__body" data-accordion-body>
        <div class="accordion__content">
          <p>Content part 1 …</p>
          <p>Content part 2 …
            <a href="https://example" class="accordion__link">Link Example</a>
          </p>
        </div>
      </div>
    </div>

    SCSS file:

    .accordion {
      &__headline {
        margin-right: 1rem;
        padding: 2rem;
        color: red;
        cursor: pointer;
      }
      &__button {
        font-size: 1.5rem;
        justify-content: space-between;
        align-content: center;
        display: flex;
        &__icon {
          font-size: 1.25rem;
          height: 1.5rem;
          width: 1.5rem;
        }
      }
      &__body {
        overflow: hidden;
        max-height: 0;
        &.is-open-accordion {
          max-height: 100%;
        }
      }
      &__content {
        padding: 2rem;
      }
    }

    Conclusion

    Writing clean CSS code brings numerous benefits to developers and clients alike. By adopting best practices and focusing on maintainability and efficiency, developers can work more effectively and deliver high-quality results. Prioritizing clean code reduces the likelihood of bugs, improves code readability, and promotes code reuse. Ultimately, these practices lead to satisfied clients and a positive development experience.

    Articles You Might Like

    https://pub-1a00a711c8764c34a12f638f11e520ad.r2.dev/articles/svelte-5.jpg
    Svelte 5 - A magical revolution
    Development
    August 26, 2024

    Explore how Svelte 5 revolutionizes web development with runes and enhanced reactivity for faster apps....

    renato

    Renato,

    JavaScript Lead

    https://pub-1a00a711c8764c34a12f638f11e520ad.r2.dev/articles/website-hacker.jpg
    Why Websites Are Hacked and How to Protect Yours
    Development
    August 07, 2024

    Learn why websites get hacked and how to protect yours with practical security measures and best practices....

    robert

    Robert,

    CEO

    https://pub-1a00a711c8764c34a12f638f11e520ad.r2.dev/articles/computer-code-editor.jpg
    Popular Node.js Backend Frameworks in 2024
    Development
    July 24, 2024

    Wondering what the top Node.js frameworks in 2024 are? Read on to discover the best options for your project!...

    robert

    Robert,

    CEO

    https://pub-1a00a711c8764c34a12f638f11e520ad.r2.dev/articles/frustrated-person.jpg
    Speed Up Your Website in 10 Easy Steps
    Development
    July 16, 2024

    Slow website costing you customers? Discover 10 practical ways to enhance your site's performance and keep visitors engaged....

    robert

    Robert,

    CEO

    https://pub-1a00a711c8764c34a12f638f11e520ad.r2.dev/articles/recaptcha-feature-2.png
    How to get Google reCAPTCHA keys
    Development
    May 21, 2024

    It is a free service provided by Google to prevent bots from abusing websites.
If you have a form on your website and the user needs to fill in data, it is always a good idea to implement some kind of detection between bots and real users....

    matej

    Matej,

    Software Developer

    https://pub-1a00a711c8764c34a12f638f11e520ad.r2.dev/articles/pexels-eye4dtail-134402-2.jpg
    WordPress vs TYPO3 vs Craft CMS
    Development
    April 19, 2024

    In this article, we compare WordPress, TYPO3, and Craft CMS - platforms that allow you to manage and customize your website. We help you decide which CMS is the most suitable for your business and use case....

    bozidar

    Bozidar,

    Software Developer

    https://pub-1a00a711c8764c34a12f638f11e520ad.r2.dev/articles/scaling-images.jpg
    The Ultimate Guide to Optimizing Images for Your Website
    Development
    March 29, 2024

    This comprehensive guide will combine insights from leading sources and our expertise to help you master image optimization for your website....

    iia

    Iia,

    Software Developer

    Client CTA background image

    A new project on the way? We’ve got you covered.