Column spanning lets a box stretch across multiple columns in a grid. It helps organize content clearly and nicely.
0
0
Column spanning in Tailwind
Introduction
You want a heading to stretch across all columns in a grid.
You need a wide image or button that covers more than one column.
You want to make some content stand out by making it wider than others.
You are building a layout with different sized boxes in a grid.
You want to control how many columns a box uses on different screen sizes.
Syntax
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}.
Examples
The first box stretches across 2 columns, the second box fills the last column.
Tailwind
<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>
The header spans all 4 columns, boxes below each take 1 column.
Tailwind
<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>
Two boxes each span 3 columns, splitting the grid in half.
Tailwind
<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>
Sample Program
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.
Tailwind
<!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>
OutputSuccess
Important Notes
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.
Summary
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.