Flexbox: How to Display 3 Items Per Row
In this article
To display exactly 3 items per row in flexbox, set the container to display: flex with flex-wrap: wrap, then give each item a width of about calc(33.333% - gap) (or flex: 0 0 33.333% with box-sizing: border-box). The gap between items is handled by gap on the container or by margins on the items. This gives you a predictable three-column grid that wraps to a new row every fourth item, without media queries for the basic layout.
This is useful for product grids, image galleries, card layouts and any list where you want a fixed number of columns. The technique works in all modern browsers, including Chrome, Edge, Firefox and Safari. You only need a text editor and a browser to test. No framework or build step is required.
What You'll Need
- A basic HTML file with a container element and at least four child items (so you can see the wrap happen).
- A CSS file or a
<style>block in the HTML. - A modern browser to preview the result. Any current version of Chrome, Edge, Firefox or Safari is fine.
- Optional: browser developer tools (F12 or right-click > Inspect) to check computed widths and gaps.
Step-by-Step
- Create the container and items. In your HTML, add a container element (for example a
<div class="grid">) and place four or more child elements inside it. Each child should have the same class, such ascard. Four items is the minimum to confirm that the fourth item wraps to a second row. - Make the container a flex container. In CSS, target the container and set
display: flex;andflex-wrap: wrap;. Withoutflex-wrap: wrap, all items stay on one line and shrink to fit, so the wrap property is essential for a multi-row layout. - Set the width of each item. Give each child a width of one third of the container. The simplest reliable method is
flex: 0 0 calc(33.333% - 10px);if you plan to use a 15px gap, or useflex: 0 0 33.333%;together withbox-sizing: border-box;and a gap on the container. Theflexshorthand setsflex-grow: 0,flex-shrink: 0andflex-basisto the width you specify. Usingcalc()subtracts the gap so three items plus two gaps fit exactly in one row. - Add spacing between items. On the container, add
gap: 15px;(or any value you like). Thegapproperty in flexbox creates space between items without adding margin to the outer edges. If you usegap, remember to subtract the gap from the item width as shown in step 3. Alternatively, use margins on the items and negative margins on the container, butgapis simpler and better supported. - Prevent items from growing or shrinking unexpectedly. The
flex: 0 0 ...shorthand already setsflex-growandflex-shrinkto 0, which keeps each item at exactly one third. If you use onlywidth: 33.333%withoutflex, items may still shrink if content is too wide. Settingmin-width: 0;on the items can help if you have long unbreakable text, but it is not required for the basic layout. - Test with four or more items. Open the page in your browser. You should see three items in the first row and the fourth item starting the second row. If all items appear on one line, check that
flex-wrap: wrap;is present and that the item widths plus gaps do not exceed 100% of the container. If items are stacked vertically, check thatdisplay: flex;is applied to the correct parent element. - Adjust for responsiveness (optional). To show fewer columns on narrow screens, add a media query that changes the item width. For example, at a max-width of 600px, set
flex: 0 0 calc(50% - 10px);for two columns, orflex: 0 0 100%;for one column. This keeps the three-column layout on desktop and adapts on mobile. The exact breakpoint depends on your content; test at common widths like 320px, 768px and 1024px.
Common Mistakes
- Forgetting
flex-wrap: wrap. Without it, flex items stay on a single line and shrink. The container must allow wrapping for multiple rows to appear. - Using percentage widths that add up to more than 100% when gaps are included. If you set
width: 33.333%and also addgap: 15px, three items plus two gaps exceed the container width, so the third item wraps early. Usecalc()to subtract the gap, or usebox-sizing: border-boxand account for gaps with margins. - Applying flex properties to the wrong element.
display: flexgoes on the parent container, not on the items. The item width and flex properties go on the children. - Relying on
justify-content: space-betweenwith fixed widths. This can create uneven spacing when the last row has fewer than three items. Usinggapkeeps spacing consistent across rows. - Ignoring
box-sizing. If items have padding or borders andbox-sizing: content-box(the default), the actual width becomes larger than the percentage, breaking the three-per-row layout. Setbox-sizing: border-boxon the items or globally.
Tips
- Use
gapinstead of margins for spacing. It is cleaner and does not add space to the outer edges of the container. - If you want the items to grow to fill the row when there are fewer than three, use
flex: 1 1 calc(33.333% - 10px);instead offlex: 0 0 .... This allows items to expand, but be aware that the last row may have wider items. - For a simple three-column grid, CSS Grid with
grid-template-columns: repeat(3, 1fr);is often easier, but flexbox is fine when you need content-based sizing or want to support older browsers. - Test with real content, not just colored boxes. Long words or images can affect layout; use
overflow-wrap: break-word;on items if needed. - Keep the gap value in a CSS custom property (variable) so you can change it in one place and update the
calc()calculations consistently.
Troubleshooting
If the layout does not show three items per row, check these common issues in order:
- All items on one line: Ensure
flex-wrap: wrap;is set on the container. Also check that the combined width of three items plus gaps does not exceed 100%. If you usedcalc(33.333% - 10px)with a 15px gap, the math is wrong; usecalc(33.333% - 10px)only if the gap is 15px (since two gaps of 15px total 30px, and 33.333% of the container minus 10px per item accounts for that). The correct formula iscalc(33.333% - (2 * gap / 3)), but a simpler approach is to useflex: 0 0 calc(33.333% - 10px)withgap: 15pxbecause 2 gaps × 15px = 30px, and 3 items × 10px = 30px, so the total width is exactly 100%. If your gap is different, adjust the subtracted value accordingly. - Items stacked vertically: Confirm that
display: flex;is on the parent and not on the items. Also check for a typo in the property name. - Items too narrow or too wide: Inspect the computed width in developer tools. If
box-sizingis notborder-box, padding and borders add to the width. Setbox-sizing: border-box;on the items or use* { box-sizing: border-box; }at the top of your CSS. - Last row items stretch to fill the row: This happens if you used
flex-grow: 1. To keep items at one third width, useflex-grow: 0(as inflex: 0 0 ...). - Gap not working: The
gapproperty for flexbox is supported in all modern browsers, but if you need to support very old browsers, use margins instead. Check that you have not setgapon the items instead of the container.
If none of these solve the problem, simplify: remove all extra CSS, keep only display: flex; flex-wrap: wrap; on the container and flex: 0 0 33.333%; on the items. Then add gap and calc back step by step.
Frequently Asked Questions
Can I use width: 33.333% instead of flex-basis?
Yes, but you must also set flex-shrink: 0 and flex-grow: 0 to prevent items from resizing. Using the flex shorthand is more reliable because it sets all three properties at once.
Why do my items wrap to a new row before three are shown?
The most common cause is that the combined width of three items plus the gaps exceeds 100% of the container. Use calc() to subtract the gap from each item's width, or reduce the gap.
How do I make the last row items align left instead of stretching?
Set flex-grow: 0 on the items (for example, flex: 0 0 calc(33.333% - 10px);). This keeps each item at one third width, so the last row will have empty space on the right, which is usually the desired behavior.
Does this work with images or cards of different heights?
Yes, flexbox will align items in a row to the tallest item by default. If you want items to stretch to equal height, that is the default behavior. If you want them to keep their natural height, set align-items: flex-start; on the container.
Can I use gap with flexbox in all browsers?
gap for flexbox is supported in Chrome 84+, Firefox 63+, Safari 14.1+ and Edge 84+. For older browsers, use margins on the items and negative margins on the container, or use CSS Grid instead.
What is the difference between flex: 0 0 33.333% and flex: 1 1 33.333%?
flex: 0 0 33.333% means the item will not grow or shrink; it stays at one third. flex: 1 1 33.333% allows the item to grow to fill available space, which can make items wider than one third if there is extra space in the row.