📜  sass (1)

📅  最后修改于: 2023-12-03 15:19:55.375000             🧑  作者: Mango

SASS - A powerful preprocessor for CSS

SASS (Syntactically Awesome Style Sheets) is a preprocessor scripting language that is interpreted or compiled into standard CSS. It extends CSS syntax, providing features like variables, nesting, inheritance, and many more, making the front-end development process more efficient and organized.

Why use SASS?
  • Code Reusability: SASS allows the use of variables, mixins, functions, and parent selectors, which makes the CSS code more reusable and less repetitive.

  • Modularity: SASS supports modular CSS architecture through partials and imports, making it easy to break down CSS code into smaller, more manageable files.

  • Productivity: SASS speeds up the development process by reducing the amount of code needed to write CSS, allowing for faster prototyping and development.

  • Maintainability: SASS provides a more organized structure for CSS that is easier to maintain and update, reducing the risk of errors and increasing the scalability of projects.

SASS Features
Variables
$primary-color: #4a90e2;
$secondary-color: #f18500;

.btn-primary {
  background-color: $primary-color;
  color: #fff;
}

.btn-secondary {
  background-color: $secondary-color;
  color: #fff;
}
Nesting
.btn {
  font-size: 16px;
  border: none;
  padding: 8px 16px;

  &.btn-primary {
    background-color: $primary-color;
    color: #fff;
  }

  &.btn-secondary {
    background-color: $secondary-color;
    color: #fff;
  }
}
Mixins
@mixin button-style($bg-color, $text-color) {
  background-color: $bg-color;
  color: $text-color;
  font-size: 16px;
  border: none;
  padding: 8px 16px;
}

.btn-primary {
  @include button-style($primary-color, #fff);
}

.btn-secondary {
  @include button-style($secondary-color, #fff);
}
Functions
@function calculate-width($num-items, $item-width, $gap-width) {
  @return ($num-items * $item-width) + ($num-items - 1) * $gap-width;
}

.container {
  width: calculate-width(5, 100px, 20px);
}
Partials

_site.scss

$primary-color: #4a90e2;
$secondary-color: #f18500;
$accent-color: #ff4444;

main.scss

@import 'site';

.btn-primary {
  background-color: $primary-color;
  color: #fff;
}

.btn-secondary {
  background-color: $secondary-color;
  color: #fff;
}

.btn-accent {
  background-color: $accent-color;
  color: #fff;
}
Conclusion

SASS is an indispensable tool for front-end developers. It offers a variety of features that help make CSS code more efficient, modular, and maintainable. With SASS, you can write CSS code faster and in a more organized way.