【问题标题】:Have div take up 1/6th of remaining parent div height让 div 占据剩余父 div 高度的 1/6
【发布时间】:2016-06-27 05:47:35
【问题描述】:

我正在尝试使用 css 制作一个简单的日历。

我有一个包含日历的父 div,我有一个包含“星期一”、“星期二”等标题的 div,并且高度固定。我现在想添加代表日历行的 div,并将剩余空间分成六个偶数行。但是,我不知道如何将剩余空间分成 6 个部分。我尝试的一切都使 div 成为父 div 的 1/6。

任何提示将不胜感激。

HTML:

<div id="parent>
    <div id="header">
        ST
    </div>
    <div class="row">
        hi
    </div>
</div>

CSS:

.row{
    width:100%;
    height: 16.66%;
    background-color:red;
}

【问题讨论】:

  • 我们能看看你目前有什么吗?
  • 查看问题。我对其进行了编辑以包含一些 html 和 css

标签: css


【解决方案1】:

当您想要分配灵活元素留下的剩余空间时,flexbox 就是答案。

html, body, #parent {
  margin: 0;
  height: 100%;
}
#parent {
  display: flex;
  flex-direction: column;
}
#header {
  background-color: green;
}
.row {
  width: 100%;
  flex: 1; /* Distribute remaining space equally among the rows */
  background-color: red;
}
.row:nth-child(odd) {
  background-color: blue;
}
<div id="parent">
  <div id="header">Header</div>
  <div class="row"></div>
  <div class="row"></div>
  <div class="row"></div>
  <div class="row"></div>
  <div class="row"></div>
  <div class="row"></div>
</div>

【讨论】:

    【解决方案2】:

    有几种方法可以做到这一点,要选择一种我需要了解更多应该如何使用它。

    此示例仅使用 CSS calc() 并从父级的 1/6 中减去标头的 1/6。

    html, body {
      margin: 0;
    }
    #parent {
      height: 100vh;
    }
    #header {
      height: 60px;
      background-color:green;
    }
    .row{
      height: calc(16.66% - 10px);
      background-color:red;
    }
    .row:nth-child(odd){
      background-color:blue;
    }
    <div id="parent">
        <div id="header">
            Header
        </div>
        <div class="row">
        </div>
        <div class="row">
        </div>
        <div class="row">
        </div>
        <div class="row">
        </div>
        <div class="row">
        </div>
        <div class="row">
        </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-28
      • 2011-11-04
      • 2012-06-15
      • 1970-01-01
      • 2019-03-18
      • 2018-04-09
      • 2018-10-23
      相关资源
      最近更新 更多