⇧
vadnica-logo
Editor My Blog Password Manager Finance My Services About the Project Telegram O meni O meni

CSS Layout (Flexbox and Grid)

CSS Layout is the foundation of every website. In the past, we used tables or float for layouts, but today we have two powerful and modern technologies: Flexbox and Grid.

Flexbox (Flexible Box Layout) is ideal for one-dimensional layouts – either in a row (horizontal) or a column (vertical). It allows easy alignment of elements, distribution of space between them, and resizing. Perfect for navigation bars, cards, button groups, and smaller components.

Grid (CSS Grid Layout) is a more powerful tool for two-dimensional layouts – managing both rows and columns simultaneously. It enables the creation of complex, complete page structures (header, sidebar, main, footer) with precise control over every part of the grid. Grid is excellent for the overall structure of a website.

In this tutorial, we will cover both technologies, their properties, and how to use them in practice. By the end, you will be able to create any layout that is responsive and adaptable to all devices.

EXAMPLE
RESULT
Property Example Description
Flexbox: Container
display: flex;display: flex; Activates Flexbox for the container
flex-directionflex-direction: row; Direction of layout (row, row-reverse, column, column-reverse)
justify-contentjustify-content: center; Alignment along the main axis (left, right, center, space-between)
align-itemsalign-items: center; Alignment along the cross axis (top, bottom, center, stretch)
flex-wrapflex-wrap: wrap; Whether elements should wrap to a new line (nowrap, wrap, wrap-reverse)
gapgap: 20px; Space between elements
Flexbox: Individual Items
flexflex: 1; Shorthand for grow, shrink, basis
flex-growflex-grow: 1; How much the item should grow relative to remaining space
flex-shrinkflex-shrink: 1; How much the item should shrink if there is not enough space
align-selfalign-self: flex-end; Overrides align-items for a specific item
Grid: Container
display: grid;display: grid; Activates Grid for the container
grid-template-columnsgrid-template-columns: 1fr 1fr 1fr; Defines the number and size of columns (1fr = one fraction)
grid-template-rowsgrid-template-rows: 100px auto; Defines the number and size of rows
grid-template-areasgrid-template-areas: "header header" "sidebar main"; Naming areas for easier layout
gapgap: 20px; Space between rows and columns
justify-itemsjustify-items: center; Alignment of content within cells (all cells)
align-itemsalign-items: stretch; Alignment of content within cells (all cells)
justify-contentjustify-content: space-around; Alignment of the entire grid within the container
Grid: Individual Items
grid-columngrid-column: 1 / 3; Places the item from column 1 to 3
grid-rowgrid-row: 1 / span 2; Places the item from row 1, spanning 2 rows
grid-areagrid-area: header; Places the item in a named area
justify-selfjustify-self: end; Overrides justify-items for a specific item