DEV Community

Arhan Khan
Arhan Khan

Posted on

Sass for Beginners: CSS With Variables, Nesting, and Superpowers

Sass is the CSS preprocessor that most professional front-end teams use. It is CSS with the features CSS was missing for years: variables, nesting, mixins, functions, and partials. You write .scss files, Sass compiles them to plain CSS, and your stylesheets become dramatically easier to maintain.

The difference shows up the moment a project grows. In plain CSS, a colour used in forty places is forty chances to make a typo. In Sass, it is one variable. In plain CSS, a component's styles scatter across a file. In Sass, they nest inside one block that mirrors your HTML.

npm install -D sass
npx sass src/main.scss dist/main.css
Enter fullscreen mode Exit fullscreen mode
$primary: #2563eb;
$radius: 8px;

@mixin card {
  border-radius: $radius;
  box-shadow: 0 2px 8px rgb(0 0 0 / 0.1);
  padding: 1rem;
}

.profile-card {
  @include card;
  background: white;

  h2 {
    color: $primary;
    margin: 0 0 0.5rem;
  }

  p {
    color: #555;
  }
}
Enter fullscreen mode Exit fullscreen mode

Sass is a great fit for any project with more than a handful of styles — design systems, large apps, and teams that share a codebase. Its partials (_variables.scss, _buttons.scss) keep styles organized, and its functions handle colour math and responsive breakpoints cleanly.

The limits are shrinking as CSS catches up: modern CSS now has variables, nesting, and container queries. For a tiny project, plain CSS may be enough, and adding a build step for one stylesheet is overkill. But Sass still offers mixins, functions, and imports that plain CSS cannot match.

If your stylesheets are getting hard to maintain, try Sass for one component this week. The moment you change a colour in one place and watch it update everywhere, you will not go back.

Top comments (0)