Skip to header Skip to main navigation Skip to main content Skip to footer

Main navigation

  • Home
  • Drupal Theming
  • Web Designer In Austin
  • Drupal Development Portfolio (opens in new tab)
  • Blog
  • Videos
  • Contact
Web Designer In Austin
Designing Drupal, Defining Distinction

Optimizing Drupal 11 Theming with SMACSS-Based CSS Categorization

SMACSS Categorization in Drupal 11 Theme Structure
Alaa Haddad, professional Drupal developer based in Austin, TX   Drupal Care
  1:57 AM CDT, Sun July 27, 2025
Share

Enhance your Drupal 11 themes by effectively organizing CSS assets using SMACSS categories for improved maintainability and performance.

Understanding SMACSS Categories in Drupal 11

In Drupal 11, managing CSS assets efficiently is crucial for creating maintainable and high-performing themes. Adopting the SMACSS (Scalable and Modular Architecture for CSS) methodology allows developers to categorize stylesheets systematically, leading to cleaner code and predictable load orders. This article delves into integrating SMACSS categories within Drupal 11's .libraries.yml file, guiding you through best practices and implementation strategies.

SMACSS Categories and Their Functions

Drupal 11 leverages SMACSS to classify CSS files into specific categories, each assigned a default weight that determines the load order:

  • Base

    • Purpose: Contains foundational styles such as resets or default HTML element styling.
    • Load Order Weight: -200
    • Example:
    css:
      base:
        css/reset.css: {}
  • Layout

    • Purpose: Defines macro layouts and structural positioning, including grid systems.
    • Load Order Weight: -100
    • Example:
    css:
      layout:
        css/grid.css: {}
  • Component

    • Purpose: Styles specific, reusable UI components like buttons or sliders.
    • Load Order Weight: 0
    • Example:
    css:
      component:
        css/button.css: {}
  • State

    • Purpose: Handles styles reflecting changes in a component's state, such as hover effects or active states.
    • Load Order Weight: 100
    • Example:
    css:
      state:
        css/button-hover.css: {}
  • Theme

    • Purpose: Applies purely visual styling that defines the look and feel of components.
    • Load Order Weight: 200
    • Example:
    css:
      theme:
        css/colors.css: {}

By organizing your CSS files into these categories within the .libraries.yml file, Drupal ensures they are loaded in a logical sequence, enhancing both maintainability and performance. [Source]

Implementing SMACSS in Drupal 11's .libraries.yml

To integrate SMACSS categories into your Drupal 11 theme:

  1. Define the Library in .libraries.yml:

    Create or update your theme's .libraries.yml file to include CSS assets categorized by SMACSS principles.

    Example:

    global-styling:
      css:
        base:
          css/reset.css: {}
        layout:
          css/layout.css: {}
        component:
          css/button.css: {}
        state:
          css/button-hover.css: {}
        theme:
          css/colors.css: {}
  2. Attach the Library in .info.yml:

    Reference the defined library in your theme's .info.yml file to ensure it's loaded globally.

    Example:

    name: 'My Custom Theme'
    type: theme
    description: 'A custom Drupal 11 theme implementing SMACSS categorization.'
    package: Custom
    core_version_requirement: ^11
    libraries:
      - mytheme/global-styling
  3. Attach Libraries Conditionally:

    For page-specific or component-specific styles, attach libraries conditionally using #attached in preprocess functions or within Twig templates.

    In a Preprocess Function:

    function mytheme_preprocess_node(array &$variables) {
      if ($variables['node']->getType() == 'article') {
        $variables['#attached']['library'][] = 'mytheme/article-styling';
      }
    }

    In a Twig Template:

    {{ attach_library('mytheme/article-styling') }}

This approach ensures that only the necessary CSS is loaded for each page or component, optimizing performance.

Drupal Services

Footer menu

  • About
  • Privacy Policy
  • Terms & Conditions
  • Flash Web Center, LLC (opens in new tab)
  • Drupal Care (opens in new tab)
  • Log in
  • Contact

Copyright © 2026 Flash Web Center, LLC | All rights reserved

Developed & Designed by Alaa Haddad