CSS高级特性
2025/9/17大约 14 分钟
CSS高级特性
学习目标
- 掌握CSS3的新特性和高级功能
- 学会使用CSS动画和过渡效果
- 了解CSS变换和滤镜效果
- 掌握现代CSS开发技巧
CSS3新特性
圆角边框 - border-radius
CSS3引入的圆角功能让我们能够轻松创建圆角元素。
点击展开示例
/* 基础圆角设置 */
.basic-radius {
border-radius: 10px; /* 四个角相同圆角 */
background-color: #3498db;
color: white;
padding: 20px;
margin: 10px;
}
/* 分别设置四个角 */
.custom-radius {
border-top-left-radius: 10px; /* 左上角 */
border-top-right-radius: 20px; /* 右上角 */
border-bottom-right-radius: 30px; /* 右下角 */
border-bottom-left-radius: 40px; /* 左下角 */
/* 简写形式:左上 右上 右下 左下 */
border-radius: 10px 20px 30px 40px;
}
/* 椭圆形圆角 */
.elliptical-radius {
border-radius: 50px / 25px; /* 水平半径 / 垂直半径 */
width: 200px;
height: 100px;
background-color: #e74c3c;
}
/* 实用圆角样式 */
.card {
border-radius: 12px; /* 现代卡片圆角 */
background: white;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
padding: 24px;
margin: 16px;
}
.button {
border-radius: 6px; /* 按钮圆角 */
padding: 12px 24px;
background-color: #2ecc71;
color: white;
border: none;
cursor: pointer;
transition: all 0.3s ease;
}
.button:hover {
border-radius: 8px; /* 悬停时稍微增加圆角 */
transform: translateY(-2px);
}
.avatar {
border-radius: 50%; /* 圆形头像 */
width: 80px;
height: 80px;
object-fit: cover;
}
.pill {
border-radius: 25px; /* 胶囊形状 */
padding: 8px 20px;
background-color: #9b59b6;
color: white;
display: inline-block;
}
/* 不规则圆角 */
.organic-shape {
border-radius: 63% 37% 54% 46% / 55% 48% 52% 45%;
width: 200px;
height: 200px;
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
animation: morphing 8s ease-in-out infinite;
}
@keyframes morphing {
0%, 100% {
border-radius: 63% 37% 54% 46% / 55% 48% 52% 45%;
}
25% {
border-radius: 40% 60% 60% 40% / 60% 30% 70% 40%;
}
50% {
border-radius: 54% 46% 38% 62% / 49% 70% 30% 51%;
}
75% {
border-radius: 61% 39% 55% 45% / 61% 38% 62% 39%;
}
}
阴影效果 - box-shadow
阴影效果为元素添加深度和层次感。
点击展开示例
/* 基础阴影语法:x偏移 y偏移 模糊半径 扩展半径 颜色 */
.basic-shadow {
box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
padding: 20px;
background: white;
margin: 20px;
}
/* 多重阴影 */
.multiple-shadows {
box-shadow:
0 1px 3px rgba(0, 0, 0, 0.12), /* 细微阴影 */
0 1px 2px rgba(0, 0, 0, 0.24); /* 主要阴影 */
padding: 20px;
background: white;
}
/* 内阴影 */
.inset-shadow {
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
padding: 20px;
background: #f8f9fa;
border-radius: 8px;
}
/* 发光效果 */
.glow-effect {
box-shadow: 0 0 20px rgba(52, 152, 219, 0.5);
background-color: #3498db;
color: white;
padding: 20px;
border-radius: 8px;
transition: box-shadow 0.3s ease;
}
.glow-effect:hover {
box-shadow: 0 0 30px rgba(52, 152, 219, 0.8);
}
/* 材质设计阴影 */
.material-shadow-1 {
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
}
.material-shadow-2 {
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
}
.material-shadow-3 {
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
}
.material-shadow-4 {
box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
}
.material-shadow-5 {
box-shadow: 0 19px 38px rgba(0, 0, 0, 0.30), 0 15px 12px rgba(0, 0, 0, 0.22);
}
/* 悬浮卡片效果 */
.hover-card {
background: white;
border-radius: 12px;
padding: 24px;
transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
cursor: pointer;
}
.hover-card:hover {
transform: translateY(-8px);
box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
}
/* 彩色阴影 */
.colored-shadow {
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
color: white;
padding: 20px;
border-radius: 12px;
box-shadow: 0 8px 32px rgba(255, 107, 107, 0.3);
transition: all 0.3s ease;
}
.colored-shadow:hover {
box-shadow: 0 12px 48px rgba(255, 107, 107, 0.4);
transform: translateY(-4px);
}
渐变背景 - gradient
点击展开示例
/* 线性渐变 */
.linear-gradient {
background: linear-gradient(to right, #ff6b6b, #4ecdc4);
height: 100px;
border-radius: 8px;
}
/* 指定角度的线性渐变 */
.angled-gradient {
background: linear-gradient(45deg, #667eea, #764ba2);
height: 100px;
border-radius: 8px;
}
/* 多色渐变 */
.multi-color-gradient {
background: linear-gradient(
90deg,
#ff6b6b 0%,
#feca57 25%,
#48dbfb 50%,
#ff9ff3 75%,
#54a0ff 100%
);
height: 100px;
border-radius: 8px;
}
/* 径向渐变 */
.radial-gradient {
background: radial-gradient(circle, #ff6b6b, #4ecdc4);
height: 100px;
border-radius: 8px;
}
/* 椭圆径向渐变 */
.elliptical-gradient {
background: radial-gradient(ellipse at center, #667eea, #764ba2);
height: 100px;
border-radius: 8px;
}
/* 锥形渐变 */
.conic-gradient {
background: conic-gradient(from 0deg, #ff6b6b, #feca57, #48dbfb, #ff9ff3, #ff6b6b);
height: 100px;
border-radius: 50%;
}
/* 实用渐变样式 */
.gradient-button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 12px 24px;
border-radius: 6px;
cursor: pointer;
transition: all 0.3s ease;
position: relative;
overflow: hidden;
}
.gradient-button::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
opacity: 0;
transition: opacity 0.3s ease;
}
.gradient-button:hover::before {
opacity: 1;
}
.gradient-button span {
position: relative;
z-index: 1;
}
/* 渐变文字 */
.gradient-text {
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
font-size: 2rem;
font-weight: bold;
}
/* 动态渐变背景 */
.animated-gradient {
background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
background-size: 400% 400%;
animation: gradientShift 15s ease infinite;
height: 200px;
border-radius: 12px;
}
@keyframes gradientShift {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
CSS动画
过渡效果 - transition
过渡效果让属性变化更加平滑自然。
点击展开示例
/* 基础过渡语法:属性 持续时间 时间函数 延迟 */
.basic-transition {
background-color: #3498db;
color: white;
padding: 15px 30px;
border: none;
border-radius: 6px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.basic-transition:hover {
background-color: #2980b9;
}
/* 多属性过渡 */
.multi-transition {
background-color: #e74c3c;
color: white;
padding: 15px 30px;
border: none;
border-radius: 6px;
cursor: pointer;
transform: scale(1);
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
transition:
background-color 0.3s ease,
transform 0.3s ease,
box-shadow 0.3s ease;
}
.multi-transition:hover {
background-color: #c0392b;
transform: scale(1.05);
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}
/* 所有属性过渡 */
.all-transition {
transition: all 0.3s ease;
padding: 15px 30px;
background-color: #2ecc71;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
}
.all-transition:hover {
background-color: #27ae60;
transform: translateY(-3px);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
}
/* 不同的时间函数 */
.timing-functions {
display: flex;
gap: 10px;
flex-wrap: wrap;
}
.timing-demo {
width: 100px;
height: 50px;
background-color: #9b59b6;
margin-bottom: 10px;
cursor: pointer;
transition: transform 1s;
}
.ease { transition-timing-function: ease; }
.linear { transition-timing-function: linear; }
.ease-in { transition-timing-function: ease-in; }
.ease-out { transition-timing-function: ease-out; }
.ease-in-out { transition-timing-function: ease-in-out; }
.cubic-bezier { transition-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55); }
.timing-demo:hover {
transform: translateX(200px);
}
/* 延迟过渡 */
.delayed-transition {
background-color: #f39c12;
color: white;
padding: 15px 30px;
border: none;
border-radius: 6px;
cursor: pointer;
transition: all 0.5s ease 0.2s; /* 延迟0.2秒 */
}
.delayed-transition:hover {
background-color: #e67e22;
transform: scale(1.1);
}
关键帧动画 - @keyframes
关键帧动画提供了更复杂和精确的动画控制。
点击展开示例
/* 基础关键帧动画 */
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.fade-in {
animation: fadeIn 0.6s ease-out;
}
/* 百分比关键帧 */
@keyframes bounce {
0% {
transform: translateY(0);
}
25% {
transform: translateY(-20px);
}
50% {
transform: translateY(0);
}
75% {
transform: translateY(-10px);
}
100% {
transform: translateY(0);
}
}
.bounce-animation {
animation: bounce 1s ease-in-out;
}
/* 无限循环动画 */
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.rotate-infinite {
animation: rotate 2s linear infinite;
width: 50px;
height: 50px;
background-color: #3498db;
border-radius: 50%;
}
/* 脉冲动画 */
@keyframes pulse {
0% {
transform: scale(1);
box-shadow: 0 0 0 0 rgba(52, 152, 219, 0.7);
}
70% {
transform: scale(1.05);
box-shadow: 0 0 0 10px rgba(52, 152, 219, 0);
}
100% {
transform: scale(1);
box-shadow: 0 0 0 0 rgba(52, 152, 219, 0);
}
}
.pulse-button {
background-color: #3498db;
color: white;
border: none;
padding: 15px 30px;
border-radius: 6px;
cursor: pointer;
animation: pulse 2s infinite;
}
/* 打字机效果 */
@keyframes typing {
from {
width: 0;
}
to {
width: 100%;
}
}
@keyframes blink {
50% {
border-color: transparent;
}
}
.typewriter {
font-family: 'Courier New', monospace;
font-size: 1.5rem;
border-right: 2px solid #333;
white-space: nowrap;
overflow: hidden;
animation:
typing 3s steps(30, end),
blink 0.75s step-end infinite;
}
/* 加载动画 */
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.loading-spinner {
border: 4px solid #f3f3f3;
border-top: 4px solid #3498db;
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 1s linear infinite;
}
/* 波浪动画 */
@keyframes wave {
0%, 100% {
transform: translateY(0);
}
50% {
transform: translateY(-20px);
}
}
.wave-container {
display: flex;
gap: 5px;
align-items: center;
justify-content: center;
height: 60px;
}
.wave-dot {
width: 10px;
height: 10px;
background-color: #3498db;
border-radius: 50%;
animation: wave 1.4s ease-in-out infinite;
}
.wave-dot:nth-child(1) { animation-delay: 0s; }
.wave-dot:nth-child(2) { animation-delay: 0.1s; }
.wave-dot:nth-child(3) { animation-delay: 0.2s; }
.wave-dot:nth-child(4) { animation-delay: 0.3s; }
.wave-dot:nth-child(5) { animation-delay: 0.4s; }
/* 动画控制属性 */
.animation-controls {
animation-name: bounce;
animation-duration: 2s; /* 持续时间 */
animation-timing-function: ease-in-out; /* 时间函数 */
animation-delay: 0.5s; /* 延迟 */
animation-iteration-count: 3; /* 重复次数 */
animation-direction: alternate; /* 方向:normal, reverse, alternate, alternate-reverse */
animation-fill-mode: forwards; /* 填充模式:none, forwards, backwards, both */
animation-play-state: running; /* 播放状态:running, paused */
}
/* 简写形式 */
.animation-shorthand {
animation: bounce 2s ease-in-out 0.5s 3 alternate forwards;
}
变换 - transform
点击展开示例
/* 2D变换 */
.transform-demo {
width: 100px;
height: 100px;
background-color: #e74c3c;
margin: 50px;
transition: transform 0.3s ease;
}
/* 平移 */
.translate {
transform: translate(50px, 30px); /* X轴50px, Y轴30px */
}
.translateX {
transform: translateX(50px); /* 只在X轴平移 */
}
.translateY {
transform: translateY(30px); /* 只在Y轴平移 */
}
/* 缩放 */
.scale {
transform: scale(1.5); /* 等比例缩放1.5倍 */
}
.scaleX {
transform: scaleX(2); /* X轴缩放2倍 */
}
.scaleY {
transform: scaleY(0.5); /* Y轴缩放0.5倍 */
}
.scale-xy {
transform: scale(1.5, 0.8); /* X轴1.5倍,Y轴0.8倍 */
}
/* 旋转 */
.rotate {
transform: rotate(45deg); /* 顺时针旋转45度 */
}
.rotate-negative {
transform: rotate(-30deg); /* 逆时针旋转30度 */
}
/* 倾斜 */
.skew {
transform: skew(20deg, 10deg); /* X轴倾斜20度,Y轴倾斜10度 */
}
.skewX {
transform: skewX(20deg); /* 只在X轴倾斜 */
}
.skewY {
transform: skewY(10deg); /* 只在Y轴倾斜 */
}
/* 组合变换 */
.combined-transform {
transform: translate(50px, 30px) rotate(45deg) scale(1.2);
}
/* 变换原点 */
.transform-origin-demo {
width: 100px;
height: 100px;
background-color: #3498db;
margin: 50px;
transition: transform 0.3s ease;
}
.origin-center {
transform-origin: center; /* 默认值 */
transform: rotate(45deg);
}
.origin-top-left {
transform-origin: top left;
transform: rotate(45deg);
}
.origin-bottom-right {
transform-origin: bottom right;
transform: rotate(45deg);
}
.origin-custom {
transform-origin: 20px 30px; /* 自定义原点 */
transform: rotate(45deg);
}
/* 3D变换 */
.transform-3d {
perspective: 1000px; /* 设置透视距离 */
width: 200px;
height: 200px;
margin: 100px auto;
}
.cube-3d {
width: 100px;
height: 100px;
background-color: #9b59b6;
transform-style: preserve-3d; /* 保持3D效果 */
transition: transform 1s ease;
}
.cube-3d:hover {
transform: rotateX(45deg) rotateY(45deg);
}
/* 3D翻转卡片 */
.flip-card {
background-color: transparent;
width: 200px;
height: 200px;
perspective: 1000px;
margin: 50px;
}
.flip-card-inner {
position: relative;
width: 100%;
height: 100%;
text-align: center;
transition: transform 0.6s;
transform-style: preserve-3d;
}
.flip-card:hover .flip-card-inner {
transform: rotateY(180deg);
}
.flip-card-front, .flip-card-back {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: bold;
}
.flip-card-front {
background-color: #3498db;
}
.flip-card-back {
background-color: #e74c3c;
transform: rotateY(180deg);
}
滤镜效果
点击展开示例
/* CSS滤镜效果 */
.filter-demo {
width: 200px;
height: 150px;
background-image: url('demo-image.jpg');
background-size: cover;
background-position: center;
margin: 10px;
border-radius: 8px;
transition: filter 0.3s ease;
}
/* 模糊效果 */
.blur {
filter: blur(5px);
}
.blur:hover {
filter: blur(0);
}
/* 亮度调整 */
.brightness {
filter: brightness(1.5); /* 增加亮度 */
}
.brightness-dark {
filter: brightness(0.5); /* 降低亮度 */
}
/* 对比度调整 */
.contrast {
filter: contrast(1.5); /* 增加对比度 */
}
/* 饱和度调整 */
.saturate {
filter: saturate(2); /* 增加饱和度 */
}
.desaturate {
filter: saturate(0); /* 去色(黑白) */
}
/* 色相旋转 */
.hue-rotate {
filter: hue-rotate(90deg); /* 色相旋转90度 */
}
/* 反色 */
.invert {
filter: invert(1); /* 完全反色 */
}
.invert-partial {
filter: invert(0.5); /* 部分反色 */
}
/* 透明度 */
.opacity-filter {
filter: opacity(0.5); /* 50%透明度 */
}
/* 阴影滤镜 */
.drop-shadow {
filter: drop-shadow(4px 4px 8px rgba(0, 0, 0, 0.3));
}
/* 组合滤镜 */
.combined-filter {
filter: brightness(1.2) contrast(1.1) saturate(1.3);
}
.combined-filter:hover {
filter: brightness(1) contrast(1) saturate(1) blur(0);
}
/* 实用滤镜效果 */
.image-hover-effects {
overflow: hidden;
border-radius: 12px;
}
.image-hover-effects img {
width: 100%;
height: 200px;
object-fit: cover;
transition: all 0.3s ease;
filter: brightness(0.8) contrast(1.2);
}
.image-hover-effects:hover img {
transform: scale(1.1);
filter: brightness(1) contrast(1) saturate(1.2);
}
/* 毛玻璃效果 */
.glass-effect {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 12px;
padding: 20px;
color: white;
}
现代CSS技术
CSS变量(自定义属性)
点击展开示例
/* 定义CSS变量 */
:root {
/* 颜色变量 */
--primary-color: #3498db;
--secondary-color: #2ecc71;
--danger-color: #e74c3c;
--warning-color: #f39c12;
--dark-color: #2c3e50;
--light-color: #ecf0f1;
/* 尺寸变量 */
--border-radius: 8px;
--box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
--transition: all 0.3s ease;
/* 字体变量 */
--font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
--font-size-sm: 14px;
--font-size-md: 16px;
--font-size-lg: 18px;
/* 间距变量 */
--spacing-xs: 4px;
--spacing-sm: 8px;
--spacing-md: 16px;
--spacing-lg: 24px;
--spacing-xl: 32px;
}
/* 使用CSS变量 */
.button {
background-color: var(--primary-color);
color: white;
border: none;
border-radius: var(--border-radius);
padding: var(--spacing-sm) var(--spacing-md);
font-family: var(--font-family);
font-size: var(--font-size-md);
box-shadow: var(--box-shadow);
transition: var(--transition);
cursor: pointer;
}
.button:hover {
background-color: var(--secondary-color);
transform: translateY(-2px);
}
/* 主题切换 */
.theme-light {
--bg-color: #ffffff;
--text-color: #333333;
--border-color: #e0e0e0;
}
.theme-dark {
--bg-color: #1a1a1a;
--text-color: #ffffff;
--border-color: #404040;
}
.themed-component {
background-color: var(--bg-color);
color: var(--text-color);
border: 1px solid var(--border-color);
padding: var(--spacing-lg);
border-radius: var(--border-radius);
transition: var(--transition);
}
/* 动态变量 */
.dynamic-color {
--hue: 200;
background-color: hsl(var(--hue), 70%, 50%);
transition: background-color 0.3s ease;
}
.dynamic-color:hover {
--hue: 300;
}
CSS Grid布局
点击展开示例
/* 基础Grid布局 */
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 三列等宽 */
grid-template-rows: auto;
gap: 20px;
padding: 20px;
}
.grid-item {
background-color: #3498db;
color: white;
padding: 20px;
border-radius: 8px;
text-align: center;
}
/* 复杂Grid布局 */
.complex-grid {
display: grid;
grid-template-columns: 200px 1fr 150px;
grid-template-rows: 60px 1fr 40px;
grid-template-areas:
"header header header"
"sidebar main aside"
"footer footer footer";
gap: 10px;
min-height: 100vh;
}
.grid-header {
grid-area: header;
background-color: #2c3e50;
color: white;
display: flex;
align-items: center;
padding: 0 20px;
}
.grid-sidebar {
grid-area: sidebar;
background-color: #ecf0f1;
padding: 20px;
}
.grid-main {
grid-area: main;
background-color: white;
padding: 20px;
}
.grid-aside {
grid-area: aside;
background-color: #f8f9fa;
padding: 20px;
}
.grid-footer {
grid-area: footer;
background-color: #34495e;
color: white;
display: flex;
align-items: center;
justify-content: center;
}
/* 响应式Grid */
.responsive-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
padding: 20px;
}
.responsive-grid-item {
background-color: #e74c3c;
color: white;
padding: 20px;
border-radius: 8px;
min-height: 200px;
}
现代CSS选择器
点击展开示例
/* 属性选择器 */
input[type="email"] {
border: 2px solid #3498db;
}
input[placeholder*="密码"] {
border: 2px solid #e74c3c;
}
a[href^="https://"] {
color: #2ecc71;
}
a[href$=".pdf"] {
color: #e74c3c;
}
/* 结构伪类选择器 */
.list-item:nth-child(odd) {
background-color: #f8f9fa;
}
.list-item:nth-child(even) {
background-color: #ffffff;
}
.list-item:nth-child(3n) {
border-left: 4px solid #3498db;
}
.list-item:first-child {
border-top-left-radius: 8px;
border-top-right-radius: 8px;
}
.list-item:last-child {
border-bottom-left-radius: 8px;
border-bottom-right-radius: 8px;
}
.list-item:only-child {
border-radius: 8px;
}
/* 状态伪类选择器 */
input:focus {
outline: none;
border-color: #3498db;
box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.25);
}
input:valid {
border-color: #2ecc71;
}
input:invalid {
border-color: #e74c3c;
}
button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
/* 伪元素选择器 */
.quote::before {
content: '"';
font-size: 2em;
color: #3498db;
}
.quote::after {
content: '"';
font-size: 2em;
color: #3498db;
}
.tooltip {
position: relative;
}
.tooltip::after {
content: attr(data-tooltip);
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
background-color: #333;
color: white;
padding: 8px 12px;
border-radius: 4px;
font-size: 14px;
white-space: nowrap;
opacity: 0;
visibility: hidden;
transition: all 0.3s ease;
}
.tooltip:hover::after {
opacity: 1;
visibility: visible;
transform: translateX(-50%) translateY(-5px);
}
最佳实践与性能优化
1. 动画性能优化
点击展开示例
/* 使用transform和opacity进行动画(GPU加速) */
.optimized-animation {
/* 好的做法 */
transform: translateX(100px);
opacity: 0.5;
transition: transform 0.3s ease, opacity 0.3s ease;
}
/* 避免动画这些属性(会触发重排) */
.avoid-animation {
/* 避免 */
left: 100px;
width: 200px;
height: 100px;
}
/* 使用will-change提示浏览器 */
.will-change-demo {
will-change: transform, opacity;
transition: transform 0.3s ease;
}
.will-change-demo:hover {
transform: scale(1.1);
}
/* 动画完成后移除will-change */
.animation-complete {
will-change: auto;
}
2. CSS组织和维护
点击展开示例
/* 使用BEM命名规范 */
.card {
/* 块 */
background: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.card__header {
/* 元素 */
padding: 20px 20px 0;
border-bottom: 1px solid #eee;
}
.card__title {
/* 元素 */
margin: 0;
font-size: 1.25rem;
}
.card__body {
/* 元素 */
padding: 20px;
}
.card--featured {
/* 修饰符 */
border: 2px solid #3498db;
}
.card--large {
/* 修饰符 */
max-width: 600px;
}
/* 使用CSS自定义属性进行主题管理 */
.component {
background-color: var(--component-bg, #ffffff);
color: var(--component-text, #333333);
border: 1px solid var(--component-border, #e0e0e0);
}
/* 响应式设计的最佳实践 */
.responsive-component {
/* 移动优先 */
padding: var(--spacing-sm);
font-size: var(--font-size-sm);
}
@media (min-width: 768px) {
.responsive-component {
padding: var(--spacing-md);
font-size: var(--font-size-md);
}
}
@media (min-width: 1024px) {
.responsive-component {
padding: var(--spacing-lg);
font-size: var(--font-size-lg);
}
}
3. 可访问性和用户体验
点击展开示例
/* 尊重用户的动画偏好 */
@media (prefers-reduced-motion: reduce) {
* {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
/* 高对比度模式支持 */
@media (prefers-contrast: high) {
.button {
border: 2px solid currentColor;
}
}
/* 焦点管理 */
.focus-visible {
outline: 2px solid #3498db;
outline-offset: 2px;
}
/* 确保足够的颜色对比度 */
.accessible-text {
color: #333333; /* 4.5:1 对比度 */
background-color: #ffffff;
}
/* 触摸友好的设计 */
@media (pointer: coarse) {
.touch-target {
min-height: 44px;
min-width: 44px;
padding: 12px;
}
}
总结
本章我们学习了CSS的高级特性,包括CSS3新特性、动画效果、变换、滤镜以及现代CSS技术。这些技术让我们能够创建更加丰富、动态和现代化的网页界面。
关键要点:
- CSS3提供了丰富的视觉效果和动画能力
- 合理使用动画和过渡可以提升用户体验
- 性能优化和可访问性同样重要
- 现代CSS技术如变量、Grid布局提供了更好的开发体验
- 始终考虑用户的需求和设备限制
通过掌握这些CSS技术,你已经具备了创建现代化、响应式和用户友好的网页界面的能力。继续实践和探索,你将能够创造出更加出色的网页作品!