Efficient Methods for Displaying Unordered Lists in Two Columns

Nov 10, 2025 · Programming · 18 views · 7.8

Keywords: HTML | CSS | Multi-column Layout | Unordered List | Browser Compatibility | JavaScript

Abstract: This article explores various techniques to display unordered lists in two columns using HTML and CSS. It covers modern CSS3 columns for compatible browsers, JavaScript-based solutions for legacy support like Internet Explorer, and alternative methods such as Flexbox and Grid. Detailed code examples and explanations are provided to ensure clarity and practical implementation.

Introduction

In web development, displaying unordered lists in multiple columns is a common requirement for improving layout and readability. This article addresses the challenge of transforming a standard unordered list into a two-column format, with a focus on cross-browser compatibility, including legacy browsers like Internet Explorer.

Modern Browser Solution with CSS3 Columns

The CSS3 columns module provides a straightforward method to split content into columns. For an unordered list, applying the columns property can achieve the desired layout. Here is a basic example:

ul {
  columns: 2;
  -webkit-columns: 2;
  -moz-columns: 2;
}

This CSS rule sets the list to display in two columns, with vendor prefixes for WebKit and Mozilla browsers to ensure broader compatibility. The content automatically flows into the columns, and for most modern browsers, this works seamlessly.

Legacy Browser Support with JavaScript

For browsers that do not support CSS3 columns, such as older versions of Internet Explorer, a JavaScript-based solution is necessary. This involves manipulating the DOM to rearrange list items into columns. Below is a pure JavaScript implementation inspired by common approaches:

function displayListInColumns(selector, columns) {
  const list = document.querySelector(selector);
  const items = Array.from(list.children);
  list.innerHTML = ''; // Clear existing items
  // Create columns
  for (let i = 0; i < columns; i++) {
    const column = document.createElement('ul');
    list.appendChild(column);
  }
  const columnElements = list.children;
  // Distribute items
  items.forEach((item, index) => {
    const columnIndex = index % columns;
    columnElements[columnIndex].appendChild(item);
  });
}
// Usage: displayListInColumns('.columns', 2);

This function takes a CSS selector and the number of columns, then redistributes the list items accordingly. It ensures that items are placed in a left-to-right, top-to-bottom order, matching the desired display.

Alternative Approaches

Beyond columns, CSS Flexbox and Grid offer modern alternatives for multi-column layouts. Flexbox allows for flexible item distribution, while Grid provides precise control over rows and columns. For instance, using CSS Grid:

ul {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
}

This creates a two-column grid where list items are automatically placed. Flexbox can be used similarly with display: flex; flex-wrap: wrap; and setting item widths.

Conclusion

Choosing the right method depends on browser support requirements and project constraints. CSS3 columns are ideal for modern browsers, while JavaScript fallbacks handle legacy cases. Flexbox and Grid provide robust, responsive alternatives. Developers should test across browsers to ensure consistent behavior.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.