将div的高度根据前一个div的高度调整为适应父div。

huangapple go评论58阅读模式
英文:

Make div height adjustable to parent div based on previous div height

问题

有两个<div>,一个在另一个下面,第一个<div>包含一个可能有2-3行的标题。
要求第二个<div>,包含描述,将占用父<div>的剩余高度。如果内容很多,应该出现垂直滚动条。滚动条只应用于第二个<div>

这里可以找到jsfiddle链接: https://jsfiddle.net/6emsjuya/2

代码片段:

html,
body {
  height: 100%;
  width: 100%;
  margin: 0;
}

#main-container {
  height: 100%;
  width: 100%;
}

#fixed-height {
  background-color: #ebcaca;
  height: 80px;
}

#remaining-height {
  background-color: #ebe6e6;
  height: calc(100% - 100px);
  overflow-y: auto;
}
<div id="main-container">
  <div id="fixed-height">
    Fixed height
  </div>
  <div id="remaining-height">
    Remaining height 1<br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height <br> Remaining height
    <br>
  </div>
</div>

这里,现在第一个<div>的高度是固定的80px。应该是动态的,即根据内容,第一个<div>应该采用相应的高度,第二个<div>将占用剩余的高度,并在需要时出现滚动条。

英文:

There are two divs one below another, here first div will contain title that might have 2 -3 lines.
And the requirement is the second div which will contain the description will take remaining height of the parent div. And if the content is large then vertical scroll should be applicable. Scroll should be applicable to second div only.

Here you can find the jsfiddle link: https://jsfiddle.net/6emsjuya/2

Code snippet:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

html,
body {
  height: 100%;
  width: 100%;
  margin: 0;
}

#main-container {
  height: 100%;
  width: 100%;
}

#fixed-height {
  background-color: #ebcaca;
  height: 80px;
}

#remaining-height {
  background-color: #ebe6e6;
  height: calc(100% - 100px);
  overflow-y: auto;
}

<!-- language: lang-html -->

&lt;div id=&quot;main-container&quot;&gt;
  &lt;div id=&quot;fixed-height&quot;&gt;
    Fixed height
  &lt;/div&gt;
  &lt;div id=&quot;remaining-height&quot;&gt;
    Remaining height 1&lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining
    height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height
    &lt;br&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Here right now the first div has fixed height 80px. which should be dynamic i.e. based on the content first div should take height and second div will take remaining height with scroll if needed.

答案1

得分: 1

你可以使用弹性布局来实现你期望的效果。

html,
body {
  height: 100%;
  width: 100%;
  margin: 0;
}

#main-container {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
}

#fixed-height {
  background-color: #ebcaca;
}

#remaining-height {
  background-color: #ebe6e6;
  overflow-y: auto;
  height: 100%;
}
<div id="main-container">
  <div id="fixed-height">
    固定高度
  </div>
  <div id="remaining-height">
    剩余高度 1<br> 剩余高度 <br> 剩余高度 <br> 剩余高度 <br> 剩余高度 <br> 剩余高度 <br> 剩余高度 <br> 剩余高度 <br> 剩余高度 <br> 剩余高度 <br> 剩余高度 <br> 剩余高度 <br> 剩余高度 <br> 剩余高度 <br> 剩余高度 <br> 剩余高度 <br> 剩余高度 <br> 剩余高度 <br> 剩余高度 <br> 剩余高度
  </div>
</div>
英文:

Well you can use flex box to instane you expect.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

html,
body {
  height: 100%;
  width: 100%;
  margin: 0;
}

#main-container {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
}

#fixed-height {
  background-color: #ebcaca;
}

#remaining-height {
  background-color: #ebe6e6;
  overflow-y: auto;
  height: 100%;
}

<!-- language: lang-html -->

&lt;div id=&quot;main-container&quot;&gt;
  &lt;div id=&quot;fixed-height&quot;&gt;
    Fixed height
  &lt;/div&gt;
  &lt;div id=&quot;remaining-height&quot;&gt;
    Remaining height 1&lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining
    height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height &lt;br&gt; Remaining height
    &lt;br&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月13日 15:31:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定