Home >Web Front-end >CSS Tutorial >CSS Flexbox and Grid: The Art of Building Responsive Layouts
Turn on Flex layout mode. Set an element as a Flex container, and its direct child elements will become Flex items.
.container { display: flex; }
Defines the main axis direction (the direction of item arrangement). Optional values:
.container { flex-direction: row | row-reverse | column | column-reverse; }
Controls whether to wrap when there is not enough space in a row. Optional values:
.container { flex-wrap: nowrap | wrap | wrap-reverse; }
Defines the alignment on the main axis. Optional values:
.container { justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly; }
Defines the alignment on the cross axis. Optional values:
.container { align-items: stretch | flex-start | flex-end | center | baseline; }
Only works in multi-line Flex layout (flex-wrap: wrap), defines the alignment of multi-line items on the cross axis. Optional values:
.container { display: flex; }
Defines the order of items. The smaller the value, the higher the order. The default value is 0.
.container { flex-direction: row | row-reverse | column | column-reverse; }
Defines the enlargement ratio of the item. The default value is 0, which means no enlargement. If all items are set to non-zero values, the remaining space is distributed proportionally.
.container { flex-wrap: nowrap | wrap | wrap-reverse; }
Defines the shrinkage ratio of the item. Defaults to 1, meaning it can shrink. If all items are set to non-zero values, they shrink proportionally to prevent overflowing the container.
.container { justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly; }
Defines the initial size of the item before the remaining space is distributed. Accepts length, percentage, auto (default), or content values.
.container { align-items: stretch | flex-start | flex-end | center | baseline; }
Shorthand for flex-grow, flex-shrink, and flex-basis. Defaults to 0 1 auto.
.container { align-content: stretch | flex-start | flex-end | center | space-between | space-around; }
Overrides the container's align-items property to define the alignment of a single item on the cross axis. The optional values are the same as align-items.
.item { order: <integer>; }
Turn on Grid layout mode. Set an element as a Grid container, and its direct children will become Grid items (cells).
.item { flex-grow: <number>; /* Default is 0 */ }
Define the size of the grid's column and row tracks. Accepts length, percentage, fr (fraction unit, representing the fraction of the grid space) or auto values. You can also use the repeat() function to create repeated tracks, and the minmax() function to define the minimum and maximum size of the track.
.item { flex-shrink: <number>; /* defaults to 1 */ }
Define the area of the grid layout by naming the item and describing the grid structure with a string. The item name uses . to represent a blank cell.
.item { flex-basis: <length> | <percentage> | auto | content; }
Set the gap between items in the grid. Accepts length or percentage value.
.container { display: flex; }
Define the track size of newly added rows or columns when automatically filling the grid. Takes effect when the item exceeds the defined grid range.
.container { flex-direction: row | row-reverse | column | column-reverse; }
Controls how grid items are automatically filled and arranged. Optional values:
.container { flex-wrap: nowrap | wrap | wrap-reverse; }
Manually specify the start and end positions of items in the grid.
.container { justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly; }
Shorthand property for setting grid-row-start, grid-column-start, grid-row-end, and grid-column-end at the same time, or referencing the area name defined in grid-template-areas.
.container { align-items: stretch | flex-start | flex-end | center | baseline; }
In some cases, we can combine the advantages of CSS Grid and Flexbox to create more complex responsive layouts.
.container { align-content: stretch | flex-start | flex-end | center | space-between | space-around; }
First, CSS Grid is used to create a grid layout with adaptive column width. Each grid item (child element) uses Flexbox inside to vertically center the content. When the screen width is less than 768px, the media query switches to a single column layout to adapt to mobile devices.
The choice of using Flexbox or Grid usually depends on specific needs:
The above is the detailed content of CSS Flexbox and Grid: The Art of Building Responsive Layouts. For more information, please follow other related articles on the PHP Chinese website!