Flex & Grid 综合实践示例
1. 顶栏导航(Flex)
.header {
display: flex;
align-items: center;
gap: 24px;
padding: 0 20px;
min-height: 56px;
}
.header__brand { flex: 0 0 auto; }
.header__nav {
display: flex;
flex-wrap: wrap;
gap: 12px 16px;
flex: 1 1 auto;
min-width: 0;
}
.header__actions {
margin-left: auto;
display: flex;
gap: 8px;
flex: 0 0 auto;
}
2. 水平垂直居中(Flex)
.modal-backdrop {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
padding: 16px;
}
.modal {
width: min(480px, 100%);
}
3. 响应式卡片墙(Grid)
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 260px), 1fr));
gap: 16px;
align-content: start;
}
.card {
display: flex;
flex-direction: column;
gap: 12px;
padding: 16px;
border: 1px solid #e5e5e5;
border-radius: 8px;
}
.card__footer {
margin-top: auto;
display: flex;
justify-content: flex-end;
gap: 8px;
}
4. 后台管理骨架(Grid + Flex)
.app {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-rows: 56px minmax(0, 1fr);
grid-template-areas:
"sidebar topbar"
"sidebar content";
height: 100vh;
}
.sidebar { grid-area: sidebar; overflow: auto; }
.topbar {
grid-area: topbar;
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
padding: 0 16px;
border-bottom: 1px solid #eee;
}
.content {
grid-area: content;
min-height: 0;
overflow: auto;
padding: 24px;
}
@media (max-width: 768px) {
.app {
grid-template-columns: 1fr;
grid-template-rows: 56px minmax(0, 1fr) auto;
grid-template-areas:
"topbar"
"content"
"sidebar";
}
}
5. 圣杯式内容页(Grid areas)
.holy {
display: grid;
grid-template-columns: 200px 1fr 220px;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header header"
"nav main aside"
"footer footer footer";
gap: 16px;
min-height: 100vh;
}
.holy__header { grid-area: header; }
.holy__nav { grid-area: nav; }
.holy__main { grid-area: main; min-width: 0; }
.holy__aside { grid-area: aside; }
.holy__footer { grid-area: footer; }
@media (max-width: 900px) {
.holy {
grid-template-columns: 1fr;
grid-template-areas:
"header"
"main"
"nav"
"aside"
"footer";
}
}
6. 两栏:固定侧栏 + 自适应主区(Flex)
.split {
display: flex;
gap: 24px;
align-items: flex-start;
}
.split__side {
flex: 0 0 280px;
}
.split__main {
flex: 1 1 auto;
min-width: 0;
}
@media (max-width: 720px) {
.split { flex-direction: column; }
.split__side { flex-basis: auto; width: 100%; }
}
7. 表单标签对齐(Grid)
.form {
display: grid;
grid-template-columns: max-content 1fr;
gap: 12px 16px;
align-items: center;
}
.form label { justify-self: end; }
.form .full-row {
grid-column: 1 / -1;
}
.form .actions {
grid-column: 2;
display: flex;
gap: 8px;
justify-content: flex-start;
}
8. 媒体对象:图文横向(Flex)
.media {
display: flex;
gap: 12px;
align-items: flex-start;
}
.media__img {
flex: 0 0 64px;
width: 64px;
height: 64px;
object-fit: cover;
border-radius: 8px;
}
.media__body {
flex: 1 1 auto;
min-width: 0;
}
.media__title {
margin: 0 0 4px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
9. 英雄区跨列(Grid)
.gallery {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 120px;
gap: 12px;
}
.gallery__hero {
grid-column: span 2;
grid-row: span 2;
}
@media (max-width: 640px) {
.gallery { grid-template-columns: repeat(2, 1fr); }
.gallery__hero { grid-column: span 2; grid-row: span 1; }
}
10. 内容限宽 + 通栏破出(Grid)
.prose-layout {
display: grid;
grid-template-columns: 1fr min(65ch, calc(100% - 32px)) 1fr;
row-gap: 1.25rem;
}
.prose-layout > * {
grid-column: 2;
}
.prose-layout > .full-bleed {
grid-column: 1 / -1;
}
使用建议
- 先选对模型(见《Flex vs Grid:选型对比》),再复制模式改 token
- 凡「可收缩主区域」都带上
min-width: 0/min-height: 0或minmax(0, 1fr) - 间距优先
gap,结构优先语义化 HTML,避免靠order救响应式 - 上线前用目标最低浏览器过一遍 gap、auto-fit、滚动区域
更多原理与坑点见同库其它条目:容器/项目属性、兼容性、注意事项。