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

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

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 是页面级布局的理想选择
  • 两者结合使用效果最佳
  • 根据具体需求选择合适的工具

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