"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Create Side-by-Side Divs with Equal Auto Widths using CSS?

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

Published on 2024-11-14
Browse:362

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;
}

HTML Example:

Note:

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

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3