【问题标题】:How to make child element/component ignore size of its parent?如何让子元素/组件忽略其父元素的大小?
【发布时间】:2019-09-19 08:28:06
【问题描述】:

我在组件 B(标题)内渲染组件 A(全屏覆盖)。我将组件A指定为height: 100vh,它确实是我屏幕的高度,只是它被我的标题大小向下推。

我希望它既是屏幕的高度又覆盖整个屏幕,但不指定margin-top: -$(size of my header)

我该怎么做?

【问题讨论】:

  • 你试过position: fixed;position: absolute;吗?

标签: css reactjs sass


【解决方案1】:

通过使用属性position 和值fixed

如 cmets 中所述,您可以使用 position 属性,如下所示:

<div class="componentA">test</div>
.componentA {
    position: fixed; // we make it position relative to the viewport.
    top:0; // top edge of content box to top position 0
    right:0; // right edge of content box to right position 0
    bottom:0; // bottom edge of content box to bottom position 0
    left:0; // left edge of content box to left position 0
}

正如你所看到的,我们现在已经将具有类 componentA 的 div 的 内容框 边缘定位在视口的零点(上、右、下、左)。如果这有意义的话。

关于位置:固定;(来自w3school

带有position: fixed; 的元素相对于视口定位,这意味着即使页面滚动,它也始终保持在同一个位置。 top、right、bottom 和 left 属性用于定位元素。

视口(来自this answer

视口是用户当前可以看到的网页部分。滚动条移动视口以显示页面的其他部分。

更具体地说,视口与屏幕分辨率或浏览器窗口的大小无关;它仅指用户在任何给定时间可以看到网页的空间。

【讨论】:

  • 谢谢!将position 设置为固定,top 设置为 0,width 设置为 100% 有效!只是现在我无法再访问最初用来安装叠加层的按钮组件......摆弄 z-index 但这没有产生任何结果。您是否知道我如何提升单个组件,使其即使在这个定位的固定叠加层(即组件 A 的顶部)上也“高于”。
  • 将组件 b 设置为 z-index: 0,然后将 componentA 设置为 1,最后将按钮组件设置为 z-index 3。如果我的回答对您有帮助,请善待并将其设置为“已回答”: )
  • @ZeyadShaban 答案与 React 无关,所以我不明白你为什么抱怨。
猜你喜欢
  • 2015-06-08
  • 1970-01-01
  • 2020-08-11
  • 1970-01-01
  • 2017-08-20
  • 2017-02-11
  • 1970-01-01
  • 1970-01-01
  • 2011-09-30
相关资源
最近更新 更多