Flexbox Centering 练习
你将构建什么
HTML page 使用 3 boxes demonstrating horizontal centering, vertical centering, and 完整 centering (两者都 axes) using flexbox.
学习目标
- Use justify-内容到 中心 on 主要 axis
- Use align-项到 中心 on cross axis
- 组合两者都 for 完整 centering
- 理解如何 flex-方向 affects 哪个 axis 是哪个
前置要求
- HTML 结构
- 基础 CSS 包含 display: flex
步骤
- 1
设置 HTML
创建 centering.HTML 使用 3 demo 容器, 每个使用 child box 内部.
代码<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Flexbox Centering</title> <link rel="stylesheet" href="centering.css"> </head> <body> <div class="demo horizontal"> <div class="box">H-Center</div> </div> <div class="demo vertical"> <div class="box">V-Center</div> </div> <div class="demo both"> <div class="box">Centered</div> </div> </body> </html>预期结果
Three 容器每个使用 box 内部, 否样式 yet.
- 2
样式 demo 容器 and boxes
创建 centering.CSS. Give 每个 demo fixed 高度 and background, and box 可见样式.
代码.demo { display: flex; height: 150px; margin-bottom: 1rem; background: #f1f5f9; border: 2px dashed #94a3b8; } .box { background: #2563eb; color: #fff; padding: 1rem 2rem; border-radius: 0.5rem; }预期结果
Three gray 容器使用 blue boxes at 左边缘.
- 3
添加 horizontal centering
For .horizontal demo, use justify-内容: 中心到 中心 box on 主要 axis (horizontal in 行).
代码.horizontal { justify-content: center; }预期结果
首先 box 是 horizontally centered in 它的容器.
- 4
添加 vertical and 完整 centering
For .vertical use align-项: 中心. For .两者都 use 两者都 justify-内容 and align-项中心.
代码.vertical { align-items: center; } .both { justify-content: center; align-items: center; }预期结果
其次 box 是 vertically centered, 第三 box 是 centered on 两者都 axes.
提示
使用 flex-方向: 行 (默认), justify-内容 controls horizontal and align-项 controls vertical.
- 5
添加响应式样式
使用媒体查询为移动端调整布局:在小屏幕上将多列改为单列、缩小字号、调整间距。移动优先意味着小屏幕体验同样重要。
代码@media (max-width: 640px) { .container { flex-direction: column; padding: 1rem; } h1 { font-size: 1.5rem; } .card { width: 100%; margin-bottom: 1rem; } }预期结果
在 640px 以下视口,布局变为单列,字号和间距缩小,内容不溢出。
自己动手试试
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flexbox Centering</title>
<link rel="stylesheet" href="centering.css">
</head>
<body>
<!-- TODO: 3 demo containers -->
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flexbox Centering</title>
<link rel="stylesheet" href="centering.css">
</head>
<body>
<div class="demo horizontal"><div class="box">H-Center</div></div>
<div class="demo vertical"><div class="box">V-Center</div></div>
<div class="demo both"><div class="box">Centered</div></div>
</body>
</html>
/* centering.css */
.demo { display: flex; height: 150px; margin-bottom: 1rem; background: #f1f5f9; border: 2px dashed #94a3b8; }
.box { background: #2563eb; color: #fff; padding: 1rem 2rem; border-radius: 0.5rem; }
.horizontal { justify-content: center; }
.vertical { align-items: center; }
.both { justify-content: center; align-items: center; }你学到了什么
你 mastered flexbox centering — 大多数常见 CSS 布局任务. justify-内容 centers on 主要 axis, align-项 on cross axis, and 组合两者都 centers perfectly in 两者都方向.
相关资源
相关课程
- CSS Flexbox
Flexbox alignment 属性.
相关练习
- 完整 Flex Centering
练习 flex centering.
- Predict Flex Justify-内容
Predict flex 布局 results.
相关速查表
- CSS Cheatsheet
Flexbox 引用.
相关术语
- CSS
Cascading 样式 Sheets.
相关术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
- CSS
Cascading 样式 Sheets.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。