Front-end developers actively use CSS pre- and post-processors these days. They allow writing readable and clean code. The most popular pre-processors are Sass, Less, and Stylus, while PostCSS is the first post-processor that comes to mind.
In this article, we would like to focus on the particular features of these tools. You will know their main advantages for web applications development. The main idea is to show you how CSS processors can help programmers reduce the amount of written code for their projects, increase productivity, and build complex web applications.
So let’s see what CSS can offer us today.
Sass: CSS with superpowers
You have probably heard the term Sass floating around, but what exactly does it mean? Let’s see what the official website of this CSS extension tells us.
Sass is a stylesheet language that’s compiled to CSS. It allows you to use variables, nested rules, mixins, functions, and more, all with a fully CSS-compatible syntax. Sass helps keep large stylesheets well-organized and makes it easy to share design within and across projects.
Today we would like to focus on the most interesting and rarely used tools called loops and map functions. These are the very instruments that help us get the most out of CSS.
The SCSS is known as an extension of CSS syntax. At Codica, we often use it while building web applications as it is closer to native CSS and doesn’t require additional operations when copying your code from a browser.
Below you can see a code snippet that shows us how Web Fonts are usually declared in CSS. This is a perfect example of code repetition in style sheets.
At the sight of the code snippet above, we get the impression of dealing with duplicate code.
This situation calls to mind a scene from the Matrix movie when Neo spots a black cat walking by which was followed by another black cat behaving exactly the same way. That was a visual illustration of déjà vu during the Hotel Ambush.
When we face code duplication, such Sass components as map functions and mixins can save the day.
Below you can see an example of how these tools can help web app developers simplify their code.
For starters, let’s describe our fonts in map fonts.
Now we declare variables font using the map-get function:
The feature @mixin allows us to avoid code duplication:
We declare font-face for each font with the help of iterator @each and previously mentioned @mixin:
Now in order to add/delete a new font, we need to upload/delete font files in the folder which lies in the path declared in variable
'../assets/fonts/' in the example). It is worth noting that we should follow file naming, declared in map $fonts and declare/delete variable:
All features and tools mentioned above make CSS the integral part of your project stylization.
In their turn, its preprocessors including Sass enable web app developers to write clean and efficient code which is easy to maintain. For many complex web projects, it is the most important requirement.
Subscribe to our blog to get valuable insights from our experts.
The usage of PostCSS plugin called Preset Env and CSS Modules is a matter of taste. The plugin lets a web application developer transform modern CSS so that it could be easily understood by most browsers. At the same time, CSS Modules help resolve the issue of the so-called global scope in CSS.
In their turn, code linting and adding vendor prefixes to support cross-browser compatibility are essential for building web applications of any complexity.
Why use Post-processors in web applications development?
The cool thing about post-processors is that they streamline the web app development process, increase code readability, and help solve issues front-end developers face, including cross-browser compatibility. If we want to add support for some properties, we can use @mixin to add prefixes to them. For example, here we add mixins to the transition property which allows us to declare reusable styles thus avoiding code duplication:
Related reading: Vue vs React: Choosing the Best Tool for Your Project in 2019
However, this approach has a downside too. It adds prefixes to all browsers even if your end-users get along without them.
That’s when PostCSS with plugin Autoprefixer saves the day. This plugin helps automatically add prefixes to all browsers in CSS, allowing to avoid code duplication and reduce the number of manual tasks, as a result saving web application developers a lot of time.
Let’s talk about Autoprefixer and other plugins in more detail.
PostCSS plugins and tools
GitHub offers the following description of Autoprefixer:
PostCSS plugin to parse CSS and add vendor prefixes to CSS rules using values from Can I Use. It is recommended by Google and used in Twitter and Alibaba.
They also mention that “Autoprefixer uses Browserslist, so you can specify the browsers you want to target in your project with queries like > 5%”.
This approach fixes the cross-browser compatibility issue, however partially. We use global statistics that can differ in various regions of the world.
Let’s see what Autoprefixer developers mention in this regard on GitHub:
If you want to change the default set of browsers we recommend to combine last 1 version, not dead with > 0.2% (or > 1% in the US, > 1% in my stats). last n versions adds too many dead browsers and does not add popular old versions. Choosing a percentage above 0.2% will, in the long run, make popular browsers even more popular. We might run into a monopoly and stagnation situation, as we had with Internet Explorer 6. Please use this setting with caution.
Opera Mini has 100 million users in Africa and it is more popular in the global market than Microsoft Edge. Chinese QQ Browsers has more market share than Firefox and desktop Safari combined.
You may also like: 4 Useful Git Commands That Will Make Your Life Easier
Theoretically, we should always cater to an end-user and Browserslist gives us such an opportunity thanks to Browserslist-ga. This package collects statistics from Google Analytics in the JSON file which is used in Browserslist.
On the Browserlist-ga page, we can see the following guide to using this tool:
“In the root directory of your project run:
(npx comes with npm 5.2+, for older versions run
npm install -g browserslist-ga and then
(to run the latest code directly from GitHub, execute
npx github:browserslist/browserslist-ga instead)
You will be asked to log in with your Google Account. Your access token will only be used locally to generate a
browserslist-stats.json file in the root of your project. After finishing the steps, you can use your stats with Browserlist by adding the following to your Browserslist config:
> 0.5% in my stats # Or a different percentage
Note that you can query against your custom usage data while also querying against global or regional data. For example, the query
> 1% in my stats, > 5% in the US, 10% is permitted”.
As a result, we get “clean” CSS with browser support, based on the statistics of visits to our site.
As it is stated on Browserlist-ga page:
All the praise goes to the humans and martians that develop and maintain Can I Use and Browserslist.
CSSNext is another PostCSS plugin that is worth looking at. It allows using the latest CSS syntax, for example, new custom properties without worrying about browser support.
The last but not least plugin that we will discuss in this article is Stylelint. It is a CSS linter for proofreading and validating the CSS code. It helps you avoid errors and follow consistent coding conventions.
CSS pre- and post-processors are very popular technologies. Their promise of clean code, simplified web application development workflow and, as a result, increased productivity is very tempting.
By preparing this guide we set a goal to show you how CSS Processors help us develop custom software products of any complexity for our clients. We hope that examples from our practice demonstrate all the power of the tools at your hand.
Check our other articles to get more tips on adopting the latest web app development techniques.