Modern pages need layouts that adapt to screens of different sizes. CSS has two layout systems built for this: Flexbox for one direction and Grid for two.
Flexbox
Flexbox arranges items along one line, either a row or a column. An element becomes a flex container when its display is set to flex. Its children are then flex items. The property flex-direction chooses row or column. The property justify-content spreads items along the main direction with values such as flex-start, center, space-between and space-around. The property align-items places items across the main direction.
Definition: Flexbox. A one-dimensional layout model that arranges items in a row or in a column.
Flex Items
The property flex-wrap: wrap lets items move to a new line when there is not enough room. The property gap sets the space between items. The property flex: 1 on an item lets it grow and take an equal share of the free space.
Grid
Grid arranges items in rows and columns together. An element becomes a grid container with display: grid. The property grid-template-columns defines the columns, for example repeat(3, 1fr) for three equal columns. The unit fr stands for one share of the free space. The property gap sets the space between rows and columns.
Definition: Grid. A two-dimensional layout model that arranges items in rows and columns.
Choosing Between Them
Use Flexbox when items sit in a single row or column, such as a navigation bar or a row of cards. Use Grid when the layout needs rows and columns that line up, such as a photo gallery or a whole page with header, sidebar and content.
Table 2.7: Flexbox compared with Grid
| Feature | Flexbox | Grid |
|---|---|---|
| Directions | One at a time | Rows and columns together |
| Main property | display: flex | display: grid |
| Best for | Menus, rows of cards | Page layouts, galleries |
Example 2.7: A flex row and a grid
<style>
.box { background: #DEEAF6; border: 1px solid #1F3864;
padding: 8px; text-align: center; }
.row { display: flex; justify-content: space-between;
gap: 8px; margin-bottom: 12px; }
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
</style>
<div class="row">
<div class="box">A</div><div class="box">B</div><div class="box">C</div>
</div>
<div class="grid">
<div class="box">1</div><div class="box">2</div><div class="box">3</div>
<div class="box">4</div><div class="box">5</div><div class="box">6</div>
</div>
Output in the browser:
Responsive Layouts
Flexbox and Grid become responsive with a few extra rules. A flex container with flex-wrap: wrap lets cards drop onto a new line on a narrow screen. The declaration grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)) creates as many columns as fit, each at least 200 pixels wide. A media query such as @media (max-width: 600px) can switch a layout to a single column on phones.
Common mistakes
- Setting justify-content on an element that is not a flex container.
- Forgetting that justify-content and align-items swap directions when flex-direction is column.
- Using Flexbox for a layout that needs rows and columns together.
- Hard-coding pixel widths for columns, which defeats a responsive layout.
Key takeaways
- Flexbox lays items out in one direction, and Grid in rows and columns.
- display: flex and display: grid turn an element into a container.
- justify-content works along the main axis and align-items across it.
- The fr unit is a share of the free space in a grid.
Practice questions
- Which property turns an element into a flex container?
- What does repeat(3, 1fr) create?
- When is Grid a better choice than Flexbox?
Answers to the practice questions
- display set to flex.
- Three columns of equal width.
- When the layout needs both rows and columns to line up.
Sources
- Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit II
