Combining Grid and Flexbox
Complex pages rarely require choosing one layout tool for everything. Grid and Flexbox can solve different relationships in the same component.
✓ What you will learn in this topic
- Explain the key idea: Combine layout systems according to relationships, while preserving source order.
- Apply the concept: Grid distributes the cards; Flexbox aligns actions inside each card and can push them to the bottom.
- Avoid this common mistake:
Concept explanation
Use Grid to establish a page or galleryâs rows and columns. Use Flexbox within a card to align a title and action, or to distribute items along one axis. Nested layouts are normal; choose the simplest model for each container.
Check wrapping, minimum sizes, and source order. Avoid using order or dense automatic placement to produce a visual arrangement that conflicts with how people navigate the HTML.
Syntax
A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.
.demo {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 1rem;
}
.demo .card {
display: flex;
flex-direction: column;
}Example 1: a small page element
Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.
<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>.demo {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 1rem;
}
.demo .card {
display: flex;
flex-direction: column;
}Expected result: Grid distributes the cards; Flexbox aligns actions inside each card and can push them to the bottom.
Example 2: a practical interface card
The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.
<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>.dashboard-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 18rem), 1fr));
gap: 1.25rem;
}
.card__actions {
display: flex;
justify-content: flex-end;
gap: 0.75rem;
margin-block-start: auto;
}Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.
Common mistake
Using visual reordering to create a reading order that contradicts the HTML source.
Best practice
Keep source order meaningful and use Grid for macro structure with Flexbox for local one-axis alignment.
Practice exercise
Build a dashboard shell with named grid areas and a responsive card section; use Flexbox inside each card for actions. Apply it to the example above and inspect the result at a narrow and a wide viewport.
Section summary
- Combine layout systems according to relationships, while preserving source order.
- Keep source order meaningful and use Grid for macro structure with Flexbox for local one-axis alignment.
Knowledge check
Which layout model is most directly suited to rows and columns together?
Reveal the explanation
Grid handles two-dimensional structure; Flexbox is often simpler for one-axis alignment inside components.
Finish the module lessons, then take the server-scored module quiz to check your understanding.
Practical example
css.demo {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 1rem;
}
.demo .card {
display: flex;
flex-direction: column;
}
Expected result:
Grid distributes the cards; Flexbox aligns actions inside each card and can push them to the bottom.
Hands-on practice task
Build a dashboard shell with named grid areas and a responsive card section; use Flexbox inside each card for actions.
In this section
Other lessons in Advanced Grid and Flexbox Layouts
Broaden your knowledge
Related learning topics
HTML
Learn to structure accessible, meaningful web pages with a complete HTML course from first document to professional application patterns.
JavaScript
Build strong JavaScript foundations with browser examples, focused practice, beginner projects, and module quizzes.