CSS中的弹性盒子常用于布局,而在弹性盒子中,我们可以规定子元素的排序方式。下面介绍几种常用的排序方法。1. flexdirection属性flexdirection属性决定了弹性盒子是水平排列还是垂...
CSS中的弹性盒子常用于布局,而在弹性盒子中,我们可以规定子元素的排序方式。下面介绍几种常用的排序方法。
1. flex-direction属性
flex-direction属性决定了弹性盒子是水平排列还是垂直排列,同时也决定了子元素的排列方式。
当flex-direction属性的值为row时,子元素默认从左到右水平排列;当值为column时,子元素默认从上到下垂直排列;当值为row-reverse时,子元素从右到左水平排列;当值为column-reverse时,子元素从下到上垂直排列。
代码示意:
pre{
display:flex;
flex-direction: row;//水平排列
flex-direction: column;//垂直排列
flex-direction: row-reverse;//从右到左水平排列
flex-direction: column-reverse;//从下到上垂直排列
}
2. order属性
order属性规定了子元素的显示顺序,数值越小的子元素排在前面。
代码示意:
pre{
display:flex;
flex-direction: row;
}
p:first-child{
order: 2;//第一个p在第二个p后面
}
p:last-child{
order: 1;//最后一个p在第一个p前面
}
3. flex属性
flex属性是flex-grow、flex-shrink和flex-basis三个属性的缩写,分别用于控制子元素的扩展、收缩和基础大小。
代码示意:
pre{
display:flex;
flex-direction: row;
}
p:first-child{
flex:1;//占据剩余空间的1/2
}
p:last-child{
flex:2;//占据剩余空间的2/3
}
以上是CSS中常用的弹性盒子排序方法,灵活运用这些方法可以轻松实现各种布局效果。