ReasonJun

SCSS : Concept and Feature 본문

Frontend/SCSS

SCSS : Concept and Feature

ReasonJun 2023. 8. 31. 13:01
728x90

SCSS (Sass CSS) stands for "Sassy CSS" and is a popular extension of CSS (Cascading Style Sheets). It introduces a set of enhancements and features that make writing and maintaining CSS code more efficient and organized. SCSS is often used in combination with build tools like Webpack and preprocessors to streamline web development workflows.

Here are the key features and concepts of SCSS:

  • Nesting: SCSS allows you to nest CSS rules within each other, which helps create a more hierarchical and readable structure. This mimics the structure of the HTML elements in your document, making it easier to understand the relationships between elements and their styles.
.container {
  width: 100%;
  padding: 20px;

  .header {
    font-size: 24px;
    color: #333;
  }
}
  • Variables: SCSS enables you to define variables that can store reusable values such as colors, fonts, or dimensions. This makes it easy to maintain consistent styling throughout your project.
$primary-color: #007bff;
$font-family: 'Arial', sans-serif;

.button {
  background-color: $primary-color;
  font-family: $font-family;
}
  • Mixins: Mixins allow you to define reusable groups of CSS declarations that can be included within other rule sets. This is particularly useful for applying cross-browser compatibility fixes or creating complex styles.
@mixin border-radius($radius) {
  -webkit-border-radius: $radius;
  -moz-border-radius: $radius;
  border-radius: $radius;
}

.rounded-box {
  @include border-radius(10px);
}
  • Partials: SCSS supports the use of partials, which are separate SCSS files containing specific CSS rules. These files are typically prefixed with an underscore (e.g., _variables.scss) and are included in other SCSS files using the @import directive.
  • Imports: SCSS allows you to split your styles into multiple files and import them as needed. This helps organize your styles into logical components or sections.
@import 'variables';
@import 'buttons';
@import 'forms';
  • Operators: SCSS supports mathematical operators like +, -, *, and /, allowing you to perform calculations within your style rules.
.box {
  width: 100px + 20px;
  height: 200px / 2;
}
  • Extend/Inheritance: SCSS provides the @extend directive, which lets you share a set of CSS properties from one selector to another. This can reduce duplicated code and encourage more modular styling.
.button {
  padding: 10px;
  background-color: #007bff;
}

.success-button {
  @extend .button;
  background-color: #28a745;
}
  • Functions: SCSS includes built-in functions and allows you to define your own. Functions can be used to manipulate values, perform calculations, and generate dynamic styles.
@function darken($color, $amount) {
  @return mix(black, $color, $amount);
}

.dark-bg {
  background-color: darken(#007bff, 20%);
}

SCSS code needs to be compiled into standard CSS before it can be used in web browsers. There are several ways to do this, including using the command line, build tools like Webpack, or standalone SCSS compilers. Once compiled, the resulting CSS can be linked to your HTML documents just like regular CSS.

728x90
Comments