【问题标题】:Why height=100% doesn't work?为什么 height=100% 不起作用?
【发布时间】:2018-08-17 18:12:17
【问题描述】:

我使用了占用所有可用空间的第三方组件,即width=100%height=100%。我无法控制它。

我正在尝试将其放入以下布局,但它的height=100% 不起作用(我希望第三方组件占据所有绿色空间)。

为什么?你会如何解决这个问题?

.container {
  display: flex;
  flex-direction: column;
  width: 200px;
  height: 100px;
}

.header {
  display: flex;
  background-color: rgba(255, 0, 0, 0.5);
}

.content {
  flex-grow: 1;
  background-color: rgba(0, 255, 0, 0.5);
}

.third-party-component {
  height: 100%;
  width: 100%;
  background-color: rgba(0, 0, 255, 0.5);
}
<div class="container">
  <div class="header">Header</div>
  <div class="content">
    <div class="third-party-component">
      Third party component
    </div>
  </div>
</div>

【问题讨论】:

  • sn-p 正在按预期工作......

标签: html css flexbox


【解决方案1】:

一般来说,对于在height 上使用百分比来获取其父级高度的元素,父级需要height other than auto or being positioned absolute,否则height 将被计算为auto

基于这两个选项,正如您在评论中提到的,您自己的标题在高度上是动态的,您将获得绝对定位。

content 中添加absolute 的问题,它将被从流中取出并停止作为正常流的flex 项目,好消息,可以添加一个包装器设置为absolute。

堆栈sn-p

.container {
  display: flex;
  flex-direction: column;
  width: 200px;
  height: 100px;
}

.header {
  display: flex;
  background-color: rgba(255, 0, 0, 0.5);
}

.content {
  position: relative;                               /*  added  */
  flex-grow: 1;
  background-color: rgba(0, 255, 0, 0.5);
}

.wrapper {
  position: absolute;                               /*  added  */
  left: 0;                                          /*  added  */
  top: 0;                                           /*  added  */
  right: 0;                                         /*  added  */
  bottom: 0;                                        /*  added  */
}

.third-party-component {
  height: 100%;
  width: 100%;
  background-color: rgba(0, 0, 255, 0.5);
}
<div class="container">
  <div class="header">Header</div>
  <div class="content">
    <div class="wrapper">
      <div class="third-party-component">
       Third party component
      </div>
    </div>
  </div>
</div>

另一个选项可能是更新 Flexbox 属性,给content 一个高度,使用flex: 1 1 100% 并给header flex-shrink: 0; 所以它不会缩小(因为content 得到了100%)。

这可能不适用于 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 class="container">
  <div class="header">Header</div>
  <div class="content">
    <div class="third-party-component">
      Third party component
    </div>
  </div>
</div>

【讨论】:

    【解决方案2】:

    因为.content 没有高度(高度 = 0px),而.third-party-component 有 100% 的 0px。您可以将属性height : calc (100% - &lt;height of .header&gt;) 添加到.content

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

    【讨论】:

    • 因为.content 没有高度(高度 = 0px),而.third-party-component 有 100% 的 0px。您可以将属性height : calc (100% - &lt;height of .header&gt;) 添加到.content
    • 问题是标题的高度是动态的/未知的。
    • 示例:.header 的高度为 20%,您必须将 .content 的高度设置为 80%,或者如果未设置 .header 的高度,我认为您应该使用 JS 来获取高度的.header 然后为.content 设置属性高度
    • 我看到你被否决了(而不是被我),因为有人没有告诉我为什么,我会的。 (height = 0px) 和组件具有 100% of 0px 的陈述都是错误的。检查我的回答它是如何工作的。
    【解决方案3】:

    您可以简单地在 .content 元素中使用另一个弹性容器:

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

    【讨论】:

    • 注意不能修改第三方组件,所以添加flex-grow: 1不算解决办法。
    • 定义 css 选择器和规则不会修改“组件”。你总是可以这样做的。如果您不想按类(.content &gt; div 或其他变体)定位,则可以使用嵌套选择器,如果这对您有影响的话。
    • 感谢您的回答,但这个问题的重点是如何使用第三方组件而不弄乱其样式。
    猜你喜欢
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多