十、CSS 预处理器
10.1 Sass/SCSS
// 变量
$primary-color: #007bff;
$secondary-color: #6c757d;
$spacing: 8px;
// 嵌套
.nav {
background: $primary-color;
&-list {
display: flex;
list-style: none;
&-item {
margin: 0 $spacing;
a {
color: white;
text-decoration: none;
&:hover {
text-decoration: underline;
}
}
}
}
}
// 混合宏
@mixin flex-center {
display: flex;
justify-content: center;
align-items: center;
}
@mixin box-shadow($shadow...) {
box-shadow: $shadow;
}
.container {
@include flex-center;
@include box-shadow(0 2px 4px rgba(0,0,0,0.1));
}
// 函数
@function rem($px) {
@return $px / 16px * 1rem;
}
h1 {
font-size: rem(32px);
}
// 继承
%button-base {
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
.button-primary {
@extend %button-base;
background: $primary-color;
color: white;
}
.button-secondary {
@extend %button-base;
background: $secondary-color;
color: white;
}
// 条件判断
@mixin responsive($breakpoint) {
@if $breakpoint == mobile {
@media (max-width: 768px) { @content; }
}
@else if $breakpoint == tablet {
@media (min-width: 769px) and (max-width: 1024px) { @content; }
}
@else if $breakpoint == desktop {
@media (min-width: 1025px) { @content; }
}
}
.element {
@include responsive(mobile) {
width: 100%;
}
@include responsive(desktop) {
width: 50%;
}
}
// 循环
$colors: (
'primary': #007bff,
'success': #28a745,
'danger': #dc3545,
'warning': #ffc107
);
@each $name, $color in $colors {
.btn-#{$name} {
background: $color;
&:hover {
background: darken($color, 10%);
}
}
}
// 自定义函数
@function strip-unit($number) {
@return $number / ($number * 0 + 1);
}
@function px-to-rem($px) {
@return strip-unit($px) / 16px * 1rem;
}
10.2 Less
// 变量
@primary-color: #007bff;
@secondary-color: #6c757d;
// 嵌套
.nav {
background: @primary-color;
&-list {
display: flex;
&-item {
margin: 0 10px;
}
}
}
// 混合宏
.flex-center() {
display: flex;
justify-content: center;
align-items: center;
}
.container {
.flex-center();
}
// 运算
@base-font: 16px;
h1 {
font-size: @base-font * 2; // 32px
}
// 函数
@import "some-file.less";
.darken(@color, @amount) {
color: darken(@color, @amount);
}
// 作用域
@color: #333;
.element {
@color: #666;
color: @color; // #666
}
.another {
color: @color; // #333
}
十一、CSS 架构与最佳实践
11.1 BEM 命名规范
/* BEM:Block__Element--Modifier */
/* Block */
.card { }
/* Element */
.card__header { }
.card__body { }
.card__footer { }
/* Modifier */
.card--featured { }
.card--small { }
.card__title--large { }
/* 完整示例 */
.card {
border: 1px solid #ddd;
border-radius: 8px;
overflow: hidden;
}
.card--featured {
border-color: #007bff;
box-shadow: 0 4px 12px rgba(0,123,255,0.2);
}
.card__header {
padding: 16px;
background: #f8f9fa;
border-bottom: 1px solid #eee;
}
.card__title {
font-size: 1.25rem;
margin: 0;
}
.card__title--large {
font-size: 1.5rem;
}
.card__body {
padding: 16px;
}
.card__footer {
padding: 16px;
border-top: 1px solid #eee;
background: #f8f9fa;
}
11.2 CSS 方法论
/* SMACSS 方法论 */
/* Base - 基础样式 */
body {
margin: 0;
font-family: sans-serif;
}
/* Layout - 布局 */
.l-container {
max-width: 1200px;
margin: 0 auto;
}
.l-header {
position: fixed;
top: 0;
width: 100%;
}
.l-main {
padding-top: 60px;
}
/* Module - 模块 */
.modal {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.modal.is-active {
display: block;
}
/* State - 状态 */
.is-hidden {
display: none;
}
.is-loading {
opacity: 0.5;
pointer-events: none;
}
/* Theme - 主题 */
.theme-dark {
background: #1a1a1a;
color: #fff;
}
.theme-light {
background: #fff;
color: #333;
}
11.3 性能优化
/* 1. 关键 CSS 内联 */
<style>
/* 首屏关键样式 */
.hero { background: #000; }
.header { height: 60px; }
</style>
/* 2. 避免使用昂贵的选择器 */
/* 避免 */
body div ul li a { }
/* 推荐 */
.nav-link { }
/* 3. 使用 CSS 动画代替 JavaScript */
.element {
transition: transform 0.3s;
}
.element:hover {
transform: translateX(10px);
}
/* 4. 使用 will-change 优化动画 */
.animated {
will-change: transform, opacity;
}
/* 5. 减少重排和重绘 */
/* 避免 */
for (let i = 0; i < 100; i++) {
element.style.width = i + 'px';
}
/* 推荐 */
element.style.width = '100px';
/* 6. 使用 CSS 变量减少重复 */
:root {
--color-primary: #007bff;
}
.button {
background: var(--color-primary);
}
十二、CSS 工具与框架
12.1 Tailwind CSS
<!-- 实用优先的 CSS 框架 -->
<div class="max-w-md mx-auto bg-white rounded-xl shadow-md overflow-hidden md:max-w-2xl">
<div class="md:flex">
<div class="md:flex-shrink-0">
<img class="h-48 w-full object-cover md:w-48" src="/img/placeholder.jpg" alt="Placeholder">
</div>
<div class="p-8">
<div class="uppercase tracking-wide text-sm text-indigo-500 font-semibold">案例研究</div>
<a href="#" class="block mt-1 text-lg leading-tight font-medium text-black hover:underline">
使用 Tailwind CSS 构建现代网站
</a>
<p class="mt-2 text-gray-500">Tailwind CSS 是一个实用优先的 CSS 框架,可以快速构建自定义设计。</p>
</div>
</div>
</div>
/* 自定义主题 */
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer components {
.btn-primary {
@apply px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors;
}
.card {
@apply bg-white rounded-lg shadow-md overflow-hidden;
}
}
@layer utilities {
.text-shadow {
text-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
}
12.2 CSS Modules
/* Button.module.css */
.button {
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
background: #007bff;
color: white;
}
.button--large {
padding: 12px 24px;
font-size: 1.1rem;
}
.button--outline {
background: transparent;
border: 1px solid #007bff;
color: #007bff;
}
js
// 在 React 中使用
import styles from './Button.module.css';
function Button({ children, variant, size }) {
return (
<button className={`
${styles.button}
${size === 'large' && styles['button--large']}
${variant === 'outline' && styles['button--outline']}
`}>
{children}
</button>
);
}
CSS 作为 Web 设计的基石,已经从简单的样式语言发展成为功能强大的设计系统。本文系统性地梳理了 CSS 的核心知识点,从基础语法到现代特性,从布局技术到动画效果,帮助开发者建立完整的知识体系。
来源:
https://app-aaqhxe29haf5.appmiaoda.com