Home > Web Front-end > CSS Tutorial > How to Make Side-by-Side Divs Have Equal Widths in CSS?

How to Make Side-by-Side Divs Have Equal Widths in CSS?

Barbara Streisand
Release: 2024-11-14 13:31:02
Original
895 people have browsed it

How to Make Side-by-Side Divs Have Equal Widths in CSS?

Equal Widths for Side-by-Side Divs in CSS

In HTML, organizing elements within a parent container is often achieved using div elements. However, aligning multiple child divs side by side with equal widths can pose a challenge.

Consider the following HTML structure:

<div>
Copy after login

In this example, the child divs are aligned side by side, but they have arbitrary widths. To achieve equal widths, one can employ CSS magic using the display: table property.

#wrapper {
    display: table;
    table-layout: fixed;

    width:90%;
    height:100px;
    background-color:Gray;
}
#wrapper div {
    display: table-cell;
    height:100px;
}
Copy after login

This CSS solution has several key features:

  • Sets the parent div (#wrapper) to display as a table (display: table) and sets table-layout: fixed to ensure equal column widths.
  • Sets the child divs (#wrapper div) to display as table cells (display: table-cell) with equal heights.

Benefits:

  • Works in modern browsers (IE7 is not supported)
  • Automatically adjusts column widths based on available space
  • Allows for flexible layouts with varying numbers of child divs

Live Examples:

  • Two Divs: [JSFiddle](https://jsfiddle.net/g4dGz/1/)
  • Three Divs: [JSFiddle](https://jsfiddle.net/g4dGz/)

The above is the detailed content of How to Make Side-by-Side Divs Have Equal Widths in CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template