CSS 媒体查询是响应式网页设计的基石,允许你根据设备特性(如视口宽度、方向和用户偏好)应用样式。在 2025 年,CSS 媒体查询和断点已经有了重大进化,包括新的范围语法、容器查询和基于偏好的特性。本综合指南涵盖了构建真正响应式网站所需的一切。
媒体查询语法
媒体查询由媒体类型和一个或多个检查条件的表达式组成。当条件为真时,查询内的样式会被应用。
/* Basic media query syntax */
@media media-type and (media-feature) {
/* CSS rules here */
}
/* Examples */
@media screen and (min-width: 768px) {
.container { max-width: 720px; }
}
@media print {
.no-print { display: none; }
}
/* Without media type (applies to all) */
@media (min-width: 768px) {
.sidebar { display: block; }
}你可以使用逻辑运算符组合条件:and 要求所有条件为真,or(逗号分隔)要求至少一个为真,not 否定整个查询。
/* AND — all conditions must be true */
@media screen and (min-width: 768px) and (max-width: 1024px) {
/* tablet only */
}
/* OR — comma-separated, at least one must be true */
@media (max-width: 480px), (orientation: portrait) {
/* mobile OR portrait */
}
/* NOT — negates the entire query */
@media not print {
/* everything except print */
}
/* ONLY — prevents older browsers from applying styles */
@media only screen and (min-width: 768px) {
/* modern browsers only */
}2025 年推荐断点
2025 年的设备格局已经发生了变化。这些断点基于当前使用数据覆盖了最常见的屏幕尺寸。关键是先为内容设计,然后在布局出现问题的地方添加断点——但这些值是很好的起点。
| Device | Breakpoint | CSS (Mobile-First) | Common Devices |
|---|---|---|---|
| Mobile (portrait) | 0 – 479px | Default styles (no query) | iPhone SE, Galaxy S series |
| Mobile (landscape) | 480px | @media (min-width: 480px) | Phones in landscape |
| Tablet | 768px | @media (min-width: 768px) | iPad Mini, iPad Air, Galaxy Tab |
| Laptop | 1024px | @media (min-width: 1024px) | iPad Pro, small laptops |
| Desktop | 1280px | @media (min-width: 1280px) | Standard desktop monitors |
| Large Desktop / 4K | 1536px | @media (min-width: 1536px) | Large monitors, 4K displays |
/* Complete mobile-first breakpoint system */
/* Base: Mobile portrait (0–479px) */
.container { padding: 0 16px; }
/* 480px: Mobile landscape */
@media (min-width: 480px) {
.container { max-width: 460px; margin: 0 auto; }
}
/* 768px: Tablet */
@media (min-width: 768px) {
.container { max-width: 720px; }
}
/* 1024px: Laptop */
@media (min-width: 1024px) {
.container { max-width: 960px; }
}
/* 1280px: Desktop */
@media (min-width: 1280px) {
.container { max-width: 1200px; }
}
/* 1536px: Large desktop / 4K */
@media (min-width: 1536px) {
.container { max-width: 1400px; }
}移动优先 vs 桌面优先
编写响应式 CSS 有两种主要方法:移动优先(使用 min-width)和桌面优先(使用 max-width)。移动优先是行业标准,也是 2025 年推荐的方法。
为什么选择移动优先?移动流量占全球网络流量的 60% 以上。从移动样式开始意味着你的基础 CSS 更简洁,在低功耗设备上加载更快,并且你可以逐步增强以适应更大的屏幕。
/* ========== MOBILE-FIRST (Recommended) ========== */
/* Base styles: mobile */
.grid {
display: flex;
flex-direction: column;
gap: 16px;
}
.grid-item {
width: 100%;
}
/* Tablet: 2 columns */
@media (min-width: 768px) {
.grid {
flex-direction: row;
flex-wrap: wrap;
}
.grid-item {
width: calc(50% - 8px);
}
}
/* Desktop: 3 columns */
@media (min-width: 1024px) {
.grid-item {
width: calc(33.333% - 11px);
}
}
/* ========== DESKTOP-FIRST (Less common) ========== */
/* Base styles: desktop */
.grid {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
.grid-item {
width: calc(33.333% - 11px);
}
/* Tablet: 2 columns */
@media (max-width: 1023px) {
.grid-item {
width: calc(50% - 8px);
}
}
/* Mobile: 1 column */
@media (max-width: 767px) {
.grid {
flex-direction: column;
}
.grid-item {
width: 100%;
}
}宽度查询:经典语法和新范围语法
基于宽度的媒体查询是最常用的。在 2025 年,所有现代浏览器都支持 Media Queries Level 4 中引入的新范围语法,它更具可读性且不容易出错。
新的范围语法使用数学比较运算符(>=、<=、>、<),在 Chrome 104+、Firefox 63+、Safari 16.4+ 和 Edge 104+ 中受支持。
/* ===== Traditional syntax (widely supported) ===== */
@media (min-width: 768px) { /* 768px and up */ }
@media (max-width: 767px) { /* below 768px */ }
@media (min-width: 768px) and (max-width: 1023px) { /* 768–1023px */ }
/* ===== New range syntax (2025 — all modern browsers) ===== */
@media (width >= 768px) { /* 768px and up */ }
@media (width < 768px) { /* below 768px */ }
@media (768px <= width < 1024px) { /* 768–1023px */ }
/* More examples of range syntax */
@media (width >= 480px) {
/* mobile landscape and up */
}
@media (width >= 768px) and (width < 1024px) {
/* tablet only */
}
@media (height >= 800px) {
/* tall viewports */
}
/* Range syntax is more intuitive for ranges */
/* Old: min and max are confusing with "and" */
@media (min-width: 768px) and (max-width: 1279px) { ... }
/* New: reads naturally as a mathematical range */
@media (768px <= width <= 1279px) { ... }其他媒体特性
除了宽度之外,CSS 媒体查询可以检测许多设备特性和用户偏好。这些特性对于构建无障碍、用户友好的界面至关重要。
orientation(方向)
检测视口是纵向(高度 > 宽度)还是横向(宽度 > 高度)模式。
/* Portrait mode */
@media (orientation: portrait) {
.gallery {
grid-template-columns: 1fr;
}
}
/* Landscape mode */
@media (orientation: landscape) {
.gallery {
grid-template-columns: repeat(3, 1fr);
}
}
/* Combine with width */
@media (orientation: landscape) and (max-height: 500px) {
/* landscape on a phone — reduce vertical padding */
.hero { padding: 20px 0; }
}hover 和 pointer
检测主要输入机制的能力。对于区分触摸设备和鼠标设备至关重要。
/* Device has hover capability (mouse/trackpad) */
@media (hover: hover) {
.button:hover {
background-color: #2563eb;
transform: translateY(-2px);
}
}
/* Device does NOT have hover (touchscreen) */
@media (hover: none) {
.button {
/* larger touch target */
padding: 16px 24px;
font-size: 18px;
}
}
/* Fine pointer (mouse) */
@media (pointer: fine) {
.input { height: 32px; }
}
/* Coarse pointer (touch/finger) */
@media (pointer: coarse) {
.input { height: 48px; } /* larger touch target */
}
/* Combine for precise detection */
@media (hover: hover) and (pointer: fine) {
/* desktop with mouse — show tooltips on hover */
.tooltip:hover .tooltip-text { display: block; }
}prefers-color-scheme(偏好配色方案)
检测用户偏好的配色方案(浅色或深色)。这对于实现暗色模式至关重要。
/* Define color tokens with CSS custom properties */
:root {
--bg: #ffffff;
--text: #1a1a1a;
--card-bg: #f8fafc;
--border: #e2e8f0;
--primary: #2563eb;
}
/* Dark mode overrides */
@media (prefers-color-scheme: dark) {
:root {
--bg: #0f172a;
--text: #f1f5f9;
--card-bg: #1e293b;
--border: #334155;
--primary: #60a5fa;
}
}
/* Use the tokens everywhere */
body {
background-color: var(--bg);
color: var(--text);
}
.card {
background-color: var(--card-bg);
border: 1px solid var(--border);
}
/* Manual toggle override with data attribute */
[data-theme="dark"] {
--bg: #0f172a;
--text: #f1f5f9;
--card-bg: #1e293b;
--border: #334155;
--primary: #60a5fa;
}prefers-reduced-motion(偏好减少动画)
检测用户是否在系统设置中请求减少动画。这对于无障碍访问很重要——务必尊重此偏好。
/* Default: add animations */
.element {
transition: transform 0.3s ease, opacity 0.3s ease;
}
.element:hover {
transform: scale(1.05);
}
@keyframes slide-in {
from { transform: translateX(-100%); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}
.animated {
animation: slide-in 0.5s ease-out;
}
/* Reduce or remove motion when user prefers it */
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}
/* Alternative: opt-in approach (safer) */
@media (prefers-reduced-motion: no-preference) {
.element {
transition: transform 0.3s ease;
}
}aspect-ratio(宽高比)
针对具有特定宽高比的视口。适用于超宽显示器或特定的设备形态。
/* Ultra-wide monitors (21:9 or wider) */
@media (min-aspect-ratio: 21/9) {
.hero {
max-width: 1600px;
margin: 0 auto;
}
}
/* Standard widescreen (16:9) */
@media (aspect-ratio: 16/9) {
.video-container { padding: 0; }
}
/* Tall/narrow viewports (phones) */
@media (max-aspect-ratio: 3/4) {
.sidebar { display: none; }
}容器查询
容器查询是近年来最大的 CSS 新增功能之一。它不是查询视口大小,而是让你根据父容器的大小来设置元素样式。这使得组件在不同的布局上下文中真正可复用。
container-type: inline-size 启用基于宽度的容器查询。container-type: size 同时启用宽度和高度查询,但浏览器计算成本更高。大多数情况下使用 inline-size。
/* Step 1: Define a container */
.card-wrapper {
container-type: inline-size; /* enable width-based queries */
container-name: card; /* optional: name for targeting */
}
/* Shorthand */
.card-wrapper {
container: card / inline-size;
}
/* Step 2: Query the container */
@container card (min-width: 400px) {
.card {
display: flex;
flex-direction: row;
}
.card-image {
width: 40%;
}
.card-content {
width: 60%;
}
}
@container card (min-width: 600px) {
.card {
gap: 24px;
}
.card-title {
font-size: 1.5rem;
}
}
/* Container query with range syntax */
@container (width >= 300px) {
.component { padding: 24px; }
}
/* Container query units */
.card-title {
/* cqw = 1% of container width */
font-size: clamp(1rem, 3cqw, 1.5rem);
}
/* Practical example: sidebar vs main content */
.sidebar .card-wrapper {
/* Container is narrow → card stacks vertically */
container-type: inline-size;
}
.main .card-wrapper {
/* Container is wide → card goes horizontal */
container-type: inline-size;
}
/* Same @container rules work in both contexts! */热门框架断点对比
以下是 2025 年流行的 CSS 框架如何定义它们的断点。了解这些有助于在框架之间迁移或为项目选择框架。
| Breakpoint | Tailwind CSS | Bootstrap 5 | Material UI |
|---|---|---|---|
| Extra Small | — | <576px | 0px (xs) |
| Small | 640px (sm) | 576px (sm) | 600px (sm) |
| Medium | 768px (md) | 768px (md) | 900px (md) |
| Large | 1024px (lg) | 992px (lg) | 1200px (lg) |
| Extra Large | 1280px (xl) | 1200px (xl) | 1536px (xl) |
| 2X Large | 1536px (2xl) | 1400px (xxl) | — |
/* Tailwind CSS — using classes directly */
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
<!-- responsive grid -->
</div>
/* Bootstrap 5 — using column classes */
<div class="row">
<div class="col-12 col-sm-6 col-lg-4 col-xl-3">
<!-- responsive column -->
</div>
</div>
/* Custom: matching Tailwind breakpoints in plain CSS */
/* sm */ @media (min-width: 640px) { ... }
/* md */ @media (min-width: 768px) { ... }
/* lg */ @media (min-width: 1024px) { ... }
/* xl */ @media (min-width: 1280px) { ... }
/* 2xl */ @media (min-width: 1536px) { ... }响应式排版
响应式排版确保文本在所有屏幕尺寸上都清晰可读。在 2025 年,clamp() 函数是流体排版的黄金标准,无需多个媒体查询断点。
clamp(最小值, 首选值, 最大值) 函数接受三个值:最小尺寸、首选(流体)尺寸和最大尺寸。首选值通常使用视口单位来随屏幕尺寸缩放。
/* ===== clamp() — fluid typography (recommended) ===== */
h1 {
/* min: 2rem, preferred: 5vw, max: 3.5rem */
font-size: clamp(2rem, 5vw, 3.5rem);
}
h2 {
font-size: clamp(1.5rem, 3.5vw, 2.5rem);
}
p {
font-size: clamp(1rem, 1.2vw, 1.25rem);
line-height: 1.6;
}
/* ===== Complete fluid type scale ===== */
:root {
--text-xs: clamp(0.75rem, 0.7rem + 0.25vw, 0.875rem);
--text-sm: clamp(0.875rem, 0.8rem + 0.35vw, 1rem);
--text-base: clamp(1rem, 0.9rem + 0.5vw, 1.125rem);
--text-lg: clamp(1.125rem, 1rem + 0.6vw, 1.25rem);
--text-xl: clamp(1.25rem, 1rem + 1.2vw, 1.75rem);
--text-2xl: clamp(1.5rem, 1rem + 2.5vw, 2.5rem);
--text-3xl: clamp(2rem, 1.2rem + 4vw, 3.5rem);
}
/* ===== Traditional breakpoint approach ===== */
h1 { font-size: 1.75rem; }
@media (min-width: 768px) {
h1 { font-size: 2.25rem; }
}
@media (min-width: 1024px) {
h1 { font-size: 3rem; }
}
/* ===== Viewport units (use with caution) ===== */
h1 {
/* Never use vw alone — inaccessible (ignores zoom) */
font-size: 5vw; /* BAD */
/* Combine with calc or clamp for accessibility */
font-size: calc(1rem + 2vw); /* OK */
font-size: clamp(1.5rem, 4vw, 3rem); /* BEST */
}图片响应式
响应式图片对性能和用户体验至关重要。srcset 属性、sizes 属性和 <picture> 元素协同工作,为每个设备提供合适的图片。
srcset 向浏览器提供图片源列表及其尺寸,让浏览器选择最佳的一个。
<!-- srcset with width descriptors -->
<img
src="image-800.jpg"
srcset="
image-400.jpg 400w,
image-800.jpg 800w,
image-1200.jpg 1200w,
image-1600.jpg 1600w
"
sizes="
(min-width: 1280px) 1200px,
(min-width: 768px) 90vw,
100vw
"
alt="Responsive image example"
loading="lazy"
/>
<!-- srcset with pixel density descriptors -->
<img
src="logo.png"
srcset="
logo.png 1x,
logo@2x.png 2x,
logo@3x.png 3x
"
alt="Company logo"
/><picture> 元素提供艺术指导——根据媒体条件提供完全不同的图片。
<!-- Art direction with <picture> -->
<picture>
<!-- Wide screens: panoramic hero -->
<source
media="(min-width: 1024px)"
srcset="hero-wide.jpg"
/>
<!-- Tablet: cropped version -->
<source
media="(min-width: 768px)"
srcset="hero-medium.jpg"
/>
<!-- Mobile: square crop -->
<source
media="(max-width: 767px)"
srcset="hero-mobile.jpg"
/>
<!-- Fallback -->
<img src="hero-medium.jpg" alt="Hero image" />
</picture>
<!-- Format switching with <picture> -->
<picture>
<source type="image/avif" srcset="image.avif" />
<source type="image/webp" srcset="image.webp" />
<img src="image.jpg" alt="Modern format with fallback" />
</picture>
/* CSS: responsive images */
img {
max-width: 100%;
height: auto;
display: block;
}
/* Object-fit for fixed-aspect containers */
.thumbnail {
width: 300px;
height: 200px;
object-fit: cover; /* crop to fill */
object-position: center; /* crop from center */
}测试响应式设计
在各种设备上测试响应式布局至关重要。以下是 2025 年最有效的工具和技巧。
Chrome DevTools 设备工具栏:按 Ctrl+Shift+M(Windows/Linux)或 Cmd+Shift+M(Mac)切换设备工具栏。你可以选择预设设备、设置自定义尺寸、限制网络速度和模拟触摸事件。
Firefox 响应式设计模式:按 Ctrl+Shift+M 进入响应式设计模式。Firefox 提供 DPR 模拟和触摸模拟。
在实际断点处测试,而不仅仅是预设设备尺寸。缓慢调整浏览器窗口大小,观察布局断裂点。
/* Debug helper: show current breakpoint */
body::after {
content: 'MOBILE';
position: fixed;
bottom: 8px;
right: 8px;
background: #ef4444;
color: white;
padding: 4px 8px;
font-size: 12px;
border-radius: 4px;
z-index: 9999;
}
@media (min-width: 480px) {
body::after { content: 'MOBILE-L'; background: #f97316; }
}
@media (min-width: 768px) {
body::after { content: 'TABLET'; background: #eab308; }
}
@media (min-width: 1024px) {
body::after { content: 'LAPTOP'; background: #22c55e; }
}
@media (min-width: 1280px) {
body::after { content: 'DESKTOP'; background: #3b82f6; }
}
@media (min-width: 1536px) {
body::after { content: 'LARGE'; background: #8b5cf6; }
}
/* Remove in production! */
@media print {
body::after { display: none; }
}常见响应式模式
以下是实际项目中最常用的响应式设计模式。
在移动端显示/隐藏
根据屏幕尺寸切换元素的可见性。谨慎使用——隐藏的内容不应该是移动用户的必要内容。
/* Hide on mobile, show on desktop */
.desktop-only {
display: none;
}
@media (min-width: 768px) {
.desktop-only {
display: block; /* or flex, grid, etc. */
}
}
/* Show on mobile, hide on desktop */
.mobile-only {
display: block;
}
@media (min-width: 768px) {
.mobile-only {
display: none;
}
}
/* Visually hidden but accessible to screen readers */
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}移动端堆叠列
在较小屏幕上将多列布局转换为单列堆叠布局。
/* Stacked on mobile, side-by-side on tablet+ */
.two-column {
display: flex;
flex-direction: column;
gap: 24px;
}
@media (min-width: 768px) {
.two-column {
flex-direction: row;
}
.two-column > .main-content {
flex: 2;
}
.two-column > .sidebar {
flex: 1;
}
}
/* Three columns with container queries */
.card-grid {
container-type: inline-size;
display: grid;
gap: 16px;
grid-template-columns: 1fr;
}
@container (min-width: 500px) {
.card-grid {
grid-template-columns: repeat(2, 1fr);
}
}
@container (min-width: 800px) {
.card-grid {
grid-template-columns: repeat(3, 1fr);
}
}响应式导航
在移动设备上将水平导航转换为汉堡菜单。
/* Mobile: hamburger menu */
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px 16px;
}
.nav-links {
display: none; /* hidden by default on mobile */
flex-direction: column;
position: absolute;
top: 60px;
left: 0;
right: 0;
background: var(--bg);
padding: 16px;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}
.nav-links.active {
display: flex; /* toggled by JavaScript */
}
.hamburger {
display: block;
cursor: pointer;
}
/* Desktop: horizontal nav, no hamburger */
@media (min-width: 768px) {
.nav-links {
display: flex;
flex-direction: row;
position: static;
background: none;
box-shadow: none;
gap: 24px;
padding: 0;
}
.hamburger {
display: none;
}
}
/* Responsive font sizes in nav */
.nav-link {
font-size: 1rem;
padding: 12px 0;
}
@media (min-width: 768px) {
.nav-link {
font-size: 0.875rem;
padding: 8px 12px;
}
}常见问题
2025 年最佳的 CSS 断点是什么?
2025 年推荐的断点是:480px(手机横屏)、768px(平板)、1024px(笔记本)、1280px(桌面显示器)和 1536px(大屏/4K)。不过,最佳实践是在内容实际出现断裂的地方设置断点,而不是针对特定设备。将这些值作为起点,根据你的设计进行调整。
媒体查询应该用 min-width 还是 max-width?
大多数情况下使用 min-width(移动优先方法)。这是行业标准,因为它从最简单的移动设备样式开始,逐步增强以适应更大的屏幕。移动优先的 CSS 通常更简洁、性能更好。唯一的例外是在现有的纯桌面网站上添加响应式样式时,max-width(桌面优先)可能更实用。
媒体查询和容器查询有什么区别?
媒体查询响应视口(浏览器窗口)大小,而容器查询响应父元素的大小。容器查询使组件真正可复用,因为卡片组件可以根据其放置位置(侧边栏 vs 主内容区)来适应,而不仅仅是视口大小。页面级布局使用媒体查询,组件级响应使用容器查询。
如何用 CSS 媒体查询实现暗色模式?
使用 prefers-color-scheme 媒体特性:@media (prefers-color-scheme: dark) { /* 暗色样式 */ }。为颜色令牌定义 CSS 自定义属性(变量),并在暗色模式查询中切换它们。同时提供一个使用 CSS 类或 data 属性的手动切换,覆盖系统偏好,给用户完全控制权。
新的媒体查询范围语法可以在生产环境使用吗?
可以,新的范围语法(例如 @media (width >= 768px))在所有现代浏览器中均受支持:Chrome 104+、Firefox 63+、Safari 16.4+ 和 Edge 104+。截至 2025 年,这涵盖了全球 95% 以上的用户。如果需要支持旧浏览器,可以使用传统的 min-width/max-width 语法作为后备,或使用 PostCSS 插件自动转译范围语法。
CSS 媒体查询和断点是 2025 年构建响应式网站的必备工具。借助新的范围语法、容器查询和基于偏好的特性,你拥有比以往更强大的能力来创建自适应布局。使用移动优先方法,彻底测试,并始终尊重用户偏好以获得最佳体验。