Multi Column Layouts
CSS has had multi column layout capabilities for years now but I don't think many people (including me) have figured how to use them effectively. In this post, I'll explore the various ways to create multi column layouts using CSS, basic usage, and how to make the content more readable. ## CSS Multi Column Basics The CSS multi-column layout module lets you arrange content into multiple columns, similar to how text flows in a newspaper. It's a powerful and simple way to create magazine-style layouts without complex floats or flexbox/grid setups. ### Creating Columns You can create columns in two primary ways: by defining the number of columns you want or by defining the width you want each column to be. **column-count** This property specifies the exact number of columns the content should be divided into. The browser will then figure out how wide each column should be. ```css .container { column-count: 3; } ``` **column-width** This property defines the ideal width for the columns. The browser will create as many columns of at least this width as can fit into the available space. This makes your layout responsive by default. ```css .container { /*The browser will create as many 200px columns as it can.*/ column-width: 200px; } ``` **columns (Shorthand)** You can also use the shorthand `columns` property to set both `column-width` and `column-count`. ```css .container { /* Creates 3 columns, but only if the container is wide enough to support columns of at least 150px */ columns: 3 150px; } ``` ## Styling the Gaps and Rules You can style the space and the dividing line between columns. Unlike Flexbox and CSS Grid, multi column layouts don't use the [gap](https://developer.mozilla.org/en-US/docs/Web/CSS/gap) property; instead they have their own specific attributes [column-gap](https://developer.mozilla.org/en-US/docs/Web/CSS/column-gap) This property sets the size of the gap between columns. Multi-column layouts have their own gap property, separate from the standard `gap` property used in Flexbox and Grid. ```css .container { column-count: 3; /* 40px gap between columns */ column-gap: 40px; } ``` [column-rule-width](https://developer.mozilla.org/en-US/docs/Web/CSS/column-rule-width) The property defines the width of the line between columns. This property is specific to multi-column layouts. ```css .container { column-count: 2; column-rule-style: solid; column-rule-width: 10px; } ``` [column-rule-style](https://developer.mozilla.org/en-US/docs/Web/CSS/column-rule-style) This property changes the appearance of the line. It accepts a specific list of values. ```css .container { column-count: 3; column-gap: 20px; column-rule-width: 4px; column-rule-style: dashed; } ``` The possible values are the same as those for border style: * `none`: (Default) No rule is drawn. The column-rule-width is ignored. * `hidden`: The same as none. No rule is drawn. * `solid`: A single, solid, straight line. * `dotted`: A series of round dots. * `dashed`: A series of short dashes. * `double`: Two parallel solid lines. The column-rule-width is the sum of the two lines and the space between them. * `groove`: A 3D "carved" effect that appears to be etched into the page. * `ridge`: The opposite of groove, creating a 3D "embossed" effect that appears to come out from the page. * `inset`: A 3D effect that makes the content inside the columns appear depressed or sunken. * `outset`: The opposite of inset, creating a 3D effect that makes the content appear raised. [column-rule-color](https://developer.mozilla.org/en-US/docs/Web/CSS/column-rule-color) This property sets the color of the rule between columns. It supports any color available in CSS. ```css .container { column-count: 2; column-gap: 20px; column-rule-width: 4px; column-rule-style: dashed; column-rule-color: oklch(57.1% 0.222 20.1); } ``` [column-rule](https://developer.mozilla.org/en-US/docs/Web/CSS/column-rule) shorthand This property is a shorthand for [column-rule-width](https://developer.mozilla.org/en-US/docs/Web/CSS/column-rule-width), [column-rule-style](https://developer.mozilla.org/en-US/docs/Web/CSS/column-rule-style), and [column-rule-color](https://developer.mozilla.org/en-US/docs/Web/CSS/column-rule-color). ```css .container { column-count: 3; column-gap: 2rem; /* 2px blue dotted line */ column-rule: 2px dotted blue; } ``` ## Spanning Across Columns You can make an element break out of the column structure and span the entire width of the container. This is perfect for headlines, images, or blockquotes that need to be a focal point. **column-span** This property makes an element span across all columns. ```css .full-width-headline { column-span: all; } ``` The possible values are: none : The default value. The element stays within a single column. all : The element spans across all columns. When an element has `column-span: all;`, it acts as a divider. The content before the spanning element fills the columns above it, and the content after it continues filling the columns below it. **Example: Making a headline span all columns.** Add the `full-width-headline` class to the headings you want to span the full width. All others headings will remain inside their columns. ```html
Some text that flows into columns...
A Sprawling Headline
More text that continues to flow below the headline...
Some text that flows into columns...
More text that continues to flow...