CSS Flexbox 详解
CSS Flexbox
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
flex-wrap: nowrap;
background-color: DodgerBlue;
}
.flex-container>div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>flex-wrap 属性</h1>
<p>"flex-wrap: nowrap;" 规定将不对 flex 项目换行(默认):</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<div>11</div>
<div>12</div>
</div>
<p>请尝试调整浏览器窗口的尺寸。</p>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
CSS Flexbox 布局模块
在 Flexbox 布局模块(问世)之前,可用的布局模式有以下四种:
- 块(Block),用于网页中的部分(节)
- 行内(Inline),用于文本
- 表,用于二维表数据
- 定位,用于元素的明确位置
弹性框布局模块,可以更轻松地设计灵活的响应式布局结构,而无需使用浮动或定位。
浏览器支持
所有现代浏览器均支持 flexbox
属性。
29.0 | 11.0 | 22.0 | 10 | 48 |
Flexbox 元素
如需开始使用 Flexbox 模型,您需要首先定义 Flex 容器。
上面的元素表示一个带有三个 flex 项目的 flex 容器(蓝色区域)。
示例代码:
含有三个 flex 项目的 flex 容器:
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
margin: 10px;
padding: 20px;
font-size: 30px;
}
</style>
</head>
<body>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<p>弹性布局中必须有一个 <em>display</em> 属性设置为 <em>flex</em> 的父元素。</p>
<p>弹性容器的直接子元素会自动成为弹性项目。</p>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
父元素(容器)
通过将 display
属性设置为 flex
,flex 容器将可伸缩:
示例代码:
.flex-container {
display: flex;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
margin: 10px;
padding: 20px;
font-size: 30px;
}
</style>
</head>
<body>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<p>弹性布局中必须有一个 <em>display</em> 属性设置为 <em>flex</em> 的父元素。</p>
<p>弹性容器的直接子元素会自动成为弹性项目。</p>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
以下是 flex 容器属性:
flex-direction 属性
flex-direction
属性定义容器要在哪个方向上堆叠 flex 项目。
示例代码:
column
值设置垂直堆叠 flex 项目(从上到下):
.flex-container {
display: flex;
flex-direction: column;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
flex-direction: column;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>flex-direction 属性</h1>
<p>"flex-direction: column;" 垂直堆叠弹性项目(从上到下):</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
column-reverse
值垂直堆叠 flex 项目(但从下到上):
.flex-container {
display: flex;
flex-direction: column-reverse;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
flex-direction: column-reverse;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>flex-direction 属性</h1>
<p>"flex-direction: column-reverse;" 垂直堆叠弹性项目(但是从下到上):</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
row
值水平堆叠 flex 项目(从左到右):
.flex-container {
display: flex;
flex-direction: row;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
flex-direction: row;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>flex-direction 属性</h1>
<p>"flex-direction: row;" 水平地并排弹性项目(从左到右):</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
row-reverse
值水平堆叠 flex 项目(但从右到左):
.flex-container {
display: flex;
flex-direction: row-reverse;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
flex-direction: row-reverse;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>flex-direction 属性</h1>
<p>"flex-direction: row-reverse;" 水平地并排弹性项目(但是从右向左):</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
flex-wrap 属性
flex-wrap
属性规定是否应该对 flex 项目换行。
下面的例子包含 12 个 flex 项目,以便更好地演示 flex-wrap 属性。
示例代码:
wrap
值规定 flex 项目将在必要时进行换行:
.flex-container {
display: flex;
flex-wrap: wrap;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
flex-wrap: wrap;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>flex-wrap 属性</h1>
<p>"flex-wrap: wrap;" 规定 flex 项目将在必要时进行换行:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<div>11</div>
<div>12</div>
</div>
<p>Try resizing the browser window.</p>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
nowrap
值规定将不对 flex 项目换行(默认):
.flex-container {
display: flex;
flex-wrap: nowrap;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
flex-wrap: nowrap;
background-color: DodgerBlue;
}
.flex-container>div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>flex-wrap 属性</h1>
<p>"flex-wrap: nowrap;" 规定将不对 flex 项目换行(默认):</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<div>11</div>
<div>12</div>
</div>
<p>请尝试调整浏览器窗口的尺寸。</p>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
wrap-reverse
值规定如有必要,弹性项目将以相反的顺序换行:
.flex-container {
display: flex;
flex-wrap: wrap-reverse;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
flex-wrap: wrap-reverse;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>flex-wrap 属性</h1>
<p>"flex-wrap: wrap-reverse;" 规定弹性项目将以相反的顺序换行(如有必要):</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<div>11</div>
<div>12</div>
</div>
<p>Try resizing the browser window.</p>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
flex-flow 属性
flex-flow
属性是用于同时设置 flex-direction 和 flex-wrap 属性的简写属性。
示例代码:
.flex-container {
display: flex;
flex-flow: row wrap;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
flex-flow: row wrap;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>flex-flow 属性</h1>
<p>flex-flow 属性是 flex-direction 和 flex-wrap 属性的简写属性。</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<div>11</div>
<div>12</div>
</div>
<p>请尝试调整浏览器窗口的大小。</p>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
justify-content 属性
justify-content
属性用于对齐 flex 项目:
示例代码:
center
值将 flex 项目在容器的中心对齐:
.flex-container {
display: flex;
justify-content: center;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
justify-content: center;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>justify-content 属性</h1>
<p>"justify-content: center;" 在容器中央对齐弹性项目:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
flex-start
值将 flex 项目在容器的开头对齐(默认):
.flex-container {
display: flex;
justify-content: flex-start;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
justify-content: flex-start;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>justify-content 属性</h1>
<p>"justify-content: flex-start;" 在容器开头对齐弹性项目(默认):</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
flex-end
值将 flex 项目在容器的末端对齐:
.flex-container {
display: flex;
justify-content: flex-end;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
justify-content: flex-end;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>justify-content 属性</h1>
<p>"justify-content: flex-end;" 在容器末端对齐弹性项目:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
space-around
值显示行之前、之间和之后带有空格的 flex 项目:
.flex-container {
display: flex;
justify-content: space-around;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
justify-content: space-around;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>justify-content 属性</h1>
<p>"justify-content: space-around;" 显示行之前、之间和之后带有空格的 flex 项目:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
space-between
值显示行之间有空格的 flex 项目:
.flex-container {
display: flex;
justify-content: space-between;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
justify-content: space-between;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>justify-content 属性</h1>
<p>"justify-content: space-between;" 显示行之间有空格的 flex 项目:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
align-items 属性
align-items
属性用于垂直对齐 flex 项目。
在这些例子中,我们使用 200 像素高的容器,以便更好地演示 align-items 属性。
示例代码:
center
值将 flex 项目在容器中间对齐:
.flex-container {
display: flex;
height: 200px;
align-items: center;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
height: 200px;
align-items: center;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>align-items 属性</h1>
<p>"align-items: center;" 将 flex 项目在容器中间对齐:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
flex-start
值将 flex 项目在容器顶部对齐:
.flex-container {
display: flex;
height: 200px;
align-items: flex-start;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
height: 200px;
align-items: flex-start;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>align-items 属性</h1>
<p>"align-items: flex-start;" 将 flex 项目在容器顶部对齐:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
flex-end
值将弹性项目在容器底部对齐:
.flex-container {
display: flex;
height: 200px;
align-items: flex-end;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
height: 200px;
align-items: flex-end;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>align-items 属性</h1>
<p>"align-items: flex-end;" 将弹性项目在容器底部对齐:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
stretch
值拉伸 flex 项目以填充容器(默认):
.flex-container {
display: flex;
height: 200px;
align-items: stretch;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
height: 200px;
align-items: stretch;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>align-items 属性</h1>
<p>"align-items: stretch;" 拉伸 flex 项目以填充容器(默认):</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
baseline
值使 flex 项目基线对齐:
.flex-container {
display: flex;
height: 200px;
align-items: baseline;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
height: 200px;
align-items: baseline;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>align-items 属性</h1>
<p>"align-items: baseline;" 使 flex 项目基线对齐:</p>
<div class="flex-container">
<div><h1>1</h1></div>
<div><h6>2</h6></div>
<div><h3>3</h3></div>
<div><small>4</small></div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
注意:该例使用不同的 font-size 来演示项目已按文本基线对齐:
align-content 属性
align-content
属性用于对齐弹性线。
在这些例子中,我们使用 600 像素高的容器,并将 flex-wrap 属性设置为 wrap,以便更好地演示 align-content 属性。
示例代码:
space-between
值显示的弹性线之间有相等的间距:
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: space-between;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: space-between;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>align-content 属性</h1>
<p>"align-content: space-between;" 显示的弹性线之间有相等的间距:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<div>11</div>
<div>12</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
space-around
值显示弹性线在其之前、之间和之后带有空格:
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: space-around;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: space-around;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>align-content 属性</h1>
<p>"align-content: space-around;" 显示弹性线在其之前、之间和之后带有空格:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<div>11</div>
<div>12</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
stretch
值拉伸弹性线以占据剩余空间(默认):
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: stretch;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: stretch;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>align-content 属性</h1>
<p>"align-content: stretch;" 拉伸弹性线以占据剩余空间(默认):</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<div>11</div>
<div>12</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
center
值在容器中间显示弹性线:
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: center;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: center;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>align-content 属性</h1>
<p>"align-content: center;" 在容器中间显示弹性线:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<div>11</div>
<div>12</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
flex-start
值在容器开头显示弹性线:
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: flex-start;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: flex-start;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>align-content 属性</h1>
<p>"align-content: flex-start;" 在容器开头显示弹性线:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<div>11</div>
<div>12</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
flex-end
值在容器的末尾显示弹性线:
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: flex-end;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: flex-end;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>align-content 属性</h1>
<p>"align-content: flex-end;" 在容器的末尾显示弹性线:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<div>11</div>
<div>12</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
完美的居中
在下面的例子中,我们会解决一个非常常见的样式问题:完美居中。
解决方案:将 justify-content
和 align-items
属性设置为居中,然后 flex 项目将完美居中:
示例代码:
.flex-container {
display: flex;
height: 300px;
justify-content: center;
align-items: center;
}
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
justify-content: center;
align-items: center;
height: 300px;
background-color: DodgerBlue;
}
.flex-container>div {
background-color: #f1f1f1;
color: white;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<h1>完美的居中</h1>
<p>如果容器的 justify-content 和 align-items 属性都设置为 <em>center</em>,则项目会在两个轴的方向同时居中。</p>
<div class="flex-container">
<div></div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
子元素(项目)
flex 容器的直接子元素会自动成为弹性(flex)项目。
上面的元素代表一个灰色 flex 容器内的四个蓝色 flex 项目。
示例代码:
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
background-color: #f1f1f1;
}
.flex-container > div {
background-color: DodgerBlue;
color: white;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>弹性项目</h1>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<p>弹性容器的所有直接子元素会(自动)成为弹性项目。</p>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
用于弹性项目的属性有:
order
flex-grow
flex-shrink
flex-basis
flex
align-self
order 属性
order
属性规定 flex 项目的顺序。
代码中的首个 flex 项目不必在布局中显示为第一项。
order
值必须是数字,默认值是 0。
示例代码:
order
属性可以改变 flex 项目的顺序:
<div class="flex-container">
<div style="order: 3">1</div>
<div style="order: 2">2</div>
<div style="order: 4">3</div>
<div style="order: 1">4</div>
</div>
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
align-items: stretch;
background-color: #f1f1f1;
}
.flex-container>div {
background-color: DodgerBlue;
color: white;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>order 属性</h1>
<p>使用 order 属性可以根据需要对 flex 项目进行排序:</p>
<div class="flex-container">
<div style="order: 3">1</div>
<div style="order: 2">2</div>
<div style="order: 4">3</div>
<div style="order: 1">4</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
flex-grow 属性
flex-grow
属性规定某个 flex 项目相对于其余 flex 项目将增长多少。
该值必须是数字,默认值是 0。
示例代码:
使第三个弹性项目的增长速度比其他弹性项目快八倍:
<div class="flex-container">
<div style="flex-grow: 1">1</div>
<div style="flex-grow: 1">2</div>
<div style="flex-grow: 8">3</div>
</div>
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
align-items: stretch;
background-color: #f1f1f1;
}
.flex-container > div {
background-color: DodgerBlue;
color: white;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>flex-grow 属性</h1>
<p>使第三个弹性项目的增长速度比其他弹性项目快八倍:</p>
<div class="flex-container">
<div style="flex-grow: 1">1</div>
<div style="flex-grow: 1">2</div>
<div style="flex-grow: 8">3</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
flex-shrink 属性
flex-shrink
属性规定某个 flex 项目相对于其余 flex 项目将收缩多少。
该值必须是数字,默认值是 0。
示例代码:
不要让第三个弹性项目收缩得与其他弹性项目一样多:
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="flex-shrink: 0">3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
</div>
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
align-items: stretch;
background-color: #f1f1f1;
}
.flex-container>div {
background-color: DodgerBlue;
color: white;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>flex-shrink 属性</h1>
<p>不要让第三个弹性项目收缩得与其他弹性项目一样多:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="flex-shrink: 0">3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
flex-basis 属性
flex-basis
属性规定 flex 项目的初始长度。
示例代码:
将第三个弹性项目的初始长度设置为 200 像素:
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="flex-basis: 200px">3</div>
<div>4</div>
</div>
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
align-items: stretch;
background-color: #f1f1f1;
}
.flex-container > div {
background-color: DodgerBlue;
color: white;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>flex-basis 属性</h1>
<p>将第三个弹性项目的初始长度设置为 200 像素:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="flex-basis:200px">3</div>
<div>4</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
flex 属性
flex
属性是 flex-grow、flex-shrink 和 flex-basis 属性的简写属性。
示例代码:
使第三个弹性项目不可增长(0),不可收缩(0),且初始长度为 200 像素:
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="flex: 0 0 200px">3</div>
<div>4</div>
</div>
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
align-items: stretch;
background-color: #f1f1f1;
}
.flex-container>div {
background-color: DodgerBlue;
color: white;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>flex 属性</h1>
<p>使第三个弹性项目不可增长(0),不可收缩(0),且初始长度为 200 像素:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="flex: 0 0 200px">3</div>
<div>4</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
align-self 属性
align-self
属性规定弹性容器内所选项目的对齐方式。
align-self
属性将覆盖容器的 align-items 属性所设置的默认对齐方式。
在这些例子中,我们使用 200 像素高的容器,以便更好地演示 align-self 属性:
示例代码:
把第三个弹性项目对齐到容器的中间:
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="align-self: center">3</div>
<div>4</div>
</div>
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
height: 200px;
background-color: #f1f1f1;
}
.flex-container > div {
background-color: DodgerBlue;
color: white;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>align-self 属性</h1>
<p>"align-self: center;" 在容器中间对齐所选的弹性项目:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="align-self: center">3</div>
<div>4</div>
</div>
<p>align-self 会覆盖容器的 align-items 属性。</p>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
示例代码:
将第二个弹性项目在容器顶部对齐,将第三个弹性项目在容器底部对齐:
<div class="flex-container">
<div>1</div>
<div style="align-self: flex-start">2</div>
<div style="align-self: flex-end">3</div>
<div>4</div>
</div>
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
height: 200px;
background-color: #f1f1f1;
}
.flex-container > div {
background-color: DodgerBlue;
color: white;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>align-self 属性</h1>
<p>"align-self: flex-start;" 在容器顶部对齐所选的弹性项目。</p>
<p>"align-self: flex-end;" 在容器底部对齐所选的弹性项目。</p>
<div class="flex-container">
<div>1</div>
<div style="align-self: flex-start">2</div>
<div style="align-self: flex-end">3</div>
<div>4</div>
</div>
<p>align-self 会覆盖容器的 align-items 属性。</p>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
使用 Flexbox 的响应式图库
使用 flexbox 创建响应式图像库,该图像库根据屏幕大小在四幅、两幅或全宽图像之间变化:
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: Arial;
}
.header {
text-align: center;
padding: 32px;
}
.row {
display: flex;
flex-wrap: wrap;
padding: 0 4px;
}
/* 创建并排的四个等列 */
.column {
flex: 25%;
max-width: 25%;
padding: 0 4px;
}
.column img {
margin-top: 8px;
vertical-align: middle;
}
/* 响应式布局 - 创建两列而不是四列布局 */
@media (max-width: 800px) {
.column {
flex: 50%;
max-width: 50%;
}
}
/* 响应式布局 - 创建上下堆叠而不是并排的两列布局 */
@media (max-width: 600px) {
.column {
flex: 100%;
max-width: 100%;
}
}
</style>
<body>
<!-- Header -->
<div class="header">
<h1>响应式图像网格</h1>
<p>请调整浏览器窗口,来查看响应效果。</p>
</div>
<!-- Photo Grid -->
<div class="row">
<div class="column">
<img src="/i/photo/tulip-red.jpg" style="width:100%">
<img src="/i/photo/tulip-yellow-2.jpg" style="width:100%">
<img src="/i/photo/flower-2.jpg" style="width:100%">
<img src="/i/photo/tulip-yellow.jpg" style="width:100%">
<img src="/i/photo/flower-3.jpg" style="width:100%">
<img src="/i/photo/tulip.jpg" style="width:100%">
<img src="/i/photo/flower-1.jpg" style="width:100%">
<img src="/i/photo/flower-4.jpg" style="width:100%">
</div>
<div class="column">
<img src="/i/photo/tulip-yellow-2.jpg" style="width:100%">
<img src="/i/photo/tulip.jpg" style="width:100%">
<img src="/i/photo/flower-3.jpg" style="width:100%">
<img src="/i/photo/flower-1.jpg" style="width:100%">
<img src="/i/photo/flower-4.jpg" style="width:100%">
<img src="/i/photo/tulip-red.jpg" style="width:100%">
<img src="/i/photo/tulip-yellow.jpg" style="width:100%">
<img src="/i/photo/flower-2.jpg" style="width:100%">
</div>
<div class="column">
<img src="/i/photo/tulip-red.jpg" style="width:100%">
<img src="/i/photo/tulip-yellow-2.jpg" style="width:100%">
<img src="/i/photo/flower-2.jpg" style="width:100%">
<img src="/i/photo/tulip-yellow.jpg" style="width:100%">
<img src="/i/photo/flower-3.jpg" style="width:100%">
<img src="/i/photo/tulip.jpg" style="width:100%">
<img src="/i/photo/flower-1.jpg" style="width:100%">
<img src="/i/photo/flower-4.jpg" style="width:100%">
</div>
<div class="column">
<img src="/i/photo/tulip-yellow-2.jpg" style="width:100%">
<img src="/i/photo/tulip.jpg" style="width:100%">
<img src="/i/photo/flower-3.jpg" style="width:100%">
<img src="/i/photo/flower-1.jpg" style="width:100%">
<img src="/i/photo/flower-4.jpg" style="width:100%">
<img src="/i/photo/tulip-red.jpg" style="width:100%">
<img src="/i/photo/tulip-yellow.jpg" style="width:100%">
<img src="/i/photo/flower-2.jpg" style="width:100%">
</div>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
使用 Flexbox 的响应式网站
使用 flexbox 创建响应式网站,其中包含弹性导航栏和弹性内容:
完整实例【亲自试一试】:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
box-sizing: border-box;
}
/* 设置 body 元素的样式 */
body {
font-family: Arial;
margin: 0;
}
/* 标题 / LOGO */
.header {
padding: 60px;
text-align: center;
background: #1abc9c;
color: white;
}
/* 设置顶部导航栏样式 */
.navbar {
display: flex;
background-color: #333;
}
/* 设置导航条链接演示 */
.navbar a {
color: white;
padding: 14px 20px;
text-decoration: none;
text-align: center;
}
/* 更改鼠标悬停时的颜色 */
.navbar a:hover {
background-color: #ddd;
color: black;
}
/* 列容器 */
.row {
display: flex;
flex-wrap: wrap;
}
/* 创建并排的非等列 */
/* 侧栏 / 左侧列 */
.side {
flex: 30%;
background-color: #f1f1f1;
padding: 20px;
}
/* 主列 */
.main {
flex: 70%;
background-color: white;
padding: 20px;
}
/* 伪图像,仅供示例 */
.fakeimg {
background-color: #aaa;
width: 100%;
padding: 20px;
}
/* 页脚 */
.footer {
padding: 20px;
text-align: center;
background: #ddd;
}
/* 响应式布局 - 当屏幕小于 700 像素宽时,让两列堆叠而不是并排 */
@media screen and (max-width: 700px) {
.row, .navbar {
flex-direction: column;
}
}
</style>
</head>
<body>
<!-- 注释 -->
<div style="background:yellow;padding:5px">
<h4 style="text-align:center">请调整浏览器窗口来查看响应效果。</h4>
</div>
<!-- Header -->
<div class="header">
<h1>我的网站</h1>
<p>拥有 <b>弹性</b> 布局。</p>
</div>
<!-- 导航栏 -->
<div class="navbar">
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
</div>
<!-- 弹性网格(内容) -->
<div class="row">
<div class="side">
<h2>关于我</h2>
<h5>我的照片:</h5>
<div class="fakeimg" style="height:200px;">图像</div>
<p>Some text about me in culpa qui officia deserunt mollit anim..</p>
<h3>More Text</h3>
<p>Lorem ipsum dolor sit ame.</p>
<div class="fakeimg" style="height:60px;">图像</div><br>
<div class="fakeimg" style="height:60px;">图像</div><br>
<div class="fakeimg" style="height:60px;">图像</div>
</div>
<div class="main">
<h2>标题</h2>
<h5>标题描述,2021 年 1 月 1 日</h5>
<div class="fakeimg" style="height:200px;">图像</div>
<p>一些文本..</p>
<p>Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.</p>
<br>
<h2>标题</h2>
<h5>标题描述,2021 年 1 月 2 日</h5>
<div class="fakeimg" style="height:200px;">图像</div>
<p>一些文本..</p>
<p>Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.</p>
</div>
</div>
<!-- Footer -->
<div class="footer">
<h2>页脚</h2>
</div>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
CSS Flexbox 属性
下表列出了与 flexbox 一起使用的 CSS 属性:
属性 | 描述 |
---|---|
display | 规定用于 HTML 元素的盒类型。 |
flex-direction | 规定弹性容器内的弹性项目的方向。 |
justify-content | 当弹性项目没有用到主轴上的所有可用空间时,水平对齐这些项目。 |
align-items | 当弹性项目没有用到主轴上的所有可用空间时,垂直对齐这些项。 |
flex-wrap | 规定弹性项目是否应该换行,若一条 flex 线上没有足够的空间容纳它们。 |
align-content | 修改 flex-wrap 属性的行为。与 align-items 相似,但它不对齐弹性项目,而是对齐 flex 线。 |
flex-flow | flex-direction 和 flex-wrap 的简写属性。 |
order | 规定弹性项目相对于同一容器内其余弹性项目的顺序。 |
align-self | 用于弹性项目。覆盖容器的 align-items 属性。 |
flex | flex-grow、flex-shrink 以及 flex-basis 属性的简写属性。 |