CSS - 如果父级具有位置,则相对于视口的子高度:fixed [duplicate]

问题描述 投票:-1回答:1

这个问题在这里已有答案:

我需要相对于视口设置子高度。使用HTML:

<div class="parent">My divider
  <div class="child">
      Inside the div
  </div>
</div>

和CSS:

.t1 {
    background:#f00;
    color:#fff;
}

.t2 {
  background:#212121;
  height:100vh;
}

http://jsfiddle.net/hr8sL/7646/

从示例中可以看出,总是有这么小的差距,因为它考虑了父级的整体高度。但是,我只需要孩子从父母那里填充视口的“其余部分”。

我该怎么办?我尝试了很多其他的东西,但没有足够的结果

javascript html css styles
1个回答
0
投票

我会像this那样做。

使用css calc()函数,我们可以计算剩余高度。

body {
  margin: 0;
  /* This is used to reset any browser-default margins */
}

.t1 {
  background: #f00;
  color: #fff;
  height: 20px;
}

.t2 {
  background: #212121;
  height: calc(100vh - 20px);
}
<div class="t1">
  My divider
</div>
<div class="t2">
  Inside the div
</div>
© www.soinside.com 2019 - 2024. All rights reserved.