html - 将 DIV 保持在静态高度,但其上方的 DIV 处于动态高度

标签 html css

.getsmaller {
    display: block;
    width: 1000px;
    height: 150px;
    background: red;
}
<div class='getsmaller'></div>
<div class='additions'>
    Lorem ipsum dolor sit amet
</div>

在这个 HTML 结构中,如何使 height 根据 .additions 变小?

例如,正如第一个示例中所写的那样,它在一行中,现在当它超过一行时我想要发生的是使 .getsmaller 高度变小并将 .addition 移到更高的位置,像这样。

.getsmaller {
    display: block;
    width: 1000px;
    height: 120px;
    background: red;
}
<div class='getsmaller'></div>
<div class='additions'>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>

为了稍微清除它,我想将 .addition 保留在它的位置并让它变得更高,而不是向下移动。但是只改变.getsmaller

的高度

最佳答案

你可以像这样使用 flex:

body { /* I used the body as the main container but it can be any other element*/
  display: flex;
  flex-direction: column;
  height: 100vh;
  margin: 0;
}

.getsmaller {
  flex: 1; /* this will make the div take the remaining spaces so it will get smaller when the cotnent of the other grow*/
  background: red;
}
<div class='getsmaller'></div>
<div class='additions'>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>

关于html - 将 DIV 保持在静态高度,但其上方的 DIV 处于动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48492178/

相关文章:

html - IE9+ 不喜欢媒体查询 css

jquery - tabber 在页面加载时闪烁

html - 传单 map 未填满整个包装器 div

PHP - 如何创建 4 列 (4 x 25%),然后以新的 4 列结束该行

html - 图像大小为 100% 的图像贴图

php - 在 jquery 中发布不更新元素

javascript - 在 JS 中进行全尺寸跳跃之前,角色会先做一个小跳跃

javascript - 对具有 ID 的元素的 HTML href

jquery - 带有 jQ​​uery/CSS 的嵌套选项卡菜单

html - 试图将一个 div 标签置于一个 section 标签的中心