Sass(Syntactically Awesome Style Sheets)预处理器为CSS开发带来了许多强大的功能和优势。
主要好处
1. 变量(Variables)
// 定义变量
$primary-color: #3498db;
$secondary-color: #2ecc71;
$font-size-base: 16px;
$border-radius: 4px;
// 使用变量
.button {
background-color: $primary-color;
font-size: $font-size-base;
border-radius: $border-radius;
&:hover {
background-color: darken($primary-color, 10%);
}
}
.card {
border: 1px solid lighten($primary-color, 20%);
border-radius: $border-radius;
}好处:
- 统一管理颜色、尺寸等设计令牌
- 易于维护和修改主题
- 避免硬编码值的重复
2. 嵌套(Nesting)
// Sass嵌套语法
.navbar {
background: $primary-color;
padding: 1rem;
.nav-list {
display: flex;
list-style: none;
margin: 0;
.nav-item {
margin-right: 1rem;
.nav-link {
color: white;
text-decoration: none;
&:hover {
color: $secondary-color;
}
&.active {
font-weight: bold;
}
}
}
}
}编译后的CSS:
.navbar {
background: #3498db;
padding: 1rem;
}
.navbar .nav-list {
display: flex;
list-style: none;
margin: 0;
}
.navbar .nav-list .nav-item {
margin-right: 1rem;
}
.navbar .nav-list .nav-item .nav-link {
color: white;
text-decoration: none;
}
.navbar .nav-list .nav-item .nav-link:hover {
color: #2ecc71;
}
.navbar .nav-list .nav-item .nav-link.active {
font-weight: bold;
}3. 混入(Mixins)
// 定义混入
@mixin button-style($bg-color, $text-color: white) {
background-color: $bg-color;
color: $text-color;
padding: 0.5rem 1rem;
border: none;
border-radius: $border-radius;
cursor: pointer;
transition: background-color 0.3s;
&:hover {
background-color: darken($bg-color, 10%);
}
}
@mixin flex-center {
display: flex;
justify-content: center;
align-items: center;
}
@mixin responsive($breakpoint) {
@if $breakpoint == mobile {
@media (max-width: 767px) { @content; }
}
@else if $breakpoint == tablet {
@media (min-width: 768px) and (max-width: 1023px) { @content; }
}
@else if $breakpoint == desktop {
@media (min-width: 1024px) { @content; }
}
}
// 使用混入
.btn-primary {
@include button-style($primary-color);
}
.btn-secondary {
@include button-style($secondary-color);
}
.modal {
@include flex-center;
@include responsive(mobile) {
width: 90%;
}
@include responsive(desktop) {
width: 500px;
}
}4. 继承(Inheritance)
// 基础样式
%button-base {
padding: 0.5rem 1rem;
border: none;
border-radius: $border-radius;
cursor: pointer;
font-size: $font-size-base;
transition: all 0.3s;
}
%card-base {
background: white;
border-radius: $border-radius;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
padding: 1rem;
}
// 继承并扩展
.btn-primary {
@extend %button-base;
background-color: $primary-color;
color: white;
}
.btn-outline {
@extend %button-base;
background-color: transparent;
border: 1px solid $primary-color;
color: $primary-color;
}
.product-card {
@extend %card-base;
.title {
font-size: 1.2rem;
margin-bottom: 0.5rem;
}
}5. 函数(Functions)
// 内置函数
$base-color: #3498db;
.element {
background: lighten($base-color, 20%); // 变亮
border: 1px solid darken($base-color, 15%); // 变暗
color: complement($base-color); // 互补色
}
// 自定义函数
@function px-to-rem($px, $base: 16px) {
@return ($px / $base) * 1rem;
}
@function strip-unit($number) {
@if type-of($number) == 'number' and not unitless($number) {
@return $number / ($number * 0 + 1);
}
@return $number;
}
// 使用自定义函数
.title {
font-size: px-to-rem(24px); // 1.5rem
margin-bottom: px-to-rem(16px); // 1rem
}6. 运算(Operations)
$container-width: 1200px;
$gutter: 20px;
.container {
width: $container-width;
margin: 0 auto;
}
.sidebar {
width: ($container-width / 3) - $gutter;
margin-right: $gutter;
}
.main-content {
width: (($container-width / 3) * 2) - $gutter;
}
// 字符串运算
$prefix: 'app';
$component: 'button';
.#{$prefix}-#{$component} {
// 生成 .app-button
}7. 控制指令(Control Directives)
// @if 条件判断
@mixin theme($theme: light) {
@if $theme == light {
background-color: white;
color: black;
} @else if $theme == dark {
background-color: black;
color: white;
} @else {
background-color: gray;
color: white;
}
}
// @for 循环
@for $i from 1 through 12 {
.col-#{$i} {
width: percentage($i / 12);
}
}
// @each 遍历
$colors: (primary: #3498db, secondary: #2ecc71, danger: #e74c3c);
@each $name, $color in $colors {
.btn-#{$name} {
background-color: $color;
&:hover {
background-color: darken($color, 10%);
}
}
}
// @while 循环
$grid-columns: 12;
$i: 1;
@while $i <= $grid-columns {
.grid-#{$i} {
width: percentage($i / $grid-columns);
}
$i: $i + 1;
}8. 模块化(Partials & Import)
// _variables.scss
$primary-color: #3498db;
$secondary-color: #2ecc71;
$font-stack: 'Helvetica Neue', Helvetica, Arial, sans-serif;
// _mixins.scss
@mixin button-style($color) {
background-color: $color;
color: white;
padding: 0.5rem 1rem;
border: none;
border-radius: 4px;
}
// _components.scss
.button {
@include button-style($primary-color);
}
.card {
background: white;
border-radius: 4px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
// main.scss
@import 'variables';
@import 'mixins';
@import 'components';
body {
font-family: $font-stack;
color: #333;
}开发效率提升
1. 代码复用
// 创建可复用的组件样式
@mixin card($padding: 1rem, $shadow: true) {
background: white;
border-radius: $border-radius;
padding: $padding;
@if $shadow {
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
}
.product-card {
@include card(1.5rem, true);
}
.simple-card {
@include card(0.5rem, false);
}2. 主题管理
// 主题变量
$themes: (
light: (
background: white,
text: #333,
primary: #3498db
),
dark: (
background: #2c3e50,
text: white,
primary: #e74c3c
)
);
@mixin themed() {
@each $theme, $map in $themes {
.theme-#{$theme} & {
$theme-map: () !global;
@each $key, $submap in $map {
$value: map-get(map-get($themes, $theme), '#{$key}');
$theme-map: map-merge($theme-map, ($key: $value)) !global;
}
@content;
$theme-map: null !global;
}
}
}
@function themed($key) {
@return map-get($theme-map, $key);
}
// 使用主题
.header {
@include themed() {
background-color: themed('background');
color: themed('text');
}
}3. 响应式设计
// 断点管理
$breakpoints: (
xs: 0,
sm: 576px,
md: 768px,
lg: 992px,
xl: 1200px
);
@mixin media-breakpoint-up($name) {
$min: map-get($breakpoints, $name);
@if $min != 0 {
@media (min-width: $min) {
@content;
}
} @else {
@content;
}
}
// 使用
.container {
padding: 1rem;
@include media-breakpoint-up(md) {
padding: 2rem;
}
@include media-breakpoint-up(lg) {
padding: 3rem;
}
}维护性优势
1. 更好的代码组织
// 文件结构
styles/
├── abstracts/
│ ├── _variables.scss
│ ├── _functions.scss
│ └── _mixins.scss
├── base/
│ ├── _reset.scss
│ └── _typography.scss
├── components/
│ ├── _buttons.scss
│ ├── _cards.scss
│ └── _forms.scss
├── layout/
│ ├── _header.scss
│ ├── _footer.scss
│ └── _grid.scss
└── main.scss2. 错误检查
// Sass会在编译时检查错误
@function validate-color($color) {
@if type-of($color) != color {
@error "Expected a color, got #{type-of($color)}";
}
@return $color;
}
.element {
color: validate-color($primary-color); // 正确
// color: validate-color("not-a-color"); // 编译错误
}性能优势
1. 编译时优化
// 未使用的样式会被移除
%unused-placeholder {
color: red; // 不会出现在最终CSS中
}
.used-class {
@extend %button-base; // 只有被使用的placeholder才会编译
}2. 代码压缩
// 开发时的可读代码
.navigation {
.nav-list {
.nav-item {
.nav-link {
color: $primary-color;
}
}
}
}
// 编译后可以压缩为
// .navigation .nav-list .nav-item .nav-link{color:#3498db}Sass预处理器通过这些功能大大提升了CSS的开发效率、代码质量和维护性,是现代前端开发的重要工具。