flex 基本概念
采用Flex布局的元素,称为Flex容器(flex container),简称”容器”。它的所有子元素自动成为容器成员,称为Flex项目(flex item)
容器的属性
- flex-direction
- flex-wrap
- flex-flow
- justify-content
- align-items
- align-content
flex-direction属性
flex-direction属性决定主轴的方向(即项目的排列方向)。
.box {
flex-direction: row | row-reverse | column | column-reverse;
}
row(默认值):主轴为水平方向,起点在左端。
row-reverse:主轴为水平方向,起点在右端。
column:主轴为垂直方向,起点在上沿。
column-reverse:主轴为垂直方向,起点在下沿。
flex-wrap属性
默认情况下,项目都排在一条线(又称”轴线”)上。flex-wrap属性定义,如果一条轴线排不下,如何换行。
.box{
flex-wrap: nowrap | wrap | wrap-reverse;
}
(1)nowrap(默认):不换行。
(2)wrap:换行,第一行在上方。
(3)wrap-reverse:换行,第一行在下方。
flex-flow
flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。
.box {
flex-flow: <flex-direction> <flex-wrap>;
}
justify-content属性
.box {
justify-content: flex-start | flex-end | center | space-between | space-around;
}
flex-start(默认值):左对齐
flex-end:右对齐
center: 居中
space-between:两端对齐,项目之间的间隔都相等。
space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
align-items属性
.box {
align-items: flex-start | flex-end | center | baseline | stretch;
}
flex-start:交叉轴的起点对齐。
flex-end:交叉轴的终点对齐。
center:交叉轴的中点对齐。
baseline: 项目的第一行文字的基线对齐。
stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。
align-content属性
.box {
align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}
flex-start:与交叉轴的起点对齐。
flex-end:与交叉轴的终点对齐。
center:与交叉轴的中点对齐。
space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
stretch(默认值):轴线占满整个交叉轴。
flex 属性
order
flex-grow
flex-shrink
flex-basis
flex
align-self
- order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。
- flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。
- flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
- flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。
- flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。
- align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。
导航栏布局
function App(){
const inputStyle ={
outline:'none',
border:'none',
flexGrow:1
}
const headerStyle ={
display:'flex',
justifyContent:'space-between'
}
const leftContent = {
display:'flex',
alignItems:'center',
flexShrink:'0'
}
const leftContentItem={
marginLeft:'20px'
}
const searchContent={
display: 'flex',
alignItems: 'center',
maxWidth:'60%',
minWidth:'20%',
justifyContent: 'space-between'
}
const rightContent = {
display:'flex',
alignItems:'center'
}
const rightContentItem ={
marginLeft:'20px'
}
return <>
<div style={headerStyle}>
<div style={leftContent}>
<div style={leftContentItem}> 首页</div>
<div style={leftContentItem}> 博客</div>
<div style={leftContentItem}> 新闻</div>
<div style={leftContentItem}> 学习</div>
</div>
<div style={searchContent}>
<input style={inputStyle} />
</div>
<div style={rightContent}>
<div style={rightContentItem}>👤</div>
<div style={rightContentItem}>github</div>
<div style={rightContentItem}>☀️</div>
</div>
</div>
</>
}
布局 alignItem
function App(){
const content1 = {
display:'flex',
alignItems:'flex-center',
// justifyContent:'flex-end'
justifyContent:'space-between'
}
const item1 = {
flexShrink:'1',
flexBasis:'0px',
border:'1px solid red',
margin:' auto 2px'
}
return <>
<div style={content1}>
<div style={item1}>1</div>
<div style={item1}>2</div>
<div style={item1}>3</div>
<div style={item1}>4</div>
<div style={item1}>5</div>
<div style={item1}>6</div>
</div>
</>
}
布局 flexShrink
function App(){
const content1 = {
display:'flex'
}
const item1 = {
flexShrink:'1',
flexBasis:'0px',
border:'1px solid red',
margin:' auto 2px'
}
return <>
<div style={content1}>
<div style={item1}>1</div>
<div style={item1}>2</div>
<div style={item1}>3</div>
<div style={item1}>4</div>
<div style={item1}>5</div>
<div style={item1}>6</div>
</div>
</>
}
布局 flexBasis
function App(){
const content1 = {
display:'flex'
}
const item1 = {
flexBasis:'0px',
flexGrow:1,
border:'1px solid red',
margin:' auto 2px'
}
return <>
<div style={content1}>
<div style={item1}>1</div>
<div style={item1}>2</div>
<div style={item1}>3</div>
<div style={item1}>4</div>
<div style={item1}>5</div>
<div style={item1}>6</div>
</div>
</>
}
布局
function app(){
const bodyContent = {
width:'100%',
border:'1px solid red',
margin:'30px auto 0px'
}
const column1 ={
width: '70%',
height: '100px',
float:'left',
backgroundColor:'red'
}
const column2 = {
// border: '1px solid #000',
width:'28%',
backgroundColor: 'yellow',
float: 'right',
height:'100px'
}
return <>
<div style={bodyContent}>
<div>
<div style={column1}></div>
<div style={column2}></div>
<div style={{
clear:'both'
}}>
</div>
</div>
</div>
</>
}
瀑布流布局
function app (){
return <>
</>
}
flex 布局
function app(){
const constStyle ={
// flex:'1 1 150px',
backgroundColor:'red',
marginLeft:'20px'
}
return <>
<div style={{
display:'flex',
flexWrap:'warp',
justifyContent:'center'
}}>
<div style={constStyle}>
1
</div>
<div style={constStyle}>
2
</div>
<div style={constStyle}>
3
</div>
</div>
</>
}