html - 使用 % height 将元素的高度设置为元素的高度

标签 html css flexbox height css-position

我想将元素聊天框的高度设置为元素视频框的高度。但我不知道该怎么做。如果您向我发送一个不使用 JavaScript 的方法,我也会很高兴。

代码:

#content {
  width: 80%;
  max-width: 1300px;
  min-width: 900px;
  margin: 80px auto;
}
#stream {
  position: relative;
  padding-bottom: 56.25%;
  height: 0;
  overflow: hidden;
  width: 100%;
  height: auto;
  display: flex;
}
#video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
<div id="content">
  <div style="width:75%;display:inline-block;vertical-align:top;" id="videobox">
    <div id="stream">
      <iframe id="video" src="http://player.twitch.tv/?channel=marmeladenoma" height="720" width="1280" frameborder="0" scrolling="no" allowfullscreen="true">
      </iframe>
    </div>
  </div>
  <div style="width:25%;float:right;display:inline-block;background-color:rgb(3, 40, 74)" id="chatbox">
    hi
  </div>
</div>

最佳答案

只需将#contentdisplay设置为flex,然后设置#chatbox的height inherit。即:

#content {
    display: flex;
    ...
}

#chatbox {
    height: inherit;
    ...
}

因此,完整的样式规则是:

#content {
    display: flex;
    width: 80%;
    max-width: 1300px;
    min-width: 900px;
    margin: 80px auto;
}
#stream {
    position: relative;
    padding-bottom: 56.25%;
    height: 0;
    overflow: hidden;
    width: 100%;
    height: auto;
    display: flex;
}
#video {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}
#chatbox {
    height: inherit;
}

关于html - 使用 % height 将元素的高度设置为元素的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40511721/

相关文章:

html - WebForms UnobtrusiveValidationMode 需要 'jquery' 的 ScriptResourceMapping。请添加一个名为 jquery 的 ScriptResourceMapping(区分大小写)

javascript - 居中对齐 ul 中 li 的内容

css - 在 CSS3 上带有边框的波浪(或形状?)

html - 如何让两张图片适合 html 中的任何屏幕尺寸?

html - 在完美的中间对齐两个元素

html - 2x2 网格等高,尽管盒子内容量

jquery - 更改 Bootstrap Tab 动画和 css

html - 增加文本大小以仅适合一列的表格单元格

css - 如何在 IE11 中设置输入文本颜色,而不是占位符颜色

jquery - 隐藏超出外部 div 高度的内部 div