html - 如何使用CSS显示等宽的div?

标签 html css

我正在从数据库中获取输出。有时我得到 2 个 div,有时我得到 3 个 div,所以我必须设置相等的 div 宽度。我的意思是,如果有 2 个 div,则设置每个的 50%,如果有 3 个 div,则设置每个的 33.33%。你愿意帮我吗?

 #container1
  {
    width: 100%;
    display: inline-flex;
  }
#container1 div{
      color: #fff;
    display: inline-block;
    height: 100%;
    width: 50%;
    background: #24252A;
    text-align: center;
    cursor: default;
    padding: 2em 0;
}

#container1 div:nth-of-type(2) {
    background: red;
    }

#container1 div:nth-of-type(3)
{
    width: 33.33% !important;
}
<div id="container1">
  <!--div depanding upload database-->
  <div></div>
  <div></div>
  <div></div>
</div>

最佳答案

使用 flex: 1 100% 并移除宽度:

 #container1
  {
    width: 100%;
    display: inline-flex;
  }
#container1 div{
      color: #fff;
    display: inline-block;
    height: 100%;
    flex: 1 100%;
    background: #24252A;
    text-align: center;
    cursor: default;
    padding: 2em 0;
}

#container1 div:nth-of-type(2) {
    background: red;
    }
<div id="container1">
  <!--div depanding upload database-->
  <div></div>
  <div></div>
  <div></div>
</div>

 #container1
  {
    width: 100%;
    display: inline-flex;
  }
#container1 div{
      color: #fff;
    display: inline-block;
    height: 100%;
    flex: 1 100%;
    background: #24252A;
    text-align: center;
    cursor: default;
    padding: 2em 0;
}

#container1 div:nth-of-type(2) {
    background: red;
    }
<div id="container1">
  <!--div depanding upload database-->
  <div></div>
  <div></div>
</div>

关于html - 如何使用CSS显示等宽的div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45958566/

相关文章:

css - 使用 css 为 meteor/cordova 应用程序提供原生外观,使其适应平台构建

asp.net - 添加文本时防止多行文本框拉伸(stretch)

css - 并排放置 div 的正确方法

html - 具有动态内容的两个相邻 DIV。一个包裹内容,另一个滚动溢出

html - 如何在一行中等高显示四个li标签?

javascript - 有没有办法只在 HTML 表单中的某些字段上使用 tabindex?

javascript - 认为我的 ajax 有问题吗?

jquery - jScrollPane v2.0.0 (beta11) 谷歌浏览器中的奇怪行为

CSS如何制作一个从顶部开始100px的DIV到达页面底部

html - :after on a absolutely positioned div