Column spanning lets a box stretch across multiple columns in a grid. It helps organize content clearly and nicely.
Column spanning in Tailwind
col-span-{n}
// where n is the number of columns to spanUse col-span-{n} to make an element span n columns.
Make sure the parent container uses grid and defines columns with grid-cols-{number}.
<div class="grid grid-cols-3 gap-4"> <div class="col-span-2 bg-blue-300">Spans 2 columns</div> <div class="bg-green-300">Spans 1 column</div> </div>
<div class="grid grid-cols-4 gap-2"> <div class="col-span-4 bg-red-200">Full width header</div> <div class="bg-yellow-200">Box 1</div> <div class="bg-yellow-200">Box 2</div> <div class="bg-yellow-200">Box 3</div> <div class="bg-yellow-200">Box 4</div> </div>
<div class="grid grid-cols-6 gap-3"> <div class="col-span-3 bg-purple-300">Half width box</div> <div class="col-span-3 bg-purple-500">Other half</div> </div>
This example shows a grid with 4 columns. The header stretches across all columns. Box 2 spans 2 columns, and Box 4 spans 3 columns. Others fill 1 column each.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Tailwind Column Spanning Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <main class="max-w-xl mx-auto"> <h1 class="text-xl font-bold mb-4">Grid with Column Spanning</h1> <section class="grid grid-cols-4 gap-4"> <div class="col-span-4 bg-blue-400 text-white p-4 rounded">Header spanning all 4 columns</div> <div class="bg-blue-200 p-4 rounded">Box 1</div> <div class="col-span-2 bg-blue-300 p-4 rounded">Box 2 spans 2 columns</div> <div class="bg-blue-200 p-4 rounded">Box 3</div> <div class="col-span-3 bg-blue-500 text-white p-4 rounded">Box 4 spans 3 columns</div> </section> </main> </body> </html>
Column spanning only works inside a grid container.
Use gap-{size} classes to add space between grid items.
You can combine column spanning with responsive prefixes like md:col-span-2 to change span on different screen sizes.
Use col-span-{n} to make an element cover multiple columns in a grid.
Make sure the parent uses grid and defines columns with grid-cols-{number}.
Column spanning helps create flexible and clear layouts.
Practice
col-span-3 do in a grid layout?Solution
Step 1: Understand the role of
The classcol-span-{n}col-span-3tells the element to cover 3 columns in the grid layout.Step 2: Differentiate from other grid classes
It does not create columns or set width; it only controls how many columns the element spans.Final Answer:
Makes the element span across 3 columns in the grid -> Option CQuick Check:
col-span-3 = span 3 columns [OK]
- Confusing col-span with grid-cols which sets columns on container
- Thinking col-span creates columns inside the element
- Assuming col-span sets fixed width in rem or px
Solution
Step 1: Set up the grid container with 4 columns
The classgrid-cols-4inside agridcontainer creates 4 columns.Step 2: Make the item span 2 columns
The classcol-span-2on the item makes it cover 2 columns.Final Answer:
<div class='grid grid-cols-4'><div class='col-span-2'>Item</div></div> -> Option BQuick Check:
grid-cols-4 + col-span-2 = correct syntax [OK]
- Swapping order of grid and grid-cols classes
- Using incorrect class like span-col-2 instead of col-span-2
- Setting col-span larger than grid-cols
<div class='grid grid-cols-3 gap-4'> <div class='col-span-2 bg-blue-300'>A</div> <div class='bg-red-300'>B</div> <div class='bg-green-300'>C</div> </div>
Solution
Step 1: Analyze grid container and columns
The container has 3 columns with gaps. Item A spans 2 columns, so it fills columns 1 and 2 in the first row.Step 2: Position of items B and C
Item B fills the remaining 1 column in the first row (column 3). Item C moves to the next row occupying the first column.Final Answer:
Item A spans 2 columns in the first row, B occupies the third column in the first row, C starts a new row -> Option AQuick Check:
col-span-2 + grid-cols-3 layout = Item A spans 2 columns in the first row, B occupies the third column in the first row, C starts a new row [OK]
- Assuming all items fit in one row regardless of span
- Thinking col-span affects rows instead of columns
- Ignoring grid gaps affecting spacing
<div class='grid grid-cols-3'> <div class='col-span-4 bg-yellow-200'>Item 1</div> <div>Item 2</div> </div>
Solution
Step 1: Check grid container columns
The container has 3 columns defined bygrid-cols-3.Step 2: Validate col-span value
The item usescol-span-4, which tries to span 4 columns, exceeding the 3 columns available.Final Answer:
col-span-4 is invalid because the grid has only 3 columns -> Option AQuick Check:
col-span cannot exceed grid-cols [OK]
- Ignoring that col-span cannot be larger than grid-cols
- Thinking missing gap causes layout error
- Assuming grid class is missing when it is present
Solution
Step 1: Define grid with enough columns
Usinggrid-cols-6gives 6 columns, enough for spanning 4 columns on large screens.Step 2: Set default and large screen spans
col-span-2applies by default (small screens).lg:col-span-4overrides on large screens to span 4 columns.Final Answer:
grid grid-cols-6 col-span-2 lg:col-span-4 -> Option DQuick Check:
Responsive col-span with lg: prefix = grid grid-cols-6 col-span-2 lg:col-span-4 [OK]
- Using sm: prefix for large screen changes
- Setting col-span larger than grid-cols
- Not defining enough columns in grid-cols
Ready to prove it?
Pass the test and this pattern moves unknown
