
CSS Vendor Prefixes and Autoprefixer
08/05/2024
Responsive Website CSS Practice Projects
10/05/2024
CSS preprocessors
CSS was designed to be simple. Features such as conditionals and structured nesting were historically difficult or unavailable, so CSS preprocessors were created to help with those and other tasks.
A preprocessor adds a richer syntax that can make a stylesheet easier to maintain. Its files resemble CSS, but you compile them to produce a regular stylesheet that a browser can read.
Run the compiler from the command line or from a task runner such as Gulp or Grunt. Less and Sass are two widely used preprocessors.
Less and Sass
One early advantage of Less and Sass was the ability to define and reuse variables, which helps keep a design consistent. Change a single variable and the corresponding color changes wherever the compiled stylesheet uses it. Native CSS custom properties solve a similar problem directly in the browser. You can also change them at runtime without recompiling.
Preprocessors support nesting, so a related selector can sit inside its parent rule. Instead of repeating the full selector in a separate rule, group the rules where they belong. This can make the stylesheet easier to read and maintain. Native CSS now also supports nesting, with syntax that differs in some cases from Sass or Less.
Preprocessors can also use mixins, which are reusable groups of rules. For instance, define a mixin for a transform and include it on any element that needs that effect. Each example below shows the source you would write in Sass.
Sass examples
Variables
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body {
font: $font-stack;
color: $primary-color;
}
Nesting
nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}
li {
display: inline-block;
}
}
Operators
article {
float: left;
width: 600px / 960px * 100%;
}
aside {
float: right;
width: 300px / 960px * 100%;
}
Mixins
@mixin transform($property) {
-webkit-transform: $property;
-ms-transform: $property;
transform: $property;
}
.box {
@include transform(rotate(30deg));
}
Learn more about Sass and Less from these resources:
- Read the Eniun Sass tutorial.
- Explore the Less documentation.
For years, Sass and Less filled gaps in CSS. As CSS evolved, the language gained several capabilities that once required a preprocessor:
- Custom properties, such as
--primary-color. - Native nesting, including the
&nesting selector when needed. - Functions such as
calc(),clamp(),min(), andmax(). - Functional selectors such as
:is(),:where(), and:has(). - Tools such as PostCSS can also process modern CSS without introducing a different stylesheet language.
Many new projects, including sites built with React, Vue, or Svelte, can use native CSS for most of their styling. An established preprocessor may still suit a project or a team that already relies on it.
Sass remains useful in several situations:
- Large projects with existing Sass code.
- Teams that maintain a library of Sass mixins or functions.
- Frameworks or libraries that distribute Sass sources, including some Bootstrap versions.