CSS3 Multi-Column Layouts

By Codes With Pankaj


CSS3 Multi-Column Layouts allow you to divide content into multiple columns, similar to newspaper layouts. This feature is particularly useful for organizing text-heavy content, improving readability, and creating visually appealing layouts without the need for complex CSS or JavaScript. This tutorial will explain how to create multi-column layouts using CSS3, how to control column properties, and provide practical examples.

Table of Contents

  1. What are CSS3 Multi-Column Layouts?

  2. How to Create a Basic Multi-Column Layout

  3. CSS3 Multi-Column Properties

    • Column Count

    • Column Width

    • Column Gap

    • Column Rule

  4. Handling Content Spanning Across Columns

  5. Practical Examples

    • Example 1: Two-Column Layout

    • Example 2: Three-Column Layout with Column Rule

    • Example 3: Responsive Multi-Column Layout

  6. Best Practices for Using Multi-Column Layouts

  7. Cross-Browser Compatibility

  8. Conclusion


1. What are CSS3 Multi-Column Layouts?

CSS3 Multi-Column Layouts allow you to split content into multiple columns within a single container. This layout style is commonly used for long paragraphs or lists, making the content easier to read and visually appealing.

Key Concept:

  • Multi-Column Layout: A CSS layout that divides content into multiple columns within a single container.

Example:

.container {
  column-count: 3;
  column-gap: 20px;
}

Explanation:

  • This example divides the content inside the container into three columns with a 20px gap between each column.


2. How to Create a Basic Multi-Column Layout

Creating a basic multi-column layout is straightforward with CSS3. You can control the number of columns, their width, the gap between them, and more.

Basic Syntax:

selector {
  column-count: number;
  column-gap: value;
}

Example:

.container {
  column-count: 2;
  column-gap: 15px;
}

Explanation:

  • The content inside the .container is divided into two columns with a 15px gap between them.


3. CSS3 Multi-Column Properties

CSS3 provides several properties to control the behavior and appearance of multi-column layouts.

a. Column Count

The column-count property specifies the number of columns the content should be divided into.

Example:

.container {
  column-count: 3;
}

Explanation:

  • The content is divided into three columns.

b. Column Width

The column-width property specifies the ideal width of each column. The browser will calculate the number of columns based on this width and the available space.

Example:

.container {
  column-width: 200px;
}

Explanation:

  • The content is divided into columns that are approximately 200px wide. The actual number of columns will depend on the container's width.

c. Column Gap

The column-gap property specifies the space between columns.

Example:

.container {
  column-gap: 30px;
}

Explanation:

  • There is a 30px gap between each column.

d. Column Rule

The column-rule property adds a rule (a vertical line) between columns, similar to a border.

Example:

.container {
  column-rule: 2px solid #333;
}

Explanation:

  • A 2px solid line is placed between each column.

Shorthand for Column Rule:

  • column-rule-width: Sets the width of the column rule.

  • column-rule-style: Sets the style of the column rule (e.g., solid, dashed).

  • column-rule-color: Sets the color of the column rule.


4. Handling Content Spanning Across Columns

Sometimes, you might want an element or specific content to span across multiple columns rather than being confined to a single column. This can be achieved using the column-span property.

Example:

.heading {
  column-span: all;
  font-size: 24px;
  font-weight: bold;
}

Explanation:

  • The .heading element will span across all columns, creating a visual break or title across the entire width of the container.


5. Practical Examples

Let's explore some practical examples of using CSS3 multi-column layouts.

Example 1: Two-Column Layout

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Two-Column Layout - codeswithpankaj</title>
  <style>
    .container {
      column-count: 2;
      column-gap: 20px;
    }
  </style>
</head>
<body>

<div class="container">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla tincidunt.</p>
  <p>Vivamus lacinia odio vitae vestibulum vestibulum. Cras venenatis euismod.</p>
  <p>Mauris non tempor quam, et lacinia sapien. Mauris accumsan eros.</p>
</div>

</body>
</html>

Explanation:

  • The content inside the container is divided into two columns with a 20px gap between them.

Example 2: Three-Column Layout with Column Rule

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Three-Column Layout with Rule - codeswithpankaj</title>
  <style>
    .container {
      column-count: 3;
      column-gap: 20px;
      column-rule: 1px solid #ccc;
    }
  </style>
</head>
<body>

<div class="container">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla tincidunt.</p>
  <p>Vivamus lacinia odio vitae vestibulum vestibulum. Cras venenatis euismod.</p>
  <p>Mauris non tempor quam, et lacinia sapien. Mauris accumsan eros.</p>
  <p>Pellentesque habitant morbi tristique senectus et netus et malesuada.</p>
  <p>Suspendisse potenti. Etiam venenatis elit in arcu aliquet, ac volutpat.</p>
</div>

</body>
</html>

Explanation:

  • The content is divided into three columns with a 20px gap and a 1px solid line between each column.

Example 3: Responsive Multi-Column Layout

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Responsive Multi-Column Layout - codeswithpankaj</title>
  <style>
    .container {
      column-width: 200px;
      column-gap: 15px;
    }
  </style>
</head>
<body>

<div class="container">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla tincidunt.</p>
  <p>Vivamus lacinia odio vitae vestibulum vestibulum. Cras venenatis euismod.</p>
  <p>Mauris non tempor quam, et lacinia sapien. Mauris accumsan eros.</p>
  <p>Pellentesque habitant morbi tristique senectus et netus et malesuada.</p>
  <p>Suspendisse potenti. Etiam venenatis elit in arcu aliquet, ac volutpat.</p>
</div>

</body>
</html>

Explanation:

  • The content is divided into columns with a width of approximately 200px each. The number of columns adjusts automatically based on the container's width, making it responsive.


6. Best Practices for Using Multi-Column Layouts

When using CSS3 multi-column layouts, consider the following best practices:

  • Avoid Overuse: Use multi-column layouts for text-heavy content where readability is improved by splitting the text into columns.

  • Control Column Gaps: Adjust the column-gap to ensure sufficient space between columns, making the content easier to read.

  • Use Column Rules for Clarity: Adding a column-rule can help visually separate columns, especially in dense content.

  • Test Responsiveness: Ensure your

layout adapts well to different screen sizes by using column-width rather than column-count for responsive designs.


7. Cross-Browser Compatibility

CSS3 multi-column layouts are supported in all modern browsers. However, older versions of browsers like Internet Explorer may require vendor prefixes.

Example with Vendor Prefixes:

.container {
  -webkit-column-count: 3;
  -moz-column-count: 3;
  column-count: 3;
}

Explanation:

  • Including vendor prefixes ensures compatibility across different browsers.


8. Conclusion

CSS3 Multi-Column Layouts provide a simple yet powerful way to organize content into multiple columns, enhancing readability and visual appeal. By mastering the properties and techniques for creating multi-column layouts, you can create more dynamic and user-friendly web designs.

  • Column Count: Specifies the number of columns.

  • Column Width: Sets the ideal width of each column.

  • Column Gap: Controls the space between columns.

  • Column Rule: Adds a vertical line between columns.

By applying these properties effectively, you can design content layouts that are both functional and aesthetically pleasing.


For more tutorials and insights, visit Codes With Pankaj.

Last updated