CSS 一直在不断发展,新的特性和规范不断推出,使前端开发者能够创建更加美观、交互性更强的网页。本文将介绍一些你应该知道的现代 CSS 特性。
CSS 自定义属性(变量)
CSS 自定义属性允许你定义可重用的值,在整个样式表中使用。
基本语法
:root {
--primary-color: #007bff;
--secondary-color: #6c757d;
--font-size: 16px;
}
.button {
background-color: var(--primary-color);
color: white;
font-size: var(--font-size);
padding: 10px 20px;
border: none;
border-radius: 4px;
}
.button-secondary {
background-color: var(--secondary-color);
}
动态修改
可以通过 JavaScript 动态修改 CSS 自定义属性:
document.documentElement.style.setProperty("--primary-color", "#ff0000");
CSS Flexbox
Flexbox 是一种一维布局系统,用于创建灵活的行或列布局。
基本语法
.container {
display: flex;
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
gap: 20px; /* 项目间距 */
}
.item {
flex: 1 0 200px; /* 伸缩性: 增长因子 收缩因子 基础尺寸 */
}
常见应用
/* 导航栏 */
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
}
/* 卡片网格 */
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.card {
flex: 1 0 300px;
}
CSS 渐变
CSS 渐变允许你创建平滑的颜色过渡效果。
线性渐变
.linear-gradient {
background: linear-gradient(to right, #ff0000, #0000ff);
}
/* 角度渐变 */
.angle-gradient {
background: linear-gradient(45deg, #ff0000, #0000ff);
}
/* 多色渐变 */
.multi-gradient {
background: linear-gradient(to bottom, #ff0000, #ffff00, #00ff00, #0000ff);
}
径向渐变
.radial-gradient {
background: radial-gradient(circle, #ff0000, #0000ff);
}
/* 椭圆渐变 */
.ellipse-gradient {
background: radial-gradient(ellipse at center, #ff0000, #0000ff);
}
CSS 变换(Transform)
CSS 变换允许你旋转、缩放、倾斜或平移元素。
基本变换
.transform-examples {
/* 旋转 */
transform: rotate(45deg);
/* 缩放 */
transform: scale(1.5);
/* 倾斜 */
transform: skew(10deg, 5deg);
/* 平移 */
transform: translate(100px, 50px);
/* 组合变换 */
transform: rotate(45deg) scale(1.5) translate(100px, 50px);
}
3D 变换
.three-dimensional {
transform-style: preserve-3d;
transform: rotateX(45deg) rotateY(45deg);
}
CSS 过渡(Transition)
CSS 过渡允许你平滑地改变元素的样式属性。
基本语法
.transition-example {
width: 100px;
height: 100px;
background-color: #007bff;
transition: all 0.3s ease;
}
.transition-example:hover {
width: 200px;
height: 200px;
background-color: #ff0000;
}
指定属性
.transition-specific {
width: 100px;
height: 100px;
background-color: #007bff;
transition: width 0.3s ease, background-color 0.5s ease;
}
.transition-specific:hover {
width: 200px;
background-color: #ff0000;
}
CSS 动画(Animation)
CSS 动画允许你创建更复杂的动画效果,通过关键帧定义动画的各个阶段。
基本语法
@keyframes pulse {
0% {
transform: scale(1);
opacity: 1;
}
50% {
transform: scale(1.5);
opacity: 0.7;
}
100% {
transform: scale(1);
opacity: 1;
}
}
.animation-example {
width: 100px;
height: 100px;
background-color: #007bff;
animation: pulse 2s infinite;
}
动画属性
.animation-properties {
animation-name: pulse;
animation-duration: 2s;
animation-timing-function: ease;
animation-delay: 1s;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-fill-mode: both;
animation-play-state: running;
/* 简写 */
animation: pulse 2s ease 1s infinite alternate both running;
}
CSS 阴影效果
CSS 提供了多种阴影效果,包括盒阴影和文本阴影。
盒阴影
.box-shadow {
box-shadow: 10px 10px 5px rgba(0, 0, 0, 0.3);
}
/* 内阴影 */
.inner-shadow {
box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.5);
}
/* 多重阴影 */
.multi-shadow {
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3), 0 0 20px rgba(0, 0, 0, 0.2);
}
文本阴影
.text-shadow {
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}
/* 多重文本阴影 */
.multi-text-shadow {
text-shadow: 2px 2px 0 #000, -2px -2px 0 #fff;
}
CSS 过滤效果(Filter)
CSS 过滤效果允许你对元素应用图形效果,如模糊、亮度、对比度等。
.filter-example {
/* 模糊效果 */
filter: blur(5px);
/* 亮度调整 */
filter: brightness(1.5);
/* 对比度调整 */
filter: contrast(2);
/* 灰度效果 */
filter: grayscale(100%);
/* 色相旋转 */
filter: hue-rotate(90deg);
/* 多重过滤效果 */
filter: blur(2px) brightness(1.2) contrast(1.5);
}
CSS Blend Modes
CSS 混合模式允许你控制元素如何与其父元素和兄弟元素混合。
.blend-mode {
mix-blend-mode: multiply;
}
常见的混合模式包括:
- normal
- multiply
- screen
- overlay
- darken
- lighten
- color-dodge
- color-burn
- hard-light
- soft-light
- difference
- exclusion
- hue
- saturation
- color
- luminosity
总结
现代 CSS 提供了丰富的特性和工具,使前端开发者能够创建更加美观、交互性更强的网页。本文介绍了一些重要的现代 CSS 特性,包括 CSS 自定义属性、Flexbox、渐变、变换、过渡、动画、阴影效果、过滤效果和混合模式。
学习和掌握这些现代 CSS 特性,将有助于你提升前端开发技能,创建更加现代和高效的网页。建议你在实际项目中尝试使用这些特性,加深对它们的理解和应用。