flex
flex根本语法
参考MDN: [https://developer.mozilla.org…]
须要留神的点
flex-basis是对于主轴来说,主轴能够是x轴,也能够是y轴,为了不便行文,默认主轴为x轴
- flex-basis的优先级比width高
- item的宽度等于basic设置或者width设置的值加上残余空间被平分后本人占据那局部的值
比方
容器宽度等于700px
item1 flex-grow=2 flex-basic或者宽度=100px
item2 flex-grow=1 flex-basic或者宽度=200px
item3 flex-basic或者宽度=100px(flex-grow默认等于0)
item1最终宽度 =(700 - 100 - 200 - 100)/ ( 1 + 2) * 2 + 100 = 300
item1最终宽度 =(700 - 100 - 200 - 100)/ ( 1 + 2) * 1 + 200 = 300
item1最终宽度 = 100
- 设置flex属性,不论是用单值语法还是双值语法,会扭转flex-grow flex-shrink flex-basis 的默认属性
比方
flex: 1;
这个是单值语法:1代表的是flex-grow的值,其余两个值会被省略,被省略的时候,会有默认值
flex-grow: 省略时默认值为 1。 (本来默认值为 0)
flex-shrink: 省略时默认值为 1。 (本来默认值为 1)
flex-grow: 省略时默认值为 0。 (本来默认值为 auto)
所以flex: 1 等于 flex: 1 1 0,而不是felx: 1 1 auto
flex布局能够实现的几个成果
前置筹备工作
<div class="content">
<div class="item1">item1</div>
<div class="item2">item2</div>
<div class="item3">item3</div>
</div>
等分布局
.content {
display: flex;
width: 800px;
height: 100px;
}
.item1 {
background-color: #eee;
flex: 1;
}
.item2 {
background-color: #ccc;
flex: 1;
}
.item3 {
background-color: #bbb;
flex: 1;
}
item1比item2 item3宽度多200px
.content {
display: flex;
width: 800px;
height: 100px;
}
.item1 {
background-color: #eee;
flex: 1 200px;
}
.item2 {
background-color: #ccc;
flex: 1;
}
.item3 {
background-color: #bbb;
flex: 1;
}
item1的宽度是item2 item3的两倍
.content {
display: flex;
width: 800px;
height: 100px;
}
.item1 {
background-color: #eee;
flex: 2;
}
.item2 {
background-color: #ccc;
flex: 1;
}
.item3 {
background-color: #bbb;
flex: 1;
}
按倍数去调配容器的宽度
.content {
display: flex;
width: 600px;
height: 100px;
}
.item1 {
background-color: #eee;
flex: 1;
}
.item2 {
background-color: #ccc;
flex: 2;
}
.item3 {
background-color: #bbb;
flex: 3;
}
发表回复