How to Create Side-by-Side Divs with Equal Auto Widths using CSS?

DDD
Release: 2024-11-15 07:39:03
Original
145 people have browsed it

How to Create Side-by-Side Divs with Equal Auto Widths using CSS?

CSS Side-by-Side Divs with Equal Auto Widths

Achieving equal auto widths for child DIVs within a parent DIV can be tricky with traditional float and width approaches. However, using the display: table property provides a modern solution that enables this functionality.

Solution using Display: Table

  1. Set the parent DIV (#wrapper) to display as a table and enable a fixed table layout. This will ensure that child DIVs occupy equal widths.
  2. Set the height of the parent DIV to specify the vertical space available.
  3. For each child DIV, set the display property to table-cell to ensure they appear side-by-side and have the same height as the parent DIV.

CSS Example:

#wrapper {
  display: table;
  table-layout: fixed;
  width: 90%;
  height: 100px;
  background-color: Gray;
}

#wrapper div {
  display: table-cell;
  height: 100px;
}
Copy after login

HTML Example:

<div>
Copy after login

Note:

  • This solution is not compatible with IE7 and below.
  • The provided JSFiddle examples demonstrate both three and two equal-width child DIVs.

The above is the detailed content of How to Create Side-by-Side Divs with Equal Auto Widths using 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template