基本的布局方法——Flex布局

我要冲啦个人网站建设2020-12-08web前端

要求内容高度不确定下保持垂直居中:

这种情况下,我们更建议用flex布局。

在开始介绍flex之前,为了表述方便,我们约定以下术语:采用flex布局的元素,简称为“容器”,在代码示例中以container表示容器的类名。容器内的元素简称为“项目”,在代码示例中以item表示项目的类名。

基本概念

flex的概念最早是在2009年被提出,目的是提供一种更灵活的布局模型,使容器能通过改变里面项目的高宽、顺序,来对可用空间实现最佳的填充,方便适配不同大小的内容区域。

在不固定高度信息的例子中,我们只需要在容器中设置以下两个属性即可实现内容不确定下的垂直居中。

.container{

  display: flex;

  flex-direction: column;

  justify-content: center;

}

flex不单是一个属性,它包含了一套新的属性集。属性集包括用于设置容器,和用于设置项目两部分。

设置容器的属性有:

display:flex;

flex-direction:row(默认值) | row-reverse | column |column-reverse

flex-wrap:nowrap(默认值) | wrap | wrap-reverse

justify-content:flex-start(默认值) | flex-end | center |space-between | space-around | space-evenly

align-items:stretch(默认值) | center  | flex-end | baseline | flex-start

align-content:stretch(默认值) | flex-start | center |flex-end | space-between | space-around | space-evenly

设置项目的属性有:

order:0(默认值) | <integer>

flex-shrink:1(默认值) | <number>

flex-grow:0(默认值) | <number>

flex-basis:auto(默认值) | <length>

flex:none | auto | @flex-grow @flex-shrink @flex-basis

align-self:auto(默认值) | flex-start | flex-end |center | baseline| stretch
文章关键词
基本的布局方法
Flex布局
flexbox布局