DEV Community

Cover image for Enhancing Web Layouts with CSS Counters and Columns
Sharique Siddiqui
Sharique Siddiqui

Posted on

Enhancing Web Layouts with CSS Counters and Columns

When creating rich, well-structured web pages, sometimes you need more than just basic styling. You want your content to be organized, easy to read, and visually appealing. Two powerful, yet often underused, CSS features are CSS Counters and CSS Columns.

They help with automatic numbering and multi-column layouts, bringing both functionality and elegance to your designs without relying on JavaScript or complicated markup. Let’s explore these features and see how to use them effectively.

What Are CSS Counters?

CSS Counters are like automatic numbering tools built right into CSS. They let you keep track of how many times an element appears and display numbers, letters, or custom symbols accordingly—perfect for lists, headings, steps, or any content that benefits from ordered labeling.

How CSS Counters Work

  • Create a counter and initialize it in a container with counter-reset.
  • Increment the counter for each relevant element with counter-increment.
  • Display the counter value using the content property and the counter() or counters() functions.
Basic Example: Numbering Sections
css
body {
  counter-reset: section; /* Initialize counter */
}

h2 {
  counter-increment: section; /* Increase per heading */
}

h2::before {
  content: counter(section) ". ";
  font-weight: bold;
}
Enter fullscreen mode Exit fullscreen mode

With this, each <h2> element will automatically prepend a numbered label like “1. ”, “2. ”, etc.

Advanced CSS Counters Usage

Nested Counters for Subsections

You can nest counters to create hierarchical numbering such as “1.1”, “1.2”, “2.1”:

css
body {
  counter-reset: section;
}

h2 {
  counter-increment: section;
  counter-reset: subsection;
}

h2::before {
  content: counter(section) ". ";
}

h3 {
  counter-increment: subsection;
}

h3::before {
  content: counter(section) "." counter(subsection) " ";
}
Enter fullscreen mode Exit fullscreen mode

Perfect for structuring manuals, FAQs, or reports.

What Are CSS Columns?

CSS Columns allow you to create multi-column text layouts, similar to what you might see in newspapers or magazines, improving readability especially on wide screens.

Syntax Basics
css
.container {
  column-count: 3;   /* Number of columns */
  column-gap: 20px;  /* Space between columns */
}
Enter fullscreen mode Exit fullscreen mode

This splits the content inside .container into three evenly spaced columns.

Benefits of CSS Columns
  • Improved readability: Long blocks of text broken into narrower columns are easier to scan.
  • Automatic column flow: Content flows naturally from one column to the next without extra markup.
  • Easy customization: Adjust number, width, gaps, and rules (borders between columns).

Examples of CSS Columns

Control Column Width

Instead of a fixed column count, you can specify a minimum width that columns can shrink to:

css
.container {
  column-width: 200px;
  column-gap: 1rem;
}
Enter fullscreen mode Exit fullscreen mode

Browsers will create as many columns as fit inside the container width.

Add Rules Between Columns
css
.container {
  column-count: 2;
  column-gap: 16px;
  column-rule: 1px solid #ccc; /* Adds vertical line */
}
Enter fullscreen mode Exit fullscreen mode

Combining Counters and Columns: Practical Use Case

Imagine a multi-column numbered list:

xml
<ol class="multi-column">
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
  <li>Fourth item</li>
  <li>Fifth item</li>
</ol>
Enter fullscreen mode Exit fullscreen mode
css
.multi-column {
  column-count: 2;
  column-gap: 1.5rem;
  counter-reset: item;
  list-style-type: none;
  padding-left: 0;
}

.multi-column li {
  counter-increment: item;
  margin-bottom: 1rem;
  position: relative;
  padding-left: 2rem;
}

.multi-column li::before {
  content: counter(item) ". ";
  position: absolute;
  left: 0;
  font-weight: bold;
}
Enter fullscreen mode Exit fullscreen mode

This will render the numbered list across two columns with automatic numbering preserved.

Browser Support and Considerations

CSS counters and columns are well-supported on all modern browsers. Some older browsers may have quirks with column breaks or nested counters but are rare nowadays.

Final Thoughts

CSS Counters and Columns boost your ability to create polished, professional layouts and document-like experiences on the web — without needing JavaScript or complex markup.

Whether you’re numbering chapters, creating multi-column articles, or laying out complex documents, these CSS tools are simple to implement and widely supported, making your CSS both elegant and efficient.

Check out the YouTube Playlist for great CSS content for basic to advanced topics.

Please Do Subscribe Our YouTube Channel for clearing programming concept and much more ...CodenCloud

Top comments (0)