Flex Layout is a powerful CSS layout model that helps us create responsive and flexible layouts more easily. This article will introduce the basic concepts, properties and sample codes of Flex layout.
What is flex layout?
Flex Layout is a container and item based layout model. A container is the parent element to which the flex layout is applied, while an item is the child element within the container. By setting the properties of the container, we can control the arrangement, alignment and space allocation of items within the container.
How to use flex layout?
To use flex layout, you first need to display
set the container's property to flex
or inline-flex
. This will make the container a flex container and enable flex layout.
.container {
display: flex;
}
Properties of flex containers
Here are some commonly used flex container properties:
flex-direction
: Specifies the arrangement direction of the items. The default value isrow
, indicating a horizontal arrangement. Other possible values arecolumn
(align vertically),row-reverse
(align horizontally reverse), andcolumn-reverse
(align vertically reverse).
.container {
flex-direction: row;
}
flex-wrap
: Specifies whether the item wraps. The default value isnowrap
, which means no newline. Other possible values arewrap
(newline) andwrap-reverse
(reverse newline).
.container {
flex-wrap: wrap;
}
justify-content
: Specifies the alignment of the item on the main axis. The default isflex-start
, which means align left. Other possible values areflex-end
(align right),center
(align center),space-between
(justify, with equal spacing between items), andspace-around
(equal spacing on both sides of each item).
.container {
justify-content: center;
}
align-items
: Specifies the alignment of the item on the cross axis. The default isstretch
, which means Stretch Fill. Other possible values areflex-start
(align top),flex-end
(align bottom), andcenter
(align center).
.container {
align-items: center;
}
align-content
: Specifies the alignment of multi-line items on the cross axis. The default isstretch
, which means Stretch Fill. Other possible values areflex-start
(justify top),flex-end
(justify bottom),center
(justify center),space-between
(justify with equal space between lines), andspace-around
(equal space on both sides of each line).
.container {
align-content: center;
}
Properties of Flex Items
Here are some commonly used flex item properties:
flex-grow
: Specifies the magnification ratio of the item. The default value is0
, meaning no zoom in. When set to a positive number, the item will be scaled up.
.item {
flex-grow: 1;
}
flex-shrink
: Specifies the reduction ratio of the item. The default value is1
, which means it can be zoomed out.0
When set to , the item will not be scaled down.
.item {
flex-shrink: 0;
}
flex-basis
: Specifies the initial size of the item. The default value isauto
, indicating that the size is automatically calculated based on the item content. Can be set to a specific value or a percentage.
.item {
flex-basis: 50%;
}
flex
: is the short form offlex-grow
,flex-shrink
andflex-basis
. The default value is0 1 auto
.
.item {
flex: 1 0 50%;
}
align-self
: Specifies the alignment of individual items on the cross axis, overriding the container'salign-items
properties.
.item {
align-self: flex-start;
}
sample code
Here is an example of a simple flex layout:
<div class="container">
<div class="item">项目1</div>
<div class="item">项目2</div>
<div class="item">项目3</div>
</div>
.container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
.item {
flex: 1 0 25%;
height: 100px;
background-color: #ccc;
margin: 10px;
}
In this example, the container is set to a flex layout, and the items are arranged horizontally and centered. Each item has a size of 25% of the container's width, a height of 100px, a gray background color, and a 10px gap between them.
in conclusion
Flex Layout is a powerful CSS layout model that helps us create responsive and flexible layouts more easily. By setting properties for containers and items, we can control how items are arranged, aligned, and spaced. Hope this article helps you understand and use Flex layout!