Tailwind CSS Columns
Columns allow you to split the content of an element into multiple columns, similar to how text is formatted in newspapers or magazines. This is particularly useful when designing layouts that need to display content in an organized and readable fashion.
Tailwind CSS provides a comprehensive set of utilities to control and customize column properties. With Tailwind's utilities, you can quickly manage column count, width, gaps, and responsive behaviors. Let's delve into the specifics.
| Class | Properties | Example |
|---|---|---|
columns-1 | columns: 1; | <div className="columns-1"></div> |
columns-2 | columns: 2; | <div className="columns-2"></div> |
columns-3 | columns: 3; | <div className="columns-3"></div> |
columns-4 | columns: 4; | <div className="columns-4"></div> |
columns-5 | columns: 5; | <div className="columns-5"></div> |
columns-6 | columns: 6; | <div className="columns-6"></div> |
columns-7 | columns: 7; | <div className="columns-7"></div> |
columns-8 | columns: 8; | <div className="columns-8"></div> |
columns-9 | columns: 9; | <div className="columns-9"></div> |
columns-10 | columns: 10; | <div className="columns-10"></div> |
columns-11 | columns: 11; | <div className="columns-11"></div> |
columns-12 | columns: 12; | <div className="columns-12"></div> |
columns-auto | columns: auto; | <div className="columns-auto"></div> |
columns-3xs | columns: 16rem; | <div className="columns-3xs"></div> |
columns-2xs | columns: 18rem; | <div className="columns-2xs"></div> |
columns-xs | columns: 20rem; | <div className="columns-xs"></div> |
columns-sm | columns: 24rem; | <div className="columns-sm"></div> |
columns-md | columns: 28rem; | <div className="columns-md"></div> |
columns-lg | columns: 32rem; | <div className="columns-lg"></div> |
columns-xl | columns: 36rem; | <div className="columns-xl"></div> |
columns-2xl | columns: 42rem; | <div className="columns-2xl"></div> |
columns-3xl | columns: 48rem; | <div className="columns-3xl"></div> |
columns-4xl | columns: 56rem; | <div className="columns-4xl"></div> |
columns-5xl | columns: 64rem; | <div className="columns-5xl"></div> |
columns-6xl | columns: 72rem; | <div className="columns-6xl"></div> |
columns-7xl | columns: 80rem; | <div className="columns-7xl"></div> |
Overview of Columns
Columns based on Count
To specify the number of columns based on count- use utilities like columns-3, columns-4, etc. Each column's width will be automatically calculated based on the total columns.
export default function App() { return <h1>Hello world</h1> }
Columns based on Width
To specify each column's width instead of number, use utilities like- columns-xs, columns-sm, columns-md, etc. The total column number will be automatically calculated based on the width and available space.
The below example has two columns of 16rem each.
export default function App() { return <h1>Hello world</h1> }
Adding the Column Gap
To define the gap between each column, use the gap-* utilities, e.g., gap-2, gap-4, gap-8, etc.
export default function App() { return <h1>Hello world</h1> }
States and Responsiveness
Column behaviors can be adjusted conditionally for states such as hover or focus, as well as across breakpoints for responsiveness. These utilities ensure your layouts remain adaptable across various devices and states.
Hover and Focus States
You can modify columns when an element's state changes (e.g., upon hover or focus). Tailwind offers hover and focus modifiers to adjust column count, width, or gap conditionally.
export default function App() { return <h1>Hello world</h1> }
Breakpoint Modifiers
Responsive designs demand layouts that adapt fluidly to screen size. Tailwind's responsive modifiers- sm, md, lg, etc. ensure column utilities apply only on certain breakpoints.
export default function App() { return <h1>Hello world</h1> }
Custom Columns
When predefined utilities are not enough, you can use custom values to define the number of width of the columns. This includes extending the theme or using arbitrary values.
Extending the Theme
Extending Tailwind's default configuration allows you to add custom column utilities globally (e.g., unique column counts or gaps). Open your project's tailwind.config.js file and extend the theme object to define new utilities. Once done, you can apply these utilities in your components.
export default function App() { return <h1>Hello world</h1> }
Using Arbitrary Values
For one-off situations where extending the theme is too much effort, you can apply arbitrary values directly in your component.
export default function App() { return <h1>Hello world</h1> }
Real World Examples
Product Feature Grid with Alternating Columns
A responsive layout showcasing product features with icons and descriptions that switches between 2-4 columns based on screen size.
export default function App() { return <h1>Hello world</h1> }
Blog Article Masonry Layout
A masonry-style blog layout with varying content lengths organized in responsive columns.
export default function App() { return <h1>Hello world</h1> }
Portfolio Gallery Grid
A responsive portfolio gallery with image masonry layout and hover effects.
export default function App() { return <h1>Hello world</h1> }
Team Member Directory
A responsive team directory with member cards organized in columns.
export default function App() { return <h1>Hello world</h1> }
News Feed Timeline
A dynamic news feed with alternating column layout and timeline indicators.
export default function App() { return <h1>Hello world</h1> }
Customization Examples
Blog Grid
A flexible blog grid using custom column width utilities to adapt across screen size.
export default function App() { return <h1>Hello world</h1> }
Masonry Photo Grid
A dynamic masonry-style photo grid layout with custom column width.
export default function App() { return <h1>Hello world</h1> }
Minimal Art Collection
A clean, minimalist layout for art collections with custom column width.
export default function App() { return <h1>Hello world</h1> }
Best Practices
Maintain Design Consistency
To maintain consistency, define clear guidelines on how and when to use columns-* utilities in your code. Establishing fixed column widths for content sections, such as columns-2 or columns-3, ensures that similar sections have a harmonious appearance across different pages.
Additionally, define project-specific columns utilities for frequently used column structures to enhance maintainability. Standardized column classes ensure that the same grid logic applies across multiple components, reducing unnecessary styling variations and enforcing a more predictable design language.
Build Responsive Design
Tailwind’s responsive modifiers (sm:, md:, lg:, etc.) allow for dynamic column adjustments based on screen sizes. You can define a fixed number of columns using sm:columns-2 md:columns-3 lg:columns-4, etc. Alternatively, you can specify column width such as sm:columns-sm md:columns-md lg:columns-lg, etc. so the number of columns adjust automatically based on available space.
Regardless of the approach, thorough testing is crucial to ensure the layout behaves as expected across various devices and resolutions. Elements may shift unexpectedly, especially with dynamic widths, so checking for readability, spacing, and accessibility issues is essential.
Accessibility Considerations
Enhance Readability and Navigability
Column-based layouts should prioritize readability and ease of navigation. Ensure that text flows naturally across columns preventing cognitive overload for users. Overly narrow columns with small text sizes can be difficult to read, while excessively wide columns may cause strain when tracking from one line to the next.
Use appropriate text size utilities to improve readability within multi-column setups. Additionally, ensure sufficient line height (leading-relaxed) to enhance text clarity and reduce visual clutter.
When designing interactive elements within columns, ensure sufficient spacing and contrast. Provide clear visual separation between interactive and non-interactive elements using borders, outlines, or shadows to help users differentiate between actionable components and static content.
Ensure Keyboard Accessibility
Keyboard navigation should work seamlessly when implementing column-based layouts.To maintain a smooth navigation flow, ensure tabindex is properly assigned to important non-clickable elements like <div>, <section>, or <span>, allowing them to be included in the focus order when necessary. This helps users who rely on keyboards move through the content logically without skipping over critical sections.
Additionally, apply clear focus indicators to improve accessibility by making interactive elements easier to identify. Use Tailwind’s focus:outline-* and focus:ring-* utilities to ensure that buttons, inputs, and links remain visible when selected via the keyboard. Since column layouts can sometimes feel disconnected when tabbing through elements, these visual cues help users stay oriented.