Table of contents
1.
Introduction
2.
Getting started
3.
Setting output style
4.
Nested Sass output style
5.
Expanded Sass output style
6.
Compact Sass output style
7.
Compressed Sass output style
8.
FAQs
9.
Key Takeaways
Last Updated: Mar 27, 2024

Sass output styles

Author Hari Sapna Nair
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The sass output style specifies the CSS styling for the document structure. The sass file automatically generates a default CSS style that reflects the document structure. However, this default CSS styling is not perfect for all situations.
 

When compiling Sass code into CSS code, we can choose between four different sass output styles: nested output style, compact output style, expanded output style, and compressed output style. In this blog, we will learn the four different sass output styles. 

Getting started

We will start by creating a .scss file. In this case, we will create styles.scss. 

 

styles.scss:

div {
 background-color: black;
 color: white;
}

.topDiv {
 font-size: 2rem;
}

.middleDiv {
 font-size: 2.4rem;
}

.bottomDiv {
 font-size: 2.8rem;
}

.headline {
 text-decoration: underline;
 font-size: 3em;
 color: yellow;
}

 

After creating the styles.scss file, we must compile this file into CSS with any of the four different output style options mentioned above. Let's see how this is done.

Setting output style

The various ways of setting the sass output styles are as follows:-

  • GUI(Graphical User Interface) tools like CodeKit or LiveReload, have options to set the Sass output style. 
  • We can set output style via the command line by passing the --style flag in the setting.
     

Example:

sass --watch style.scss:style.css --style compact

 

This will set the sass output style as compact.

  • While using gulp or grunt task runners with the sass package, we can pass the style option to the sass config.

 

Example:

gulp.task('sass', function () {
 gulp.src('scss/style.scss')
     .pipe(sass(outputStyle: 'expanded'))
     .pipe(gulp.dest('css'))
});
You can also try this code with Online Javascript Compiler
Run Code

 

This will set the sass output style as expanded.

Nested Sass output style

The nested output style reflects the structure of the CSS styles in which every property takes its own line, and the indentation of each rule is based on its nested depth.

 

The nested output style is handy while dealing with large CSS files. It makes the file structure more readable and understandable.
 

To set the CSS output to nested output style, we can use this command:

sass --watch styles.scss:styles.css --style nested

 

Example:

div {
 background-color: black;
 color: white; }

.topDiv {
 font-size: 2rem; }

.middleDiv {
 font-size: 2.4rem; }

.bottomDiv {
 font-size: 2.8rem; }

.headline {
 text-decoration: underline;
 font-size: 3em;
 color: yellow; }

Expanded Sass output style

The expanded output style takes more space compared to the nested output style. Each property of the expanded output style has its own line. The Rules section consists of properties, all of which are intended within the rules, whereas rules do not follow any indentation. This output style looks like most human-made style sheets. 
 

To set the CSS output to nested output style, we can use this command:

sass --watch styles.scss:styles.css --style expanded

 

Example:

div {
 background-color: black;
 color: white;
}

.topDiv {
 font-size: 2rem;
}

.middleDiv {
 font-size: 2.4rem;
}

.bottomDiv {
 font-size: 2.8rem;
}

.headline {
 text-decoration: underline;
 font-size: 3em;
 color: yellow;
}

Compact Sass output style

The compact output style takes less space than the expanded and nested output styles. Here the main focus is on selectors rather than their properties. Each selector takes up one line, and its properties are also placed in the same line. The nested rules are positioned next to each other without a newline, and the separate groups of rules will have new lines between them.
 

To set the CSS output to nested output style, we can use this command:

sass --watch styles.scss:styles.css --style compact

 

Example:

div { background-color: black; color: white; }

.topDiv { font-size: 2rem; }

.middleDiv { font-size: 2.4rem; }

.bottomDiv { font-size: 2.8rem; }

.headline { text-decoration: underline; font-size: 3em; color: yellow; }

Compressed Sass output style

The compressed out style takes the least space compared to all other above-discussed sass output styles. It provides whitespaces only to separate selectors and newlines at the end of the file. This is often confusing and is not easily readable.

 

The compressed output styles remove all the comments from the code. Also, the colors are converted to their shortest notation. It is perfect for production sites, where it's essential to keep file sizes as minimum as possible. 

 

To set the CSS output to nested output style, we can use this command:

sass --watch styles.scss:styles.css --style compressed

 

Example:

div{background-color:black;color:white}.topDiv{font-size:2rem}.middleDiv{font-size:2.4rem}.bottomDiv{font-size:2.8rem}.headline{text-decoration:underline;font-size:3em;color:yellow}

FAQs

  1. Which is the default sass output style?
    By default, the sass output style is the nested output style.
     
  2. What is the benefit of using compact and compressed output styles?
    The compact and compressed output styles reduce the size of the compiled CSS code. As the number of bytes of the CSS code to download reduces, our website or app loads faster, improving the user experience. 
     
  3. When is nested output style used?
    The nested output style is used for the project's development stage as it has better readability. When taking our project into production, we must compile the CSS code with the compressed output style. 
     
  4. Which sass output syles are not human-readable?
    The compact output style and especially the compressed output styles are not intended to be read by humans.

Key Takeaways

This blog covered the various sass output styles: nested output style, expanded output style, compact output style, and compressed output style, along with examples. We also discussed setting the sass output styles and the frequently asked questions related to sass output styles.

 

Don't stop here! Check out some fantastic blogs on Sass like Sass- Introduction, Sass-Installation, and Usage, etc.

 

We hope you found this blog on sass output style useful. Liked the blog? Then feel free to upvote and share it.

Live masterclass