第13章: CSS数学函数和现代选择器
🎯 本章重点
- CSS数学函数(calc, min, max, clamp)
- 现代CSS选择器
- 属性选择器高级用法
- 伪类和伪元素
- 选择器组合和特异性
📖 内容概述
13.1 CSS数学函数
13.1.1 calc() 函数
/* 基础计算 */
.calc-basic {
width: calc(100% - 50px);
height: calc(50vh + 20px);
margin: calc(2rem * 1.5);
padding: calc(20px / 2);
}
/* 复杂计算 */
.calc-complex {
/* 响应式计算 */
width: calc(100vw - (2 * var(--container-padding)));
/* 网格布局计算 */
grid-template-columns: calc(50% - 10px) calc(50% - 10px);
/* 动画计算 */
transform: translateX(calc(100% - 50px));
}
/* 实际应用示例 */
.responsive-card {
/* 响应式卡片宽度 */
width: calc(100% - 2rem);
max-width: calc(1200px - 4rem);
/* 动态间距 */
gap: calc(var(--base-spacing) * 2);
@media (min-width: 768px) {
width: calc(50% - 1rem);
}
@media (min-width: 1024px) {
width: calc(33.333% - 1.5rem);
}
}
/* 字体大小计算 */
.fluid-typography {
font-size: calc(1rem + 0.5vw);
line-height: calc(1.4 + 0.1vw);
}
/* 颜色计算 */
.dynamic-colors {
background-color: rgb(
calc(255 - var(--darken-amount)),
calc(255 - var(--darken-amount)),
calc(255 - var(--darken-amount))
);
}
13.1.2 min() 和 max() 函数
/* min() 函数 - 取最小值 */
.min-function {
/* 限制最大宽度 */
width: min(100%, 500px);
/* 响应式字体大小 */
font-size: min(2rem, 5vw);
/* 动态间距 */
padding: min(2rem, 10%);
/* 网格布局限制 */
grid-template-columns: repeat(auto-fit, minmax(min(200px, 100%), 1fr));
}
/* max() 函数 - 取最大值 */
.max-function {
/* 确保最小宽度 */
width: max(300px, 50%);
/* 确保最小字体大小 */
font-size: max(1rem, 2vw);
/* 确保最小间距 */
margin: max(1rem, 5%);
/* 图片尺寸控制 */
img {
width: max(100px, 20%);
height: max(150px, 25%);
}
}
/* min() 和 max() 组合使用 */
.responsive-container {
/* 响应式容器 */
width: min(max(300px, 50%), 800px);
height: max(200px, 30vh);
/* 动态内边距 */
padding: min(max(1rem, 2%), 3rem);
}
/* 实际布局应用 */
.adaptive-layout {
/* 自适应网格 */
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(250px, 100%), 1fr));
gap: min(2rem, 5%);
/* 卡片尺寸控制 */
.card {
width: min(100%, 400px);
height: max(200px, 30vh);
}
}
/* 响应式导航 */
.navigation {
/* 导航项大小控制 */
--nav-item-size: min(80px, 15vw);
width: var(--nav-item-size);
height: var(--nav-item-size);
/* 图标大小 */
.icon {
font-size: min(1.5rem, 4vw);
}
}
13.1.3 clamp() 函数
/* clamp() 函数 - 钳制值范围 */
.clamp-basic {
/* 语法: clamp(最小值, 首选值, 最大值) */
font-size: clamp(1rem, 2.5vw, 2rem);
width: clamp(300px, 50%, 800px);
height: clamp(200px, 30vh, 500px);
}
/* 流体排版 */
.fluid-typography {
/* 响应式字体大小 */
font-size: clamp(1.125rem, 1.5vw + 0.5rem, 2rem);
/* 响应式行高 */
line-height: clamp(1.4, 1.1 + 0.5vw, 1.8);
/* 响应式字间距 */
letter-spacing: clamp(0.5px, 0.1em, 2px);
}
/* 布局应用 */
.responsive-layout {
/* 容器尺寸 */
width: clamp(320px, 90vw, 1200px);
margin: 0 auto;
/* 内边距 */
padding: clamp(1rem, 5vw, 3rem);
/* 网格间距 */
gap: clamp(1rem, 3vw, 2rem);
}
/* 组件尺寸控制 */
.component-sizing {
/* 按钮尺寸 */
.button {
padding: clamp(0.5rem, 2vw, 1rem) clamp(1rem, 4vw, 2rem);
font-size: clamp(0.875rem, 1.5vw, 1.125rem);
}
/* 卡片尺寸 */
.card {
width: clamp(280px, 30vw, 400px);
min-height: clamp(200px, 40vh, 300px);
}
/* 图标尺寸 */
.icon {
width: clamp(24px, 5vw, 48px);
height: clamp(24px, 5vw, 48px);
}
}
/* 实际案例:响应式标题 */
.hero-section {
.title {
font-size: clamp(2rem, 8vw, 4rem);
line-height: clamp(1.2, 1.1 + 0.5vw, 1.4);
margin-bottom: clamp(1rem, 3vw, 2rem);
}
.subtitle {
font-size: clamp(1rem, 3vw, 1.5rem);
line-height: clamp(1.4, 1.2 + 0.3vw, 1.6);
}
.cta-button {
padding: clamp(0.75rem, 2vw, 1.25rem) clamp(1.5rem, 4vw, 2.5rem);
font-size: clamp(1rem, 2vw, 1.25rem);
}
}
13.1.4 数学函数组合应用
/* 复杂计算组合 */
.complex-calculations {
/* 动态网格布局 */
grid-template-columns: repeat(
auto-fit,
minmax(
clamp(200px, calc(100vw / 4 - 2rem), 300px),
1fr
)
);
/* 响应式间距 */
gap: clamp(
1rem,
calc(2rem + 1vw - 10px),
3rem
);
}
/* 颜色动态计算 */
.dynamic-colors {
/* 基于视口的颜色变化 */
--hue: calc(var(--base-hue) + (100vw / 100));
--saturation: clamp(50%, calc(60% + 5vw), 80%);
--lightness: clamp(40%, calc(50% - 2vw), 60%);
background-color: hsl(
var(--hue),
var(--saturation),
var(--lightness)
);
}
/* 动画路径计算 */
.animated-element {
/* 动态动画路径 */
animation: move calc(2s + (100vw / 1000) * 1s) infinite alternate;
@keyframes move {
from {
transform: translateX(calc(0px - 10vw));
}
to {
transform: translateX(calc(100px + 5vw));
}
}
}
/* 响应式阴影 */
.dynamic-shadow {
/* 基于尺寸的动态阴影 */
box-shadow:
0 calc(2px + 0.1vw) calc(4px + 0.2vw) rgba(0, 0, 0, 0.1),
0 calc(4px + 0.2vw) calc(8px + 0.4vw) rgba(0, 0, 0, 0.1);
}
/* 实际应用:仪表板布局 */
.dashboard {
/* 侧边栏宽度 */
--sidebar-width: clamp(200px, 20vw, 300px);
/* 主内容区域 */
--main-content-width: calc(100vw - var(--sidebar-width));
display: grid;
grid-template-columns: var(--sidebar-width) 1fr;
/* 响应式调整 */
@media (max-width: 768px) {
--sidebar-width: clamp(60px, 15vw, 100px);
grid-template-columns: var(--sidebar-width) 1fr;
}
}
13.2 现代CSS选择器
13.2.1 属性选择器
/* 基础属性选择器 */
[attribute] {
/* 选择具有该属性的元素 */
}
[attribute="value"] {
/* 选择属性值完全匹配的元素 */
}
[attribute~="value"] {
/* 选择属性值包含该单词的元素 */
}
[attribute|="value"] {
/* 选择属性值以该值开头或后面跟着连字符的元素 */
}
[attribute^="value"] {
/* 选择属性值以该值开头的元素 */
}
[attribute$="value"] {
/* 选择属性值以该值结尾的元素 */
}
[attribute*="value"] {
/* 选择属性值包含该值的元素 */
}
/* 实际应用示例 */
/* 选择所有外部链接 */
a[href^="http"]:not([href*="mysite.com"]) {
color: #007bff;
text-decoration: underline;
}
/* 选择特定文件类型的链接 */
a[href$=".pdf"]::after {
content: " 📄";
}
a[href$=".doc"]::after,
a[href$=".docx"]::after {
content: " 📝";
}
a[href$=".zip"]::after,
a[href$=".rar"]::after {
content: " 📦";
}
/* 选择特定数据属性的元素 */
[data-status="pending"] {
background-color: #fff3cd;
border-color: #ffeaa7;
}
[data-status="success"] {
background-color: #d1edff;
border-color: #b3d9ff;
}
[data-status="error"] {
background-color: #f8d7da;
border-color: #f5c6cb;
}
/* 选择特定类型的输入框 */
input[type="text"],
input[type="email"],
input[type="password"] {
border: 1px solid #ddd;
padding: 0.5rem;
}
input[type="checkbox"],
input[type="radio"] {
margin-right: 0.5rem;
}
/* 选择特定语言的元素 */
[lang|="zh"] {
font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
}
[lang|="en"] {
font-family: "Arial", "Helvetica", sans-serif;
}
/* 选择特定数据范围的元素 */
[data-rating^="5"] {
color: gold;
}
[data-rating^="4"] {
color: orange;
}
[data-rating^="3"] {
color: #ccc;
}
13.2.2 结构伪类
/* 基础结构伪类 */
:first-child { } /* 第一个子元素 */
:last-child { } /* 最后一个子元素 */
:only-child { } /* 唯一子元素 */
:nth-child(n) { } /* 第n个子元素 */
:nth-last-child(n) { } /* 倒数第n个子元素 */
:first-of-type { } /* 同类型第一个元素 */
:last-of-type { } /* 同类型最后一个元素 */
:only-of-type { } /* 同类型唯一元素 */
:nth-of-type(n) { } /* 同类型第n个元素 */
:nth-last-of-type(n) { } /* 同类型倒数第n个元素 */
/* 实际应用示例 */
/* 列表项样式 */
.list-item:nth-child(odd) {
background-color: #f8f9fa;
}
.list-item:nth-child(even) {
background-color: #ffffff;
}
.list-item:first-child {
border-top-left-radius: 0.5rem;
border-top-right-radius: 0.5rem;
}
.list-item:last-child {
border-bottom-left-radius: 0.5rem;
border-bottom-right-radius: 0.5rem;
}
/* 表格行样式 */
tr:nth-of-type(odd) {
background-color: #f8f9fa;
}
tr:nth-of-type(even) {
background-color: #ffffff;
}
/* 卡片网格布局 */
.card:nth-child(3n+1) {
/* 每行第一个卡片 */
margin-left: 0;
}
.card:nth-child(3n) {
/* 每行最后一个卡片 */
margin-right: 0;
}
/* 导航菜单 */
.nav-item:not(:last-child) {
margin-right: 1rem;
}
.nav-item:first-child {
margin-left: 0;
}
/* 表单组 */
.form-group:not(:last-child) {
margin-bottom: 1rem;
}
/* 响应式网格调整 */
@media (max-width: 768px) {
.card:nth-child(2n+1) {
margin-left: 0;
}
.card:nth-child(2n) {
margin-right: 0;
}
}
/* 复杂选择器组合 */
.container > :not(:first-child) {
margin-top: 1rem;
}
.container > :not(:last-child) {
margin-bottom: 1rem;
}
13.2.3 状态伪类
/* 交互状态伪类 */
:hover { } /* 鼠标悬停 */
:active { } /* 激活状态 */
:focus { } /* 获得焦点 */
:focus-visible { } /* 键盘焦点可见 */
:focus-within { } /* 子元素获得焦点 */
/* 表单状态伪类 */
:checked { } /* 选中状态 */
:disabled { } /* 禁用状态 */
:enabled { } /* 启用状态 */
:required { } /* 必填字段 */
:optional { } /* 可选字段 */
:valid { } /* 有效输入 */
:invalid { } /* 无效输入 */
:in-range { } /* 范围内值 */
:out-of-range { } /* 范围外值 */
:read-only { } /* 只读状态 */
:read-write { } /* 可读写状态 */
/* 实际应用示例 */
/* 按钮交互状态 */
.button {
transition: all 0.3s ease;
}
.button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
.button:active {
transform: translateY(0);
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
.button:focus {
outline: 2px solid #007bff;
outline-offset: 2px;
}
.button:focus:not(:focus-visible) {
outline: none;
}
/* 表单验证样式 */
input:valid {
border-color: #28a745;
}
input:invalid {
border-color: #dc3545;
}
input:focus:valid {
box-shadow: 0 0 0 0.2rem rgba(40, 167, 69, 0.25);
}
input:focus:invalid {
box-shadow: 0 0 0 0.2rem rgba(220, 53, 69, 0.25);
}
/* 自定义复选框 */
.checkbox-input {
display: none;
}
.checkbox-label {
position: relative;
padding-left: 2rem;
cursor: pointer;
}
.checkbox-label::before {
content: "";
position: absolute;
left: 0;
top: 50%;
transform: translateY(-50%);
width: 1.5rem;
height: 1.5rem;
border: 2px solid #ddd;
border-radius: 0.25rem;
transition: all 0.3s ease;
}
.checkbox-input:checked + .checkbox-label::before {
background-color: #007bff;
border-color: #007bff;
}
.checkbox-input:checked + .checkbox-label::after {
content: "✓";
position: absolute;
left: 0.4rem;
top: 50%;
transform: translateY(-50%);
color: white;
font-weight: bold;
}
/* 禁用状态样式 */
.button:disabled,
input:disabled,
select:disabled {
opacity: 0.6;
cursor: not-allowed;
}
/* 焦点容器样式 */
.form-group:focus-within {
border-color: #007bff;
box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
}
13.2.4 内容相关伪类
/* 内容相关伪类 */
:empty { } /* 空元素 */
:target { } /* URL目标元素 */
:root { } /* 文档根元素 */
/* 语言相关伪类 */
:lang(en) { } /* 特定语言 */
:dir(ltr) { } /* 文字方向 */
/* 实际应用示例 */
/* 空状态样式 */
.empty-state:empty {
display: flex;
align-items: center;
justify-content: center;
min-height: 200px;
background-color: #f8f9fa;
border: 2px dashed #dee2e6;
border-radius: 0.5rem;
}
.empty-state:empty::before {
content: "暂无数据";
color: #6c757d;
font-size: 1.1rem;
}
/* 目标锚点样式 */
.section:target {
background-color: #fff3cd;
border-left: 4px solid #ffc107;
padding-left: 1rem;
}
/* 目录导航高亮 */
.toc-link[href]:target {
color: #007bff;
font-weight: bold;
}
/* 多语言支持 */
:lang(zh) {
font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
}
:lang(en) {
font-family: "Arial", "Helvetica", sans-serif;
}
:lang(ja) {
font-family: "Hiragino Sans", "Yu Gothic", sans-serif;
}
/* 文字方向支持 */
:dir(ltr) {
text-align: left;
}
:dir(rtl) {
text-align: right;
}
/* 根元素变量定义 */
:root {
--primary-color: #007bff;
--secondary-color: #6c757d;
--success-color: #28a745;
--danger-color: #dc3545;
--warning-color: #ffc107;
--info-color: #17a2b8;
--font-size-base: 1rem;
--line-height-base: 1.5;
--border-radius: 0.375rem;
}
/* 响应式根元素调整 */
@media (prefers-color-scheme: dark) {
:root {
--bg-primary: #1a1a1a;
--bg-secondary: #2d2d2d;
--text-primary: #ffffff;
--text-secondary: #b0b0b0;
}
}
@media (prefers-reduced-motion: reduce) {
:root {
--transition-duration: 0.1s;
}
}
13.3 高级选择器技巧
13.3.1 选择器组合
/* 后代选择器 */
.container .item { }
/* 子元素选择器 */
.container > .item { }
/* 相邻兄弟选择器 */
.item + .item { }
/* 通用兄弟选择器 */
.item ~ .item { }
/* 实际应用示例 */
/* 卡片网格系统 */
.card-grid {
display: grid;
gap: 1rem;
}
.card-grid > .card {
background: white;
border-radius: 0.5rem;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.card-grid > .card + .card {
margin-top: 0;
}
/* 面包屑导航 */
.breadcrumb {
display: flex;
align-items: center;
}
.breadcrumb > * {
display: flex;
align-items: center;
}
.breadcrumb > * + *::before {
content: "›";
margin: 0 0.5rem;
color: #6c757d;
}
/* 表单验证链 */
.form-group input:invalid + .error-message {
display: block;
color: #dc3545;
font-size: 0.875rem;
}
.form-group input:valid + .error-message {
display: none;
}
/* 标签组样式 */
.tag-group .tag:not(:last-child) {
margin-right: 0.5rem;
}
.tag-group .tag:first-child {
margin-left: 0;
}
/* 响应式调整 */
@media (max-width: 768px) {
.card-grid > .card {
margin: 0.5rem;
}
.card-grid > .card + .card {
margin-top: 0.5rem;
}
}
13.3.2 :is() 和 :where() 伪类
/* :is() 伪类 - 考虑特异性 */
:is(header, footer, section) h1 {
font-size: 2rem;
color: #333;
}
/* 等价于 */
header h1, footer h1, section h1 {
font-size: 2rem;
color: #333;
}
/* :where() 伪类 - 忽略特异性 */
:where(header, footer, section) h1 {
font-size: 2rem;
color: #333;
}
/* 实际应用示例 */
/* 简化复杂选择器 */
:is(.card, .panel, .widget) :is(h1, h2, h3) {
margin-bottom: 1rem;
color: #2c3e50;
}
/* 响应式选择器简化 */
@media (max-width: 768px) {
:is(.sidebar, .navigation, .toolbar) {
display: none;
}
}
/* 表单样式简化 */
:where(input[type="text"], input[type="email"], textarea) {
border: 1px solid #ddd;
padding: 0.5rem;
border-radius: 0.25rem;
}
/* 按钮样式简化 */
:is(.button, [role="button"], [type="button"], [type="submit"]) {
display: inline-flex;
align-items: center;
justify-content: center;
padding: 0.5rem 1rem;
border: 1px solid transparent;
border-radius: 0.25rem;
cursor: pointer;
transition: all 0.3s ease;
}
/* 特异性管理 */
.component :where(h1, h2, h3) {
/* 特异性为 0,0,1,0 */
color: blue;
}
.component h1 {
/* 特异性为 0,0,2,0 - 会覆盖上面的样式 */
color: red;
}
/* 复杂场景应用 */
:is(.dark-theme, .high-contrast) :where(a, button, [tabindex]) {
outline: 2px solid currentColor;
}
:is(.dark-theme, .high-contrast) :where(a, button, [tabindex]):focus {
outline-offset: 2px;
}
13.3.3 :has() 父选择器
/* :has() 选择器 - 根据子元素选择父元素 */
.container:has(.featured) {
border: 2px solid gold;
}
.card:has(img) {
display: flex;
flex-direction: column;
}
.form-group:has(input:invalid) {
border-color: #dc3545;
}
/* 实际应用示例 */
/* 根据内容调整布局 */
.article:has(.image-gallery) {
display: grid;
grid-template-columns: 2fr 1fr;
gap: 2rem;
}
.article:has(.video) {
max-width: 800px;
}
/* 表单验证反馈 */
.field-group:has(:invalid) {
background-color: #f8d7da;
border-left: 4px solid #dc3545;
}
.field-group:has(:valid) {
background-color: #d1edff;
border-left: 4px solid #007bff;
}
/* 交互状态反馈 */
.menu-item:has(.submenu:hover) {
background-color: #f8f9fa;
}
.dropdown:has(:focus) {
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
/* 内容检测 */
.card:has(.price:empty) {
display: none;
}
.container:has(.empty-state) {
justify-content: center;
align-items: center;
min-height: 300px;
}
/* 响应式调整 */
@media (max-width: 768px) {
.layout:has(.sidebar) {
grid-template-columns: 1fr;
}
.layout:has(.sidebar.expanded) {
grid-template-columns: 250px 1fr;
}
}
/* 复杂条件选择 */
.dashboard:has(.widget[data-priority="high"]) {
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
}
.dashboard:has(.widget[data-priority="low"]) {
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
💡 数学函数和选择器总结
- 数学函数: 使用 calc(), min(), max(), clamp() 创建动态和响应式布局
- 属性选择器: 基于元素属性进行精确选择
- 结构伪类: 根据元素在文档中的位置进行选择
- 状态伪类: 根据元素状态进行样式调整
- 高级伪类: 使用 :is(), :where(), :has() 简化复杂选择器
- 选择器组合: 通过组合选择器实现精确的样式控制
这些现代CSS特性大大增强了样式表的表达能力和灵活性,使得开发者能够创建更加动态、响应式和可维护的Web界面。