Flex 布局写法教程 - 介绍了Flex布局的语法,今天介绍常见布局的Flex写法。html
你会看到,无论是什么布局,Flex每每均可以几行命令搞定。程序员
我只列出代码,详细的语法解释请查阅《Flex布局教程:语法篇》。个人主要参考资料是Landon Schropp的文章和Solved by Flexbox。bash
骰子的一面,最多能够放置9个点。ide
下面,就来看看Flex如何实现,从1个点到9个点的布局。你能够到codepen查看Demo。工具
若是不加说明,本节的HTML模板一概以下。布局
<div class="box">
<span class="item"></span>
</div>
复制代码
上面代码中,div元素(表明骰子的一个面)是Flex容器,span元素(表明一个点)是Flex项目。若是有多个项目,就要添加多个span元素,以此类推。学习
首先,只有左上角1个点的状况。Flex布局默认就是首行左对齐,因此一行代码就够了。flex
.box {
display: flex;
}
复制代码
设置项目的对齐方式,就能实现居中对齐和右对齐。网站
.box {
display: flex;
justify-content: center;
}
复制代码
.box {
display: flex;
justify-content: flex-end;
}
复制代码
设置交叉轴对齐方式,能够垂直移动主轴。spa
.box {
display: flex;
align-items: center;
}
复制代码
.box {
display: flex;
justify-content: center;
align-items: center;
}
复制代码
.box {
display: flex;
justify-content: center;
align-items: flex-end;
}
复制代码
.box {
display: flex;
justify-content: flex-end;
align-items: flex-end;
}
复制代码
.box {
display: flex;
justify-content: space-between;
}
复制代码
.box {
display: flex;
flex-direction: column;
justify-content: space-between;
}
复制代码
.box {
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
}
复制代码
.box {
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: flex-end;
}
复制代码
.box {
display: flex;
}
.item:nth-child(2) {
align-self: center;
}
复制代码
.box {
display: flex;
justify-content: space-between;
}
.item:nth-child(2) {
align-self: flex-end;
}
复制代码
.box {
display: flex;
}
.item:nth-child(2) {
align-self: center;
}
.item:nth-child(3) {
align-self: flex-end;
}
复制代码
.box {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
align-content: space-between;
}
复制代码
HTML代码以下:
<div class="box">
<div class="column">
<span class="item"></span>
<span class="item"></span>
</div>
<div class="column">
<span class="item"></span>
<span class="item"></span>
</div>
</div>
复制代码
CSS代码以下:
.box {
display: flex;
flex-wrap: wrap;
align-content: space-between;
}
.column {
flex-basis: 100%;
display: flex;
justify-content: space-between;
}
复制代码
.box {
display: flex;
flex-wrap: wrap;
align-content: space-between;
}
复制代码
.box {
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-content: space-between;
}
复制代码
HTML代码以下:
<div class="box">
<div class="row">
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
</div>
<div class="row">
<span class="item"></span>
</div>
<div class="row">
<span class="item"></span>
<span class="item"></span>
</div>
</div>
复制代码
CSS代码以下:
.box {
display: flex;
flex-wrap: wrap;
}
.row{
flex-basis: 100%;
display:flex;
}
.row:nth-child(2){
justify-content: center;
}
.row:nth-child(3){
justify-content: space-between;
}
复制代码
.box {
display: flex;
flex-wrap: wrap;
}
复制代码
最简单的网格布局,就是平均分布。在容器里面平均分配空间,跟上面的骰子布局很像,可是须要设置项目的自动缩放。
HTML代码以下:
<div class="Grid">
<div class="Grid-cell">...</div>
<div class="Grid-cell">...</div>
<div class="Grid-cell">...</div>
</div>
复制代码
CSS代码以下:
.Grid {
display: flex;
}
.Grid-cell {
flex: 1;
}
复制代码
某个网格的宽度为固定的百分比,其他网格平均分配剩余的空间。
HTML代码以下:
<div class="Grid">
<div class="Grid-cell u-1of4">...</div>
<div class="Grid-cell">...</div>
<div class="Grid-cell u-1of3">...</div>
</div>
复制代码
CSS代码以下:
.Grid {
display: flex;
}
.Grid-cell {
flex: 1;
}
.Grid-cell.u-full {
flex: 0 0 100%;
}
.Grid-cell.u-1of2 {
flex: 0 0 50%;
}
.Grid-cell.u-1of3 {
flex: 0 0 33.3333%;
}
.Grid-cell.u-1of4 {
flex: 0 0 25%;
}
复制代码
圣杯布局(Holy Grail Layout)指的是一种最多见的网站布局。页面从上到下,分红三个部分:头部(header),躯干(body),尾部(footer)。其中躯干又水平分红三栏,从左到右为:导航、主栏、副栏。
HTML代码以下:
<body class="HolyGrail">
<header>...</header>
<div class="HolyGrail-body">
<main class="HolyGrail-content">...</main>
<nav class="HolyGrail-nav">...</nav>
<aside class="HolyGrail-ads">...</aside>
</div>
<footer>...</footer>
</body>
复制代码
CSS代码以下:
.HolyGrail {
display: flex;
min-height: 100vh;
flex-direction: column;
}
header,
footer {
flex: 1;
}
.HolyGrail-body {
display: flex;
flex: 1;
}
.HolyGrail-content {
flex: 1;
}
.HolyGrail-nav, .HolyGrail-ads {
/* 两个边栏的宽度设为12em */
flex: 0 0 12em;
}
.HolyGrail-nav {
/* 导航放到最左边 */
order: -1;
}
复制代码
若是是小屏幕,躯干的三栏自动变为垂直叠加。
@media (max-width: 768px) {
.HolyGrail-body {
flex-direction: column;
flex: 1;
}
.HolyGrail-nav,
.HolyGrail-ads,
.HolyGrail-content {
flex: auto;
}
}
复制代码
咱们经常须要在输入框的前方添加提示,后方添加按钮。
HTML代码以下:
<div class="InputAddOn">
<span class="InputAddOn-item">...</span>
<input class="InputAddOn-field">
<button class="InputAddOn-item">...</button>
</div>
复制代码
CSS代码以下:
.InputAddOn {
display: flex;
}
.InputAddOn-field {
flex: 1;
}
复制代码
有时,主栏的左侧或右侧,须要添加一个图片栏。
HTML代码以下:
<div class="Media">
<img class="Media-figure" src="" alt="">
<p class="Media-body">...</p>
</div>
复制代码
CSS代码以下:
.Media {
display: flex;
align-items: flex-start;
}
.Media-figure {
margin-right: 1em;
}
.Media-body {
flex: 1;
}
复制代码
有时,页面内容太少,没法占满一屏的高度,底栏就会抬高到页面的中间。这时能够采用Flex布局,让底栏老是出如今页面的底部。
HTML代码以下:
<body class="Site">
<header>...</header>
<main class="Site-content">...</main>
<footer>...</footer>
</body>
复制代码
CSS代码以下:
.Site {
display: flex;
min-height: 100vh;
flex-direction: column;
}
.Site-content {
flex: 1;
}
复制代码
每行的项目数固定,会自动分行。
CSS的写法:
.parent {
width: 200px;
height: 150px;
background-color: black;
display: flex;
flex-flow: row wrap;
align-content: flex-start;
}
.child {
box-sizing: border-box;
background-color: white;
flex: 0 0 25%;
height: 50px;
border: 1px solid red;
}
复制代码
(完)
小编推荐:程序员导航
做为一名码农,随着平时工做的须要,这里收集了国内外不少优秀网站,这其中包括在线工具、在线运行、免费接口、在线资源、在线学习、技术论坛、技术博客等等,知足通常程序员平常需求。
地址1:nav.imaring.com 地址1:www.code-elf.cn
做者:阮一峰 原文连接