Table of Contents
1. Using CSS Flexbox
2. Using CSS Grid
3. Using Float (Legacy method)
4. Using Inline-block (Simple cases)
Which method should you use?
Home Web Front-end CSS Tutorial How to create a multi-column layout with CSS?

How to create a multi-column layout with CSS?

Aug 05, 2025 am 05:13 AM

Use Flexbox for simple, responsive one-dimensional layouts with equal or flexible column widths. 2. Use CSS Grid for precise, two-dimensional layouts and responsive designs with auto-adjusting columns via repeat(auto-fit, minmax()). 3. Avoid float due to the need for clearfix and poor responsiveness. 4. Avoid inline-block because of whitespace sensitivity and limited layout control. For modern layouts, prefer Grid for complex designs and Flexbox for simpler, linear arrangements, ensuring maintainability and responsiveness.

How to create a multi-column layout with CSS?

Creating a multi-column layout in CSS can be done in several ways, depending on the level of control and responsiveness you need. Here are the most common and effective methods:

How to create a multi-column layout with CSS?

1. Using CSS Flexbox

Flexbox is one of the easiest and most flexible ways to create multi-column layouts. It’s ideal for one-dimensional layouts (either row or column-based).

Example: 3-column layout

How to create a multi-column layout with CSS?
.container {
  display: flex;
  gap: 20px; /* space between columns */
  padding: 20px;
}

.column {
  flex: 1; /* each column takes equal space */
  background-color: #f0f0f0;
  padding: 15px;
  border-radius: 8px;
}
<div class="container">
  <div class="column">Column 1</div>
  <div class="column">Column 2</div>
  <div class="column">Column 3</div>
</div>

Key points:

  • Use flex-wrap: wrap if you want columns to stack on small screens.
  • Control individual column width with flex: 2 1 0 (e.g., one column wider than others).
  • Combine with media queries for responsive behavior.

2. Using CSS Grid

CSS Grid is powerful for two-dimensional layouts and gives precise control over rows and columns.

How to create a multi-column layout with CSS?

Example: Responsive 3-column layout

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr; /* three equal columns */
  gap: 20px;
  padding: 20px;
}

/* Responsive: switch to 1 column on small screens */
@media (max-width: 768px) {
  .container {
    grid-template-columns: 1fr;
  }
}

You can also use repeat() and minmax() for more flexibility:

.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  padding: 20px;
}

This automatically adjusts the number of columns based on available space — great for responsive design.


3. Using Float (Legacy method)

Although outdated, float was widely used before Flexbox and Grid.

.column {
  float: left;
  width: 33.33%;
  padding: 10px;
}

/* Clear floats */
.row::after {
  content: "";
  display: table;
  clear: both;
}

Why avoid float:

  • Requires clearfix hacks.
  • Less flexible and harder to make responsive.
  • Not intended for layout purposes (originally for text wrapping images).

4. Using Inline-block (Simple cases)

Useful for basic layouts but sensitive to whitespace in HTML.

.container {
  font-size: 0; /* removes whitespace gap */
}

.column {
  display: inline-block;
  vertical-align: top;
  width: 33.33%;
  font-size: 16px;
  padding: 10px;
}

Whitespace in HTML (like line breaks) creates gaps between inline-block elements, so this method is less reliable.


Which method should you use?

  • For modern layouts: Use Flexbox or Grid.
    • Use Flexbox for simpler, linear layouts (e.g., navigation bars, equal-height columns).
    • Use Grid for complex, two-dimensional designs (e.g., dashboards, card grids).
  • Avoid float and inline-block for layout unless supporting very old browsers.

Responsive multi-column layouts work best with Grid using minmax() and auto-fit, or Flexbox with wrapping and media queries.

Basically, Flexbox and Grid make multi-column layouts straightforward and maintainable. Pick Grid for more control, Flexbox for simplicity.

The above is the detailed content of How to create a multi-column layout with CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1580
276
What are common CSS browser inconsistencies? What are common CSS browser inconsistencies? Jul 26, 2025 am 07:04 AM

Different browsers have differences in CSS parsing, resulting in inconsistent display effects, mainly including the default style difference, box model calculation method, Flexbox and Grid layout support level, and inconsistent behavior of certain CSS attributes. 1. The default style processing is inconsistent. The solution is to use CSSReset or Normalize.css to unify the initial style; 2. The box model calculation method of the old version of IE is different. It is recommended to use box-sizing:border-box in a unified manner; 3. Flexbox and Grid perform differently in edge cases or in old versions. More tests and use Autoprefixer; 4. Some CSS attribute behaviors are inconsistent. CanIuse must be consulted and downgraded.

What is the accent-color property? What is the accent-color property? Jul 26, 2025 am 09:25 AM

accent-color is an attribute used in CSS to customize the highlight colors of form elements such as checkboxes, radio buttons and sliders; 1. It directly changes the default color of the selected state of the form control, such as changing the blue check mark of the checkbox to red; 2. Supported elements include input boxes of type="checkbox", type="radio" and type="range"; 3. Using accent-color can avoid complex custom styles and extra DOM structures, and maintain native accessibility; 4. It is generally supported by modern browsers, and old browsers need to be downgraded; 5. Set accent-col

Describe the `vertical-align` property and its typical use cases Describe the `vertical-align` property and its typical use cases Jul 26, 2025 am 07:35 AM

Thevertical-alignpropertyinCSSalignsinlineortable-cellelementsvertically.1.Itadjustselementslikeimagesorforminputswithintextlinesusingvalueslikebaseline,middle,super,andsub.2.Intablecells,itcontrolscontentalignmentwithtop,middle,orbottomvalues,oftenu

How to purge unused CSS? How to purge unused CSS? Jul 27, 2025 am 02:47 AM

UseautomatedtoolslikePurgeCSSorUnCSStoscanandremoveunusedCSS;2.IntegratepurgingintoyourbuildprocessviaWebpack,Vite,orTailwind’scontentconfiguration;3.AuditCSSusagewithChromeDevToolsCoveragetabbeforepurgingtoavoidremovingneededstyles;4.Safelistdynamic

How to change text color in CSS? How to change text color in CSS? Jul 27, 2025 am 04:25 AM

To change the text color in CSS, you need to use the color attribute; 1. Use the color attribute to set the text foreground color, supporting color names (such as red), hexadecimal codes (such as #ff0000), RGB values (such as rgb(255,0,0)), HSL values (such as hsl(0,100%,50%)), and RGBA or HSLA with transparency (such as rgba(255,0,0,0.5)); 2. You can apply colors to any element containing text, such as h1 to h6 titles, paragraph p, link a (note the color settings of different states of a:link, a:visited, a:hover, a:active), buttons, div, span, etc.; 3. Most

css dark mode toggle example css dark mode toggle example Jul 30, 2025 am 05:28 AM

First, use JavaScript to obtain the user system preferences and locally stored theme settings, and initialize the page theme; 1. The HTML structure contains a button to trigger topic switching; 2. CSS uses: root to define bright theme variables, .dark-mode class defines dark theme variables, and applies these variables through var(); 3. JavaScript detects prefers-color-scheme and reads localStorage to determine the initial theme; 4. Switch the dark-mode class on the html element when clicking the button, and saves the current state to localStorage; 5. All color changes are accompanied by 0.3 seconds transition animation to enhance the user

How to use the CSS backdrop-filter property? How to use the CSS backdrop-filter property? Aug 02, 2025 pm 12:11 PM

Backdrop-filter is used to apply visual effects to the content behind the elements. 1. Use backdrop-filter:blur(10px) and other syntax to achieve the frosted glass effect; 2. Supports multiple filter functions such as blur, brightness, contrast, etc. and can be superimposed; 3. It is often used in glass card design, and it is necessary to ensure that the elements overlap with the background; 4. Modern browsers have good support, and @supports can be used to provide downgrade solutions; 5. Avoid excessive blur values and frequent redrawing to optimize performance. This attribute only takes effect when there is content behind the elements.

What are user agent stylesheets? What are user agent stylesheets? Jul 31, 2025 am 10:35 AM

User agent stylesheets are the default CSS styles that browsers automatically apply to ensure that HTML elements that have not added custom styles are still basic readable. They affect the initial appearance of the page, but there are differences between browsers, which may lead to inconsistent display. Developers often solve this problem by resetting or standardizing styles. Use the Developer Tools' Compute or Style panel to view the default styles. Common coverage operations include clearing inner and outer margins, modifying link underscores, adjusting title sizes and unifying button styles. Understanding user agent styles can help improve cross-browser consistency and enable precise layout control.

See all articles