Skip to main content

CSS Grid and Flexbox Cheatsheet

CSS Grid

Properties for Grid Container

PropertyDescriptionExample
display: grid;Defines a grid containerdisplay: grid;
grid-template-rowsDefines the row structure of the gridgrid-template-rows: 100px auto 1fr;
grid-template-columnsDefines the column structure of the gridgrid-template-columns: repeat(3, 1fr);
grid-gap / gapDefines the space between grid rows and columnsgap: 20px;
grid-template-areasDefines grid layout using named areasRefer to grid-template-areas example
justify-itemsAligns grid items horizontally within their celljustify-items: center;
align-itemsAligns grid items vertically within their cellalign-items: stretch;
justify-contentAligns the entire grid horizontallyjustify-content: space-around;
align-contentAligns the entire grid verticallyalign-content: space-between;

Properties for Grid Items

PropertyDescriptionExample
grid-columnSpecifies the column span or placement of a grid itemgrid-column: 1 / 3;
grid-rowSpecifies the row span or placement of a grid itemgrid-row: 2 / span 3;
grid-areaPlaces an item in a named grid areagrid-area: header;
justify-selfAligns an item horizontally within its celljustify-self: end;
align-selfAligns an item vertically within its cellalign-self: center;

Grid Example

.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: 100px 200px;
grid-gap: 10px;
grid-template-areas:
"header header header"
"sidebar main main";
}

.header {
grid-area: header;
}

.sidebar {
grid-area: sidebar;
}

.main {
grid-area: main;
}

CSS Flexbox

Flexbox is a one-dimensional layout system for arranging items in rows or columns.

Flexbox Container Properties

PropertyDescriptionExample
display: flexDefines a container as a flexbox.display: flex;
flex-directionDefines the direction of the main axis (row or column).flex-direction: row-reverse;
justify-contentAligns items along the main axis (horizontal for row).justify-content: space-around;
align-itemsAligns items along the cross-axis (vertical for row).align-items: center;
align-contentAligns rows in a multi-line flex container.align-content: space-between;
flex-wrapSpecifies whether items should wrap onto multiple lines.flex-wrap: wrap;
gapDefines spacing between flex items.gap: 15px;

Flexbox Item Properties

PropertyDescriptionExample
orderSpecifies the order of the item in the container.order: 2;
flex-growDefines how much an item grows relative to others.flex-grow: 1;
flex-shrinkDefines how much an item shrinks relative to others.flex-shrink: 0;
flex-basisDefines the default size of an item before flexing.flex-basis: 200px;
flexShorthand for flex-grow, flex-shrink, and flex-basis.flex: 1 0 auto;
align-selfAligns an individual item on the cross-axis.align-self: flex-end;

Example: Flexbox Layout

.container {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
gap: 10px;
}

.item {
flex: 1;
order: 2;
}