Trước đây khi chúng ta thiết kế web đặc biệt là dàn trang layout, menu, chia các cột cho các thành phần trong web thì hầu hết đều sử dụng các thuộc tính như float kèm theo đó là clear float để chia cột . Khó khăn là khi responsive và hiển thị trên nhiều thiết bị phải chỉnh sửa code khá nhiều nên rất tốn thời gian.
Hoặc nhanh hơn thì các bạn sử dụng CSS Framework như bootstrap chẳng hạn, nhưng như thế thì website của bạn lại có nhiều đoạn code ‘dư thừa’ bạn không sử dụng gây ảnh hưởng đến tốc độ website…
Thế là Flexbox được sinh ra để cải thiện tình hình này, giúp cho việc dàn trang, canh các thành phần với nhau linh hoạt, dễ dàng và tiết kiệm thời gian hơn rất nhiều.
Hôm nay mình viết bài này để chúng ta cùng tìm hiểu về sức mạnh của CSS Flexbox để xem cách sử dụng nó như thế nào, nó có những thuộc tính hay gì mà nhiều người lại sử dụng nó thay cho float hay inline thế nhỉ ?
# Thuộc tính Display: Flex
Để sử dụng flex trong css thì đơn giản là chúng ta chỉ cần sử dụng thuộc tính display: flex. Mình có layout như thế này để các bạn dễ hình dung nha.
Và đây là kết quả
The flex CSS shorthand property sets how a flex item will grow or shrink to fit the space available in its flex container.
flex: auto;
flex: initial;
flex: none; flex: 2; flex: 10em;
flex: 30%;
flex: min-content; flex: 1 30px; flex: 2 2; flex: 2 2 10%; flex: inherit;
flex: initial;
flex: revert;
flex: revert-layer;
flex: unset;
The flex property may be specified using one, two, or three values.
initial
The item is sized according to its width and height properties. It shrinks to its minimum size to fit the container, but does not grow to absorb any extra free space in the flex container. This is equivalent to setting "flex: 0 1 auto".
autoThe item is sized according to its width and height properties, but grows to absorb any extra free space in the flex container, and shrinks to its minimum size to fit the container. This is equivalent to setting "flex: 1 1 auto".
The item is sized according to its width and height properties. It is fully inflexible: it neither shrinks nor grows in relation to the flex container. This is equivalent to setting "flex: 0 0 auto".
Defines the flex-grow of the flex item. Negative values are considered invalid. Defaults to 1 when omitted. [initial is 0]
Defines the flex-shrink of the flex item. Negative values are considered invalid. Defaults to 1 when omitted. [initial is 1]
Defines the flex-basis of the flex item. A preferred size of 0 must have a unit to avoid being interpreted as a flexibility. Defaults to 0 when omitted. [initial is auto]
For most purposes, authors should set flex to one of the following values: auto, initial, none, or a positive unitless number. To see the effect of these values, try resizing the flex containers below:
By default flex items don't shrink below their minimum content size. To change this, set the item's min-width or min-height.
none | [ ? || ]
Flex box [click to toggle raw box] Raw box
#flex-container { display: flex; flex-direction: row; } #flex-container > .flex-item { flex: auto; } #flex-container > .raw-item { width: 5rem; }
CSS Flexible Box Layout Module Level 1 # flex-property |
BCD tables only load in the browser