这一章节我们来学习justify-content属性,本属性定义了项目在主轴上的对齐方式。结合上一节的布局例子进行理解,属性值分别为:
justify-content: flex-start | flex-end | center | space-between | space-around;
flex-start
:交叉轴的起点对齐
.box { background: blue; display: flex; justify-content: flex-start; }
实现效果:
flex-end
:右对齐
.box { background: blue; display: flex; justify-content: flex-end; }
实现效果:
center
: 居中
.box { background: blue; display: flex; justify-content: center; }
实现效果:
space-between
:两端对齐,项目之间的间隔都相等。
.box { background: blue; display: flex; justify-content: space-between; }
实现效果:
space-around
:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
.box { background: blue; display: flex; justify-content: space-around; }
实现效果:
自己按照上面的代码测试,感受下各个属性值实现的样式。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>justify-content</title> <style type="text/css"> .box { background: blue; display: flex; } .box div { width: 200px; height: 200px; } .box1 { background: red; } .box2 { background: orange; } .box3 { background: green; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> , </html>