| 特性 | SCSS/SASS | Less | UnoCSS | Tailwind |
|---|---|---|---|---|
| 变量 | ✅ 支持 | ✅ 支持 | ❌ | ❌ |
| 嵌套 | ✅ 完整支持 | ✅ 支持 | ❌ | ❌ |
| 混入(Mixin) | ✅ 强大 | ✅ 基础 | ❌ | ❌ |
| 函数 | ✅ 内置+自定义 | ⚠️ 有限 | ❌ | ❌ |
| 循环 | ✅ @for/@each/@while | ⚠️ 有限 | ❌ | ❌ |
| 条件判断 | ✅ @if/@else | ⚠️ 有限 | ❌ | ❌ |
| 继承 | ✅ @extend | ✅ 支持 | ❌ | ❌ |
| 模块化 | ✅ @use/@forward | ⚠️ @import | ❌ | ❌ |
| 数学运算 | ✅ 完整 | ✅ 支持 | ❌ | ❌ |
| 颜色函数 | ✅ 丰富 | ⚠️ 基础 | ❌ | ❌ |
| 编程能力 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐ | ⭐ |
// 支持多种数据类型
$primary-color: #0769fb;
$spacing: 16px;
$font-family: ('Arial', sans-serif);
$breakpoints: (
'small': 480px,
'medium': 768px,
'large': 1024px
);
// 使用
.container {
color: $primary-color;
padding: $spacing;
font-family: $font-family;
@media (min-width: map-get($breakpoints, 'medium')) {
padding: $spacing * 2;
}
}
// 基础混入
@mixin flex-center {
display: flex;
align-items: center;
justify-content: center;
}
// 带参数的混入
@mixin button($bg-color, $text-color: white) {
background-color: $bg-color;
color: $text-color;
padding: 12px 24px;
border-radius: 4px;
cursor: pointer;
transition: all 0.3s;
&:hover {
background-color: darken($bg-color, 10%);
}
}
// 使用
.btn-primary {
@include button(#0769fb);
}
.btn-danger {
@include button(#ff0000);
}
// 内置函数
.container {
// 颜色函数
background: lighten(#0769fb, 20%);
border-color: darken(#0769fb, 10%);
color: rgba(#0769fb, 0.8);
// 数学函数
width: percentage(2/3);
font-size: round(18.7px);
// 字符串函数
content: quote('Hello');
font-family: unquote('Arial');
}
// 自定义函数
@function calculate-rem($pixels) {
@return $pixels / 16px * 1rem;
}
@function spacing($multiplier) {
@return $multiplier * 8px;
}
.title {
font-size: calculate-rem(24px); // 1.5rem
margin: spacing(2); // 16px
}
// @for 循环
@for $i from 1 through 5 {
.mt-#{$i} {
margin-top: #{$i * 8}px;
}
}
// 生成: .mt-1, .mt-2, .mt-3, .mt-4, .mt-5
// @each 循环
$colors: (
'primary': #0769fb,
'success': #28a745,
'danger': #dc3545,
'warning': #ffc107
);
@each $name, $color in $colors {
.btn-#{$name} {
background-color: $color;
&:hover {
background-color: darken($color, 10%);
}
}
}
// @while 循环
$i: 1;
@while $i <= 12 {
.col-#{$i} {
width: percentage($i / 12);
}
$i: $i + 1;
}
@mixin responsive-text($size) {
@if $size == 'small' {
font-size: 14px;
line-height: 1.5;
} @else if $size == 'medium' {
font-size: 16px;
line-height: 1.6;
} @else {
font-size: 18px;
line-height: 1.7;
}
}
.title {
@include responsive-text('large');
}
// 条件判断函数
@function get-text-color($bg-color) {
@if lightness($bg-color) > 50% {
@return #000; // 浅色背景用黑色文字
} @else {
@return #fff; // 深色背景用白色文字
}
}
.button {
background: $primary-color;
color: get-text-color($primary-color);
}
%button-base {
padding: 12px 24px;
border-radius: 4px;
cursor: pointer;
transition: all 0.3s;
}
.btn-primary {
@extend %button-base;
background-color: #0769fb;
color: white;
}
.btn-secondary {
@extend %button-base;
background-color: #6c757d;
color: white;
}
// _variables.scss
$primary-color: #0769fb;
$spacing: 16px;
// _mixins.scss
@mixin flex-center {
display: flex;
align-items: center;
justify-content: center;
}
// main.scss
@use 'variables' as vars;
@use 'mixins' as mix;
.container {
color: vars.$primary-color;
padding: vars.$spacing;
@include mix.flex-center;
}
.container {
color: #333;
// 普通嵌套
.title {
font-size: 24px;
}
// & 引用父选择器
&:hover {
background: #f5f5f5;
}
// 属性嵌套
border: {
width: 1px;
style: solid;
color: #ddd;
}
// 媒体查询嵌套
@media (max-width: 768px) {
padding: 10px;
.title {
font-size: 20px;
}
}
}
$primary: #0769fb;
.button {
// 变亮/变暗
background: lighten($primary, 20%);
border-color: darken($primary, 10%);
// 调整饱和度
background: saturate($primary, 20%);
background: desaturate($primary, 20%);
// 调整透明度
background: rgba($primary, 0.8);
background: fade-in($primary, 0.2);
background: fade-out($primary, 0.2);
// 混合颜色
background: mix($primary, white, 50%);
// 调整色调
background: adjust-hue($primary, 30deg);
// 互补色
border-color: complement($primary);
}
// 配置
$spacing-base: 8px;
$colors: (
'primary': #0769fb,
'success': #28a745,
'danger': #dc3545
);
// 工具函数
@function spacing($multiplier) {
@return $multiplier * $spacing-base;
}
@function color($name) {
@return map-get($colors, $name);
}
// 混入
@mixin button-variant($color-name) {
$color: color($color-name);
background-color: $color;
color: white;
&:hover {
background-color: darken($color, 10%);
}
&:active {
background-color: darken($color, 15%);
}
}
@mixin responsive-grid($columns) {
display: grid;
grid-template-columns: repeat($columns, 1fr);
gap: spacing(2);
@media (max-width: 768px) {
grid-template-columns: repeat(1, 1fr);
}
}
// 使用
.button {
padding: spacing(1.5) spacing(3);
border-radius: 4px;
cursor: pointer;
transition: all 0.3s;
&.primary {
@include button-variant('primary');
}
&.success {
@include button-variant('success');
}
&.danger {
@include button-variant('danger');
}
}
.grid-container {
@include responsive-grid(3);
}
| 工具 | 主要用途 | 功能强度 | 适用场景 |
|---|---|---|---|
| SCSS/SASS | 预处理器 | ⭐⭐⭐⭐⭐ | 需要复杂逻辑、主题系统、大型项目 |
| Less | 预处理器 | ⭐⭐⭐ | 简单项目、快速开发 |
| UnoCSS | 原子化CSS | ⭐⭐ | 快速开发、性能优先 |
| Tailwind | 原子化CSS | ⭐⭐ | 快速开发、工具类 |
SCSS/SASS 是功能最强大的 CSS 工具,因为:
但要注意:
使用 SCSS/SASS 当你需要:
使用 UnoCSS 当你需要:
使用 Less 当你需要: