Use Flexbox when you need to arrange items along one axis, such as links in a menu or buttons in a toolbar. Use CSS Grid when you want items in separate rows to share the same columns, such as cards in a product listing. You can use both on the same page: Grid for the cards, Flexbox for the controls inside each card.
Use Flexbox for a menu that can wrap
A menu often needs links to keep their natural widths and move onto a second line on a small screen. Add display: flex to the list, allow wrapping, and use gap to space the links. Flexbox lays out each wrapped line independently, so links on different lines don’t have to align into columns.
<nav aria-label="Topics">
<ul class="topic-nav">
<li><a href="#layout">Layout</a></li>
<li><a href="#examples">Examples</a></li>
<li><a href="#practice">Practice</a></li>
</ul>
</nav>
.topic-nav {
display: flex;
flex-wrap: wrap;
gap: 0.5rem 1rem;
list-style: none;
margin: 0;
padding: 0;
}
In a working page, give the three destination sections the IDs used in the example. If the design calls for a vertical sidebar instead, set flex-direction: column and check the spacing with longer labels.
Use Grid for a responsive card collection
Imagine a collection of article cards whose titles have different lengths. If the card edges should line up from one row to the next, define columns on the parent. Put each card in a direct child of .article-grid, then add this CSS:
.article-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
gap: 1rem;
}
.article-grid > article {
min-width: 0;
overflow-wrap: anywhere;
}
auto-fit makes as many tracks as fit and collapses empty ones. minmax() gives each occupied track a flexible width. The inner min() lets the preferred 16rem minimum shrink to the container width when space is tight. A lone card can stretch across the available width; if that isn’t the design you want, consider auto-fill, which keeps empty tracks.
When either layout seems to work
Resize the container instead of judging at one desktop width. Try an odd number of cards, a long link label, and a container narrower than 16rem. If wrapping is fine and each line can size itself, keep Flexbox. If items in different rows must share column edges, choose Grid. Keep the source order logical, then tab through the links to make sure the focus order matches how people read the page. Don’t use visual reordering to correct an out-of-order document.





