【问题标题】:CSS - Child height relative to viewport if parent has position:fixed [duplicate]CSS - 如果父级具有位置,则相对于视口的子级高度:固定 [重复]
【发布时间】:2018-03-01 22:59:36
【问题描述】:

我需要使孩子的高度相对于视口。使用 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/

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

我应该如何进行?我已经尝试了很多其他的东西,但没有足够的结果

【问题讨论】:

  • 我不太确定你在问什么。您是否在问如何消除 t1t2 之间的小差距?你能不能只给t2一个否定的margin-top?或者你在问如何让t2占据剩余高度的100%?这将通过calc() 完成。
  • 我在你的代码中没有看到position:fixed

标签: javascript html css styles


【解决方案1】:

我会像 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>

【讨论】:

  • 能否请您澄清20px 的来源?
  • @KoshVery 我添加了它。 OP 没有对“分隔线”的设定高度指定任何限制,他要求提供指导。
猜你喜欢
  • 1970-01-01
  • 2018-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-28
相关资源
最近更新 更多