Why to put β€œ_” in front of filename in SCSS?


The SCSS allows developers to write CSS in a more structured way. Also, we can create multiple files for CSS while using the SCSS and import the required file in the main SCSS file.

In this tutorial, we will see the objective to put β€˜_’ before the filename in SCSS.

When should we put β€˜_’ in front of the filename in SCSS?

Whenever we put the -_’ before the filename in the SCSS, the compiler ignores the file while compiling the SCSS. The file becomes partial if the filename starts with the β€˜_’ character.

For example, we have two files named β€˜style.scss’ and β€˜_partial.scss’. The compiler only compiles the style.scss file, and ignores the _partial.scss file. However, if we require to use the css of the _partial.scss file, we can import it in the style.scss file.

Example

The example below demonstrates how to use the SCSS with the html.

Filename – demo.html

We have added a β€˜style.css’ file in the below file using the β€˜<link>’ tag, which is generated by the compiler of the SCSS. In the output, users can observe that CSS is applied to the div element's text, which became italic.

<html>
<head>
   <link rel = "stylesheet" href = "style.css">
</head>
<body>
   <h2> Using the <i> SCSS </i> with HTML. </h2>
   <div> This is a div element. </div>
</body>
</html>

Filename – style.scss

In the below file, we created the variable to store the font size and style. After that, we used the variables to style the div element.

$font-size : 20px;
$font-style: italic;
div {
   font-size: $font-size;
   font-style: $font-style;
}

Filename – style.css

Whenever we compile the style.scss file, it generates the below code, which is used by the html file.

div {
   font-size: 20px;
   font-style: italic;
}

Example

<html>
<head>
   <style>
      /* compiled scss code */
      div {
         font-size: 20px;
         font-style: italic;
      }
   </style>
</head>
<body>
   <h2> Using the <i> SCSS </i> with HTML. </h2>
   <div> This is a div element. </div>
</body>
</html>

Example

In this example, we demonstrate how to put β€˜_’ before the file name and how to use its CSS in the main css file.

Filename – demo.html

The below file contains the simple HTML code and includes the β€˜style.css’ file in the <head> tag.

<html>
<head>
   <link rel = "stylesheet" href = "style.css">
</head>
<body>
   <h2> Using the <i> SCSS from the _partial.css file </i> with HTML.</h2>
   <div> Content of the div element. </div>
</body>
</html>

Filename - _partial.css

Users need to create a _partial.scss file with β€˜_’ before the filename. After that, users need to add the below code in the file. When we compile the SCSS code, the compiler will ignore the code of this file

$text-color: blue;
$border-width: 2px;
$border-style: solid;
$border-color: green;

Filename – style.scss

Now, users need to add the below code to the style.scss file, which is the main css file. In the below code, we have imported the css from the β€˜_partial.css’ file. In this way, we can use the code of the partial files.

@import "partial"
div {
   color: $text-color;
   border: $border-width $border-style $border-color;
}

Filename – style.css

Whenever we compile the style.scss file, it generates the style.css file automatically.

div {
   color: blue;
   border: 2px solid green;
}

Example

<html>
<head>
   <style>
      /* compiled SCSS code from the _partial.css file */
      div {
         color: blue;
         border: 2px solid green;
      }
   </style>
</head>
<body>
   <h2> Using the <i> SCSS from the _partial.css file </i> with HTML.</h2>
   <div> Content of the div element. </div>
</body>
</html>

The main motive for inserting the β€˜_’ before the file name in SCSS is to make the file partial so the compiler can ignore the file. Whenever we require to use the css of the partial files, we can import it into the main file.

The main benefit of using the partial files is that we don’t require to write repetitive code, making it clearer. For example, we can add different partial files for the CSS of different parts and use them whenever required.

Updated on: 19-Apr-2023

343 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements