首頁 > web前端 > css教學 > 如何使用 CSS 建立具有相同自動寬度的並排 Div?

如何使用 CSS 建立具有相同自動寬度的並排 Div?

DDD
發布: 2024-11-15 07:39:03
原創
208 人瀏覽過

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:

<div>
登入後複製

Note:

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

以上是如何使用 CSS 建立具有相同自動寬度的並排 Div?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板