CSS - Multiple-column Layout

Hello, aspiring web developers! Today, we're going to dive into the exciting world of CSS multiple-column layouts. As your friendly neighborhood computer teacher, I'm here to guide you through this journey with plenty of examples and explanations. So, let's get started!

CSS - Multi columns

Introduction to Multi-column Layouts

Imagine you're reading a newspaper. Notice how the text is organized into neat columns? That's exactly what we're going to achieve with CSS, but on web pages! Multi-column layouts allow us to create visually appealing and easy-to-read content structures.

CSS Multiple-column Layout - Three-column Layout

Let's start with a basic three-column layout. This is perfect for creating magazine-style content or organizing information on a webpage.

<div class="three-column">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
.three-column {
  column-count: 3;
  column-gap: 40px;
}

In this example, we're using the column-count property to specify that we want three columns. The column-gap property sets the space between each column.

CSS Multiple-column Layout - Setting column-width

Sometimes, instead of specifying the number of columns, you might want to set a specific width for each column. Let's see how that works:

<div class="flexible-columns">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
.flexible-columns {
  column-width: 200px;
}

Here, we're using column-width to set each column to be 200 pixels wide. The browser will create as many columns as it can fit while maintaining this width.

CSS Multiple-column Layout - Styling Columns

Now, let's add some style to our columns. We can add borders, change background colors, and more!

<div class="styled-columns">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
.styled-columns {
  column-count: 3;
  column-gap: 40px;
  column-rule: 4px dotted #ff00ff;
  column-rule-style: dotted;
  column-rule-width: 4px;
  column-rule-color: #ff00ff;
}

In this example, we're using column-rule as a shorthand property to add a dotted line between columns. We can also set these properties individually using column-rule-style, column-rule-width, and column-rule-color.

CSS Multiple-column Layout - Spanning Columns

Sometimes, you might want a piece of content to span across multiple columns. Let's see how we can achieve that:

<div class="spanning-columns">
  <h2>This heading spans all columns</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
  <p class="span-two">This paragraph spans two columns. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
  <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
</div>
.spanning-columns {
  column-count: 3;
  column-gap: 40px;
}

.spanning-columns h2 {
  column-span: all;
  text-align: center;
}

.span-two {
  column-span: 2;
  background-color: #f0f0f0;
  padding: 10px;
}

Here, we're using column-span: all to make the heading span across all columns, and column-span: 2 to make a paragraph span across two columns.

CSS Multiple-column Layout - Columns and Fragmentation

Fragmentation refers to how content is split across columns. Let's look at how we can control this:

<div class="fragmented-columns">
  <h3>Chapter 1</h3>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
  <h3>Chapter 2</h3>
  <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
.fragmented-columns {
  column-count: 2;
  column-gap: 40px;
}

.fragmented-columns h3 {
  break-after: column;
}

In this example, we're using break-after: column to ensure that each chapter heading starts at the top of a new column.

Related Properties

Let's summarize all the properties we've learned in a handy table:

Property Description
column-count Specifies the number of columns
column-width Sets the width of each column
column-gap Defines the space between columns
column-rule Shorthand for setting a line between columns
column-rule-style Sets the style of the column rule
column-rule-width Sets the width of the column rule
column-rule-color Sets the color of the column rule
column-span Allows an element to span across columns
break-after Controls column breaks after an element

And there you have it, folks! We've journeyed through the land of CSS multi-column layouts. Remember, practice makes perfect, so don't be afraid to experiment with these properties. Who knows? You might create the next big magazine-style website!

As my old professor used to say, "CSS is like cooking. You might make a mess at first, but with practice, you'll be creating masterpieces in no time!" So keep coding, keep learning, and most importantly, have fun with it!

Credits: Image by storyset