Flex Layout

Introduction

Flex is a one-dimensional layout model, which provide powerful capabilities for content justifying and alignment. There are two axes in flex: main axis and cross axis. It’s called one-dimensional because flex always deals with layout in one dimension. Flex layout provides flexible controls on layouts, which is completely out of box compared to traditional box model.

Core Concept

The two axes of flex

Main axis and cross axis are the core concepts in flex. The main axis is the direction that content should extend, which is defined by flex-direction property. Then the cross axis is exactly perpendicular to the main axis.

Flex container and flex items

Flex container is the element using the flex layout, which is defined with display: flex or display: inline-flex. As we set flex container, its direct children become flex items. When setting flex container and flex items, they will behave as the following:

  • Items display in a row (the flex-direction property is row by default).
  • The items start from the start edge of the main axis.
  • The items do not stretch on the main axis, but can shrink.
  • The items will stretch to fill the size of the cross axis.
  • The items’ flex-basis property is set to auto.
  • The container’s flex-wrap is set to nowrap. If the items overflow the container, the overflowed content will be hidden. Otherwise, more items could be fit into the container.

When flex-wrap is set to wrap, flex items will be wrapped into multiple lines when they are overflowed. Each line should be considered to be a flex container. Space distribution works the same way as in one line for each container.

flex-direction and flex-wrap

flex-direction controls the direction of flex items on the main axis. flex-wrap controls whether flex items are wrapped into multiple lines when they overflow.

flex-basis, flex-grow and flex-shrink

These three properties are to determined flex container’s distribution of available space for flex items. flex-basis controls the width of the flex item, which it’s set to auto by default. flex-grow causes the flex item grow along the main axis if set to a positive integer. flex-grow will not make the flex item’s width greater than max-width if it’s set. flex-shrink works opposite to flex-grow, in the way that when flex container doesn’t have enough space for flex items, items with flex-shrink set to a positive integer will shrink its width to fit the container. But the width of the flex item will not smaller than min-content and min-width if the min-width is set. flex property is a shorthand for flex-grow flex-shrink flex-basis, which default value is initial, equal to 0 1 auto.

justify-content and align-items

justify-content is used to distribute the space between flex items on main axis. align-items will align flex items on the cross axis.

References

Flexbox guide on MDN
Flexbox guide on css-tricks

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值