display: flex; /* hoặc inline-flex */
}
Note: Common CSS columns are unusable in flex containers.
The flex-direction attribute determines the direction of the main-axis so that the container arranges items.
Syntax:
.container {
flex-direction: row | row-reverse | column | column-reverse;
}
Parameters:
For example:
.flex-container {
display: flex;
flex-direction: row;
}
.flex-container {
display: flex;
flex-direction: row-reverse;
}
.flex-container {
display: flex;
flex-direction: column;
}
.flex-container {
display: flex;
flex-direction: column-reverse;
}
By default, the item will automatically resize the element so that it always displays on the same line no matter which size you resize, which makes it easy to internal content (if any). Squeeze again, can cause bad interface.
So, we have the flex-wrap attribute that allows the item to automatically flow down when the container size changes.
Syntax:
.container{
flex-wrap: nowrap | wrap | wrap-reverse;
}
Parameters:
For example:
.flex-container {
display: flex;
flex-wrap: nowrap;
}
.flex-container {
display: flex;
flex-wrap: wrap;
}
.flex-container {
display: flex;
flex-wrap: wrap-reverse;
}
The flex-flow attribute is used to group two flex-direction and flex-wrap attributes.
Syntax:
flex-flow: <'flex-direction'> || <'flex-wrap'>
For example:
.flex-container {
display: flex;
flex-flow: row wrap;
}
By default, the internal items will start from main start to the main end, however, sometimes the container still has space. So, you can use the justify-content property to adjust the starting position and align the items inside the container along the main axis axis , horizontally or vertically depending on flex-direction.
Syntax:
.container {
justify-content: flex-start | flex-end | center | space-between |
space-around | space-evenly;
}
Parameters:
For example: Here the main axis is horizontal
.flex-container {
display: flex;
justify-content: flex-start;
}
.flex-container {
display: flex;
justify-content: flex-end;
}
.flex-container {
display: flex;
justify-content: center;
}
.flex-container {
display: flex;
justify-content: space-between;
}
.flex-container {
display: flex;
justify-content: space-evenly;
}
.flex-container {
display: flex;
justify-content: space-around;
}
The align-items attribute is used to adjust the starting position and align the items inside the container along the axis of the cross axis, horizontally or vertically depending on flex-direction.
Syntax:
.container {
align-items: stretch | flex-start | flex-end | center | baseline;
}
Parameters and examples illustrated
stretch: the default value, elements will be extended to fill its container, but the height / width value will be preferred if there is one, then the item will not be full high but only take the height / width value you set.
.flex-container {
display: flex;
align-items: stretch;
}
flex-start: item will start from the container's cross-start margin.
For example , if the default with the main axis is horizontal, the vertical axis is vertical, flex-direction: row then the items will start from above.
.flex-container {
display: flex;
align-items: flex-start;
}
flex-end: item will start from the cross-end margin of the container. If the default with the vertical axis is vertical, flex-direction: row then the items will roll down.
.flex-container {
display: flex;
align-items: flex-end;
}
center: item will center in the direction of the cross axis.
.flex-container {
display: flex;
align-items: center;
}
baseline: item is aligned according to their baseline.
The baseline is the path where all the letters will "sit up". You can use different font sizes to see that the items are aligned to the baseline baseline:
.flex-container {
display: flex;
align-items: baseline;
}
The align-content attribute is used to align the distance between items within the container along the axis of the horizontal axis, horizontally or vertically depending on flex-direction.
Syntax:
.container {
align-content: flex-start | flex-end | center | space-between |
space-around | stretch;
}
Parameters:
flex-start: item will start from the container's cross-start margin.
.flex-container {
display: flex;
flex-wrap: wrap;
align-content: flex-start;
}
flex-end: item will start from the cross-end margin of the container.
.flex-container {
display: flex;
flex-wrap: wrap;
align-content: flex-end;
}
center: item will be located between containers base on cross-axis.
.flex-container {
display: flex;
flex-wrap: wrap;
align-content: center;
}
space-between: items will have equal spacing between elements by the container automatically aligns the distance, the first item hides the cross-start, the last item hides the cross-end container.
.flex-container {
display: flex;
flex-wrap: wrap;
align-content: space-between;
}
space-around: similar to space-between, but the difference is that each item has 2 side spacing and these distances are equal.
.flex-container {
display: flex;
flex-wrap: wrap;
align-content: space-around;
}
stretch : default value, elements will be extended, aligned to fill its container (still prioritizing the height / width value if available).
.flex-container {
display: flex;
flex-wrap: wrap;
align-content: stretch;
}
By default, items will display in order that appears in HTML, but with the order attribute , you can rearrange the placement of items.
Syntax:
.item {
order: ; /* mặc định là 0 */
}
The flex-grow attribute allows elements to expand according to the width of the container.
Syntax:
.item {
flex-grow: ; /* mặc định là 0 */
}
This attribute is a bit complicated, Quantrimang will illustrate some of the more common situations for you to imagine. For example, we set the items to be 100px wide.
The flex-grow attribute's default value is 0 , items will not automatically scale, leaving lots of space in the container.
When we increase flex-grow = 1, the item will automatically expand evenly to fit into the container frame.
The flex-grow value is very flexible, when setting this attribute for all items with the same value , the items will have the same proportions and are evenly filled up the container. For example, if all of the flex-grow elements are 1, they will all be the same with the 1: 1 ratio, but the set of flex-grow is 100, then the end will still be the same with a 1: 1 ratio.
But the more interesting thing about the flex-grow is to apply it to each item. We have the default value of all elements, flex-grow = 0, changing the value of item2 to 1 separately, the results are as follows:
So here, setting the flex-grow to 1 then item2 will take the rest of the container into itself.
Now try for the elements to be flex-grow: 1 , but set the other 3-valued element separately:
At this point all items are expanded to fill the empty part of the container, but item3 with flex-grow: 3 will inherit more empty than the remaining items with only flex-grow: 1, more specifically about 3 times. And as mentioned above, the flex-grow attribute makes elements proportional to each other. Suppose the items have flex-grow: 4 and item3 has the flex-grow: 12 attribute, it is similar to 1 with 3.
The flex-shrink attribute is in contrast to the flex-grow attribute above, it does not expand but shrinks as we change the width of the container.
Syntax:
.item {
flex-shrink: ; /* mặc định là 1 */
}
The default value in flex-shrink is 1, allowing the particles to shrink even when the container width drops. If flex-shrink: 0, the item will not expand and take the value of the width / height attribute.
If you want item3 to shrink more than other items, increase its flex-shrink value.
Resize a small browser window, item3 shrinks more:
The flex-basis attribute is used to determine the initial length of an item.
Syntax:
.item {
flex-basis: | auto; /* mặc định là auto */
}
If you specify the overall length of the item item to be 100px, but customize item3 with flex-basis: 250px then we will get the following:
Used flex properties to pool three flex-grow, flex-shrink and flex-basis attributes .
Syntax:
.item {
flex: none | [ <'flex-grow'> <'flex-shrink'> || <'flex-basis'> ]
}
Instead of using all three attributes:
.item {
flex-grow: 1;
flex-shrink: 3;
flex-basis: 250px;
}
You can use the flex attribute briefly:
flex: 1 3 250px;
The default value of flex is:
flex: 0 1 auto;
Note :
The align-self attribute works similarly to align-items of the container section but is used separately for each item, you can use it to reposition some items that align-items specified.
.item {
align-self: auto | flex-start | flex-end | center | baseline | stretch;
}
Align-self also has the same values as align-items: flex-start, flex-end, center, stretch and baseline.
Example: We have 5 items that have been centered by align-items: center as follows:
You can align items as you like, item1 is at the top, item3 must be stretched and item5 must be at the bottom, use align-self to reposition positions.
Use flexbox to create a photo gallery that displays different layouts depending on the screen size: