The landscape of Drupal theme development has drastically changed in recent years. Cutting-edge front-end development tools are now considered standard within Drupal enterprise builds.
Developers now can leverage engineering recipes into rapid development with powerful front-end automation tools like Gulp and Sass. But with these tools comes the responsibility to address performance and extensible methods within large teams. So a consistent and scalable method of development is needed. By following the techniques recommended in this blog, you will see an increase in theming velocity throughout the lifecycle of your project.
When you step back and look holistically at your Drupal homepage, you'll see that it is essentially nothing more than a collection of several modular pieces. So the primary focus of this method in Sass â which is an extension of CSS that adds power and elegance to the basic language â revolves around developing your site as these modular pieces.
The construction of the varied isolated pieces are intended to exist as stand-alone components, but they are also designed to be combined and extended upon. This means steering away from the tendency to couple your styles to Drupal-rendered HTML markup. If you follow this same train of thought, this means that these elements should be coded in a manner so they maintain their stylistic integrity regardless of location on the page. Teams following this pattern are accelerating their development efforts by treating theming tasks as building blocks to be extended upon.
One of the primary driving factors behind component-driven development is the DRY (Don't Repeat Yourself) principle. Reducing the compiled CSS will result in improved performance and less code to maintain. Your theme will also become more predictable by removing layout code that's specific to containers. Following the component methodology builds consistency by moving common styles to base HTML elements so they can be reused throughout components. This leads to a more consistent rate of development with each sprint life cycle since momentum will be accruing as your component count increases.
To appreciate the timing of this workflow consider these concepts in a linear timeline:
In the following example, something as simple as nesting the second class 'views-row' results in twice as much rendered CSS:
.block-custom, .views-row { width: 98.5%; display: block; border-radius: 5px; span { font-weight: bold; font-weight: 500; }}
While we have extensive power with available front-end tools, it's important to remember that our front-end methods still need to catch up with our front-end tools. Using advanced nested selectors in Sass is great, but the flip side may result in not allowing your code to be extensible or even easily maintainable.
A basis for thinking through scenarios with Drupal components requires a proactive approach to Drupal markup and its classes when writing Sass. This means dictating to both the classes and markup, not just reacting to it. So let's extend this thinking a bit further by considering two opposing methods: reacting to classes versus defining classes.
If we look at this example of reacting to classes, you will see why this methods quickly fails. By essentially chasing down the markup, you're creating coupled code that is both un-extendable and counterintuitive.
aside form.login-form input.edit-name,div.view-views-articles.view-display-id-block .views-row,div.panel-example-pane > .inside-content h2,div.view-taxonomy-term .header .icon { color: $greenish; }
By proactively defining classes with a combination of preprocessing and Sass, you will establish a repeatable baseline to extend upon. This same technique can just as easily be applied with things like view modes, custom theme functions and other Drupal standards.
// hook_form_alterfunction MODULENAME_form_alter(&$form, &$form_state, $form_id) { if ($form_id == 'user_login') { $form['name']['#attributes']['class'][] = 'green'; }}// theme_preprocess_views_view_unformattedfunction THEMENAME_preprocess_views_view_unformatted(&$vars) { $rows = $vars['rows']; if ($vars['view']->name == 'articles') { foreach ($rows as $id => $row) { $vars['classes'][$id][] = 'green'; $vars['classes_array'][$id] = implode(' ', $vars['classes'][$id]); }; }}// sass file.green { color: $greenish;}
Nicole Sullivan introduced the guiding principles of Object-Oriented CSS (OOCSS) in 2008. The OOCSS methodology was introduced before Sass had even hit its full stride, so with all the modern-day tools it is even more relevant. Since its introduction, it has stood out as one of the most solid modular systems for building enterprise-grade sites.
The value of OOCSS can be nicely summed up by Sullivan herself: 'The purpose of OOCSS is to encourage code reuse and, ultimately, faster and more efficient stylesheets that are easier to add to and maintain.'
The two primary principles of OOCSS are:
In this example, you would separate this code out into two different blocks. The first three lines would go in a class designated for structure, and the last three lines would be moved to a class for skin.
// Separate Structure & Skin.sideblock { padding: 10px; min-height: 300px; width: 100%; border: solid 1px gold; box-shadow: rgba(0, 0, 0, .5) 2px 2px 5px; background: linear-gradient(#ccc, blue);}
In the following code, the requirement is that a h2 needs to reside within the footer and not offering flexibility. A better way would be to use a .header-tag
class that could live on any type of element regardless of DOM structure.
// Separate Container & Contentfooter h2 { font-family: Helvetica, sans-serif; font-size: .8em; color: purple; text-shadow: rgba(0, 0, 0, .3) 3px 3px 6px; line-height: 1;}
When scoping out the theming of a large Drupal project, step back and look at the sequential steps of development. Whether you're dealing with individual components or entire pages, you can still break out things in re-usable patterns. The intent then is to build a collection on individual pieces to be combined as needed. This means scaling down your Sass into smaller segments to be used in multiple scenarios based on the purpose of the @extend
or @include
.
Welcome FolksMuffin ice cream sweet macaroon jelly-o bear claw donut bonbon. Dessert caramels cupcake pie oat cake. Jelly-o cupcake caramels..text-white { text-shadow: 0 0px 5px black; color: $whiteish;}.tree-background { @extend %tree-image-full;}.golden-block { border: 2px solid gold;}
Component-based Drupal takes a theoretical and practical look at advanced Drupal engineering techniques to speed up development time for enterprise projects. Hopefully these recommended steps will increase velocity and cut development time after each sprint life cycle.
If this content did not answer your questions, try searching or contacting our support team for further assistance.
Fri Sep 12 2025 07:20:58 GMT+0000 (Coordinated Universal Time)