1/1/1970
Flexbox is a one-dimensional layout system used for laying out items in rows or columns.
display: flex; – Enables Flexbox on the container.flex-direction – Controls the main axis direction.
row (default) – Left to right.row-reverse – Right to left.column – Top to bottom.column-reverse – Bottom to top.justify-content – Aligns items on the main axis.
flex-start (default) – Items align at the start.flex-end – Items align at the end.center – Items align in the center.space-between – Space between items.space-around – Space around items.space-evenly – Equal spacing around items.align-items – Aligns items on the cross axis.
stretch (default) – Items stretch to fit.flex-start – Aligned at the start.flex-end – Aligned at the end.center – Aligned in the center.baseline – Aligned based on text baseline.align-content – Aligns rows on the cross axis (when wrapping).
align-items.flex-wrap – Controls wrapping of flex items.
nowrap (default) – Items stay on one line.wrap – Items wrap to the next line.wrap-reverse – Items wrap in reverse.gap – Controls spacing between items.order – Controls the visual order of items (default: 0).flex-grow – Controls how much an item grows relative to others (default: 0).flex-shrink – Controls how much an item shrinks (default: 1).flex-basis – Initial size of the item before growth/shrink (auto, %, px, etc.).flex – Shorthand for flex-grow, flex-shrink, and flex-basis.
flex: 1 0 200px;align-self – Overrides align-items for an individual item..container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
gap: 20px;
}
.item {
flex: 1;
}CSS Grid is a two-dimensional layout system for controlling both rows and columns.
display: grid; – Enables Grid layout.grid-template-columns – Defines column structure.
grid-template-columns: 1fr 2fr 1fr;grid-template-rows – Defines row structure.
grid-template-rows: 100px auto;grid-column-gap / grid-row-gap – Space between grid items (deprecated, use gap).gap – Shorthand for row-gap and column-gap.grid-auto-rows – Sets the size of automatically created rows.grid-auto-columns – Sets the size of automatically created columns.grid-auto-flow – Controls item placement.
row (default) – Places items row-wise.column – Places items column-wise.grid-column – Specifies column start/end position.
grid-column: 1 / 3;grid-row – Specifies row start/end position.
grid-row: 2 / 4;grid-area – Shorthand for grid-row-start, grid-column-start, grid-row-end, and grid-column-end.justify-self – Aligns item horizontally.align-self – Aligns item vertically.place-self – Shorthand for align-self and justify-self..container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.item {
grid-column: 1 / 3;
}| Feature | Flexbox | CSS Grid |
|---|---|---|
| Layout Type | One-dimensional (row/col) | Two-dimensional (row+col) |
| Content Flow | Controlled by items | Controlled by grid tracks |
| Best For | Simple, linear layouts | Complex, structured layouts |
| Alignment | justify-content, align-items |
place-items, place-content |
| Item Order | order property |
grid-row, grid-column |
| Responsiveness | flex-wrap and flex-basis |
auto-fit, auto-fill |
Use Flexbox for:
Use Grid for:
Centered Content (Flexbox)
.container {
display: flex;
justify-content: center;
align-items: center;
}Card Layout (CSS Grid)
.container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 20px;
}Holy Grail Layout (CSS Grid)
.container {
display: grid;
grid-template-columns: 200px 1fr 200px;
grid-template-rows: auto 1fr auto;
}
.header { grid-column: 1 / 4; }
.footer { grid-column: 1 / 4; }
.main { grid-column: 2; }