blog logo
thumbnail

How Preprocessors Drive Effective Front-End Design

Kenneth Hurh • February 8, 2019

Today’s feature-rich commerce experiences have a lot going on behind the scenes. When we look at a storefront or webpage on our browser, we often assume that CSS is what controls the page’s style: after all, web browsers can only understand CSS as the styling language for any DOM element being rendered.

Surprisingly, though, CSS can be very limited when code rules are not necessarily reusable and efficient; for example, when you’re not able to reuse styles in multiple selectors. The limitations of the web browser and CSS require these experiences to use preprocessors which offer an advanced way of writing CSS, extending the basic functionalities. The preprocessor code is later compiled into normal CSS which the web browser will read.

 

Preprocessors Today – Sass

There are many different preprocessors that are available nowadays. Depending on your project requirements and team capabilities, some are more valuable than others. Today, we’ll be talking about one of the more popular preprocessors: Sass. Sass – or Syntactically Awesome Style Sheets – is a language for style sheets. The most common syntax for Sass is known as SCSS or “Sassy CSS”. It is derived from CSS3’s syntax which means that every valid CSS3 style sheet is valid SCSS as well. SCSS files use the extension, “.scss”.

There are different powerful features of Sass. Some of the incredibly important features are: variable inheritance, mixins, and nesting.

 

Variable Inheritance: This feature in Sass allows users to easily maintain their style code. Sass supports four different data types: numbers, strings, colors, and Booleans.

Declaring a variable using Sass looks something like this:

        $main-color: #000;

The variable, main-color, is created using the “$” sign while giving it the value of black using the colon. Since “$main-color” is assigned the color black, we can then use “$main-color” to communicate “black” throughout the style sheet or in other files or directories that are imported.

        border-color: $main-color;

Instead of manually entering hex codes or other variables, we are able to easily make anything our main color with the variable.

 

Mixins: Mixins greatly improve reusability in your CSS code. These are small chunks of style code which can be used throughout your style sheet.

        @mixin box-style {

                    border: 1px solid red;

                    border-radius: 5px;

}

The mixin, “box-style” contains a few lines of code which can be used again and again elsewhere in your style sheet.

        @include box-style;

        padding: 10px;

This way, you don’t need to replicate the same box style code over and over again for every similar box on the site. By including mixins, the style coming from the “box-style” mixin will be applied where you decide to include it.

 

Nesting: Another feature that Sass has is nesting. Nesting allows developers to nest their CSS selectors in the same visual hierarchy of HTML. For example,

        nav {

        ul {

                    background-color: red;

                    margin: 0;

}

}

The “ul” selector is nested within the “nav” selector. This makes your style sheet more readable as you can visually see how the HTML is structured.

 

Should You Use A Preprocessor?

Whether you should use a preprocessor for your CSS and web browser all depends on the scale of the experience you’re building and the development team. Analyzing the project requirements and the workflow of the team as a whole should be considered before adopting preprocessors.

In recent years, as experiences have become more robust and complex, it has become much more common to use preprocessors in large scale projects. At the same time, though, it requires more effort from your development team to use a preprocessor since there will be additional functionality and complexity in your CSS code files.

 

CSS preprocessors add functionalities to CSS files with the intention of saving developer time and effort. With modern technologies for the web, preprocessors like Sass help developers be more creative, flexible, and efficient when creating style sheets.


Kenneth Hurh

About the author

Kenneth Hurh

Subscribe to our blog

Let's discuss the next step in your commerce journey.

XSchedule a meeting