为什么高度= 100%不起作用?

为什么高度= 100%不起作用?,第1张

为什么高度= 100%不起作用?

通常,对于使用percent on height来获取其父代的高度的元素,父代需要的高度不是auto或定位为绝对高度,否则height将计算为auto。

基于这两个选项,并且正如您在注释中提到的那样,您自己的标题的高度是动态的,您将得到绝对的定位。

绝对值添加到的问题content将被淘汰,并且不再像正常流动的flex项那样运行,这是一个好消息,可以将包装器设置为绝对值。

堆栈片段

.container {  display: flex;  flex-direction: column;  width: 200px;  height: 100px;}.header {  display: flex;  background-color: rgba(255, 0, 0, 0.5);}.content {  position: relative;           flex-grow: 1;  background-color: rgba(0, 255, 0, 0.5);}.wrapper {  position: absolute;           left: 0;           top: 0;            right: 0;          bottom: 0;       }.third-party-component {  height: 100%;  width: 100%;  background-color: rgba(0, 0, 255, 0.5);}<div >  <div >Header</div>  <div >    <div >      <div >       Third party component      </div>    </div>  </div></div>

另一个选择可能是更新Flexbox属性,content使用flex: 1 1 100%,给定高度,使用高度和高度,header flex-shrink: 0;使其不收缩(达到content100%)。

不过,这可能无法在Safari上运行,因为我知道height未设置该属性时会遇到问题,但由于无法访问Safari,现在无法测试。

.container {  display: flex;  flex-direction: column;  width: 200px;  height: 100px;}.header {  display: flex;  flex-shrink: 0;  background-color: rgba(255, 0, 0, 0.5);}.content {  flex: 1 1 100%;  background-color: rgba(0, 255, 0, 0.5);}.third-party-component {  height: 100%;  width: 100%;  background-color: rgba(0, 0, 255, 0.5);}<div >  <div >Header</div>  <div >    <div >      Third party component    </div>  </div></div>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5649876.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存