惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
爱范儿
爱范儿
H
Help Net Security
V
Visual Studio Blog
J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
MyScale Blog
MyScale Blog
The Cloudflare Blog
Martin Fowler
Martin Fowler
D
Docker
腾讯CDC
F
Fortinet All Blogs
雷峰网
雷峰网
GbyAI
GbyAI
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Announcements
Recent Announcements
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Blog — PlanetScale
Blog — PlanetScale
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
博客园 - 叶小钗

ACCB

Markdown 告警组件 Astro 静态网站生成器入门指南 现代前端开发趋势:2025年值得关注的技术 零基础学 Python:从入门到实战 现代前端开发工具链配置指南 为 Astro Cactus 添加网络提及功能 Markdown 元素展示 封面图片示例 这篇文章没有任何内容 这是一个非常长的标题用于测试页面布局和CSS样式的正确性 唯一标签验证 社交图片示例
CSS Grid 与 Flexbox:选择合适的布局方案
lbb · 2024-12-21 · via ACCB

布局的演进

在现代 CSS 中,我们有两个强大的布局工具:FlexboxCSS Grid。虽然它们有一些重叠的功能,但每个都有其独特的优势和适用场景。

Flexbox:一维布局的专家

特点

Flexbox 专为一维布局设计,无论是水平还是垂直方向。

.flex-container {

display: flex;

flex-direction: row; /* 或 column */

justify-content: space-between;

align-items: center;

}

适用场景

  1. 导航栏布局

.navbar {

display: flex;

justify-content: space-between;

align-items: center;

}

  1. 居中对齐

.center {

display: flex;

justify-content: center;

align-items: center;

height: 100vh;

}

  1. 弹性项目分布

.flexible-items {

display: flex;

gap: 1rem;

}

.flexible-items > * {

flex: 1; /* 等分空间 */

}

CSS Grid:二维布局的王者

特点

CSS Grid 为二维布局而生,可以同时控制行和列。

.grid-container {

display: grid;

grid-template-columns: 1fr 2fr 1fr;

grid-template-rows: auto 1fr auto;

gap: 1rem;

}

适用场景

  1. 页面整体布局

.page-layout {

display: grid;

grid-template-areas:

"header header header"

"sidebar main aside"

"footer footer footer";

grid-template-columns: 200px 1fr 200px;

grid-template-rows: auto 1fr auto;

min-height: 100vh;

}

.header { grid-area: header; }

.sidebar { grid-area: sidebar; }

.main { grid-area: main; }

.aside { grid-area: aside; }

.footer { grid-area: footer; }

  1. 卡片网格

.card-grid {

display: grid;

grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));

gap: 2rem;

}

  1. 复杂的表单布局

.form-grid {

display: grid;

grid-template-columns: 1fr 1fr;

gap: 1rem;

}

.full-width {

grid-column: 1 / -1;

}

实战对比

案例 1:导航栏

Flexbox 方案(推荐):

.navbar {

display: flex;

justify-content: space-between;

align-items: center;

padding: 1rem;

}

.nav-links {

display: flex;

gap: 2rem;

list-style: none;

}

Grid 方案(过度设计):

.navbar {

display: grid;

grid-template-columns: auto 1fr auto;

align-items: center;

padding: 1rem;

}

案例 2:响应式卡片布局

Grid 方案(推荐):

.cards {

display: grid;

grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));

gap: 2rem;

}

Flexbox 方案(复杂):

.cards {

display: flex;

flex-wrap: wrap;

gap: 2rem;

}

.card {

flex: 1 1 280px;

}

组合使用

在实际项目中,最佳实践是将两者结合使用:

/* Grid 用于页面整体布局 */

.page {

display: grid;

grid-template-areas:

"header"

"main"

"footer";

grid-template-rows: auto 1fr auto;

min-height: 100vh;

}

/* Flexbox 用于组件内部布局 */

.header {

display: flex;

justify-content: space-between;

align-items: center;

}

.card {

display: flex;

flex-direction: column;

gap: 1rem;

}

选择指南

选择 Flexbox 当:

  • ✅ 需要一维布局(行或列)
  • ✅ 项目大小灵活
  • ✅ 需要对齐和分布控制
  • ✅ 响应式组件布局

选择 Grid 当:

  • ✅ 需要二维布局(行和列)
  • ✅ 复杂的页面布局
  • ✅ 精确的网格对齐
  • ✅ 重叠元素定位

浏览器支持

两者都有良好的现代浏览器支持:

  • Flexbox: IE 10+(需要前缀)
  • Grid: IE 10+(旧语法),现代浏览器完全支持

总结

  • Flexbox 是组件级布局的首选
  • Grid 是页面级布局的理想选择
  • 两者结合使用效果最佳
  • 根据具体需求选择合适的工具

记住:工具本身没有好坏,关键是在正确的场景使用正确的工具!