CSS3 常用功能详细使用指南

  1. 选择器增强
    属性选择器
    css
    /* 以"btn-"开头的属性 */
    class\^="btn-" {
    background: #3498db;
    }

/* 以"-primary"结尾的类 */

class$="-primary" {

color: white;

}

/* 包含"active"的类 */
class* ="active" {

font-weight: bold;

}

效果:精确选择具有特定属性模式的元素

结构伪类选择器

css

/* 第3个子元素 */

li:nth-child(3) {

color: red;

}

/* 奇数行 */

tr:nth-child(odd) {

background: #f2f2f2;

}

/* 倒数第2个子元素 */

div:nth-last-child(2) {

border-bottom: 1px solid #ddd;

}

/* 不是.first类的元素 */

div:not(.first) {

opacity: 0.8;

}

效果:基于元素位置或特征进行精确样式控制

  1. 边框与背景
    圆角边框
    css
    .box {
    border-radius: 10px; /* 统一圆角 /
    border-radius: 5px 10px 15px 20px; /
    分别设置四个角 /
    border-radius: 50%; /
    圆形 */
    }
    效果:创建圆角或完全圆形元素

盒子阴影

css

.card {

box-shadow: 2px 2px 5px rgba(0,0,0,0.3);

/* 水平偏移 垂直偏移 模糊半径 颜色 */

/* 内阴影 */

box-shadow: inset 0 0 10px #000;

/* 多重阴影 */

box-shadow: 0 0 5px #999, 0 0 10px #777;

}

效果:添加深度感和立体效果

多重背景

css

.hero {

background:

url('top-image.png') top left no-repeat,

url('bottom-image.png') bottom right no-repeat,

linear-gradient(to bottom, #1e5799, #2989d8);

background-size: 200px, 300px, cover;

}

效果:在一个元素上叠加多个背景图像和渐变

  1. 渐变效果
    线性渐变
    css
    .gradient-bg {
    background: linear-gradient(to right, #ff9966, #ff5e62);

/* 多色渐变 */

background: linear-gradient(to bottom, red, yellow, green);

/* 角度渐变 */

background: linear-gradient(45deg, #3a7bd5, #00d2ff);

}

径向渐变

css

.circle {

background: radial-gradient(circle, #f5f7fa, #c3cfe2);

/* 椭圆形渐变 */

background: radial-gradient(ellipse at center, #a1c4fd, #c2e9fb);

}

效果:创建平滑的颜色过渡,替代图片背景

  1. 过渡效果
    css
    .button {
    background: #3498db;
    transition: all 0.3s ease;

/* 分开指定属性 */

transition:

background 0.3s ease,

transform 0.2s ease-out;

}

.button:hover {

background: #2980b9;

transform: scale(1.05);

}

效果:鼠标悬停时平滑过渡到新状态

  1. 动画效果
    css
    @keyframes bounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-20px); }
    }

.ball {

animation: bounce 1s infinite;

/* 分开指定属性 */

animation-name: bounce;

animation-duration: 1s;

animation-timing-function: ease;

animation-iteration-count: infinite;

animation-delay: 0.5s;

animation-direction: alternate;

}

效果:创建复杂的动画序列

  1. 2D/3D 转换
    2D 转换
    css
    .transform-box {
    transform: translate(50px, 100px); /* 移动 /
    transform: rotate(30deg); /
    旋转 /
    transform: scale(1.5); /
    缩放 /
    transform: skew(20deg, 10deg); /
    倾斜 */

/* 组合变换 /
transform: translateX(50px) rotate(45deg) scale(1.2);
}
3D 转换
css
.container {
perspective: 1000px; /
3D透视 */

}

.card {

transform-style: preserve-3d;

transition: transform 1s;

transform: rotateY(180deg);

}

效果:创建复杂的视觉变换和3D效果

  1. 弹性盒子布局 (Flexbox)
    css
    .container {
    display: flex;
    flex-direction: row; /* 或 column /
    justify-content: center; /
    主轴对齐 /
    align-items: center; /
    交叉轴对齐 */
    flex-wrap: wrap;
    }

.item {

flex: 1; /* 弹性项目 /
align-self: flex-start; /
单个项目对齐 /
order: 2; /
显示顺序 */

}

效果:创建灵活的一维布局,轻松实现各种对齐方式

  1. 网格布局 (Grid)
    css
    .container {
    display: grid;
    grid-template-columns: 1fr 2fr 1fr;
    grid-template-rows: 100px auto 50px;
    gap: 10px;

/* 区域布局 */

grid-template-areas:

"header header header"

"sidebar main aside"

"footer footer footer";

}

.header { grid-area: header; }

.main { grid-area: main; }

效果:创建复杂的二维布局系统

  1. 响应式设计 (媒体查询)
    css
    /* 默认样式 */
    .container {
    width: 100%;
    }

/* 平板设备 */

@media (min-width: 768px) {

.container {

width: 750px;

}

}

/* 桌面设备 */

@media (min-width: 992px) {

.container {

width: 970px;

}

}

/* 大屏幕 */

@media (min-width: 1200px) {

.container {

width: 1170px;

}

}

效果:根据不同屏幕尺寸应用不同样式

  1. CSS 变量
    css
    :root {
    --primary-color: #3498db;
    --secondary-color: #2980b9;
    --spacing: 16px;
    }

.element {

color: var(--primary-color);

margin: var(--spacing);

/* 默认值 */

padding: var(--spacing, 10px);

}

效果:集中管理样式值,便于主题切换和维护

  1. 滤镜效果
    css
    .image {
    filter: blur(2px); /* 模糊 /
    filter: brightness(150%); /
    亮度 /
    filter: contrast(200%); /
    对比度 /
    filter: grayscale(80%); /
    灰度 /
    filter: sepia(100%); /
    褐色 /
    filter: hue-rotate(90deg); /
    色相旋转 */

/* 组合滤镜 */

filter: brightness(1.2) contrast(0.8) saturate(1.5);

}

效果:实时图像处理,无需编辑原图

  1. 裁剪与形状
    css
    .circle {
    clip-path: circle(50% at 50% 50%);
    }

.polygon {

clip-path: polygon(0 0, 100% 0, 100% 75%, 0 100%);

}

.text-wrap {

shape-outside: circle(50%);

float: left;

width: 200px;

height: 200px;

}

效果:创建非矩形元素和文字环绕效果

相关推荐
Cutecat_2 小时前
视频字幕处理工具横向:提取模式 vs 编辑模式,该如何选择
android·前端·ios·语音识别
qq_422152572 小时前
PDF 加水印工具怎么选?2026 年文档版权保护方案对比
前端·pdf·github
kyriewen2 小时前
手写 Promise.all、race、any:不到 30 行代码,解决并发异步的所有姿势
前端·javascript·面试
brucelee1863 小时前
OpenClaw 浏览器控制(Chrome MCP)完整教程
前端·chrome
ct9783 小时前
React 状态管理方案深度对比
开发语言·前端·react
胡志辉的博客4 小时前
深入浅出理解浏览器事件循环:从一道输出题讲到 Chrome 源码
前端·javascript·chrome·chromium·event loop
代码不加糖4 小时前
js中不会冒泡的事件有哪些?
前端·javascript·vue.js
懂懂tty4 小时前
Vue2与Vue3之间API差异
前端·javascript·vue.js
AI焦点4 小时前
跨越协议鸿沟:Tool Use状态机从Anthropic到OpenAI兼容体系的适配要点
前端·人工智能
Dxy12393102164 小时前
Python线程锁:为什么多线程会“打架“,以及怎么解决
开发语言·前端·python