html - 父 div 高度不适用于子 div

标签 html css styles height

我创建了一个 html 文件,它有一个名为 parent-portlet 的父 div。 parent-portlet 类将 height 设置为 40%。 html 重新渲染正确,但问题是子 div (child-fluid) 高度未应用于父 div (parent-portlet) 的高度。

谁能告诉我一些解决办法

我的代码如下

Plunker

HTML

  <div class="parent-portlet">
    <div class="child-fluid">
      <div class="box">
        <div class="box-header well">
          <h2><i class="icon-bullhorn"></i> Alerts</h2>
        </div>
        <div class="portlet-content box-content alerts">
          <div class="alert alert-error">
            <strong>Oh snap!</strong> Change a few things up and try submitting again.
          </div>
          <div class="alert alert-success">
            <strong>Well done!</strong> You successfully read this important alert message.
          </div>
          <div class="alert alert-info">
            <strong>Heads up!</strong> This alert needs your attention, but it's not super important.
          </div>
          <div class="alert alert-block ">
            <h4 class="alert-heading">Warning!</h4>
            <p>Best check yo self, you're not looking too good. Nulla vitae elit libero, a pharetra augue. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.</p>
          </div>
        </div>
      </div>
    </div>
  </div>

CSS

.parent-portlet { 
  height: 40%;
}

.portlet-content {
  overflow-y: auto;
}

.child-fluid {
  height: inherit;
}

最佳答案

.parent-portlet 继承了其没有高度的父级的 40%。所以你可以给htmlbody height:100% 然后设置overflow-y: auto; on .parent-portlet

Plunker

默认情况下,一个元素具有 height:auto;,如果您没有为它指定高度,它就不会从其父级继承 height。此外,根据w3c :

Height : Percentage Value

The percentage was always calculated with respect to the content box of the parent element.

评论后编辑:

您已将 height: 100%;overflow-y: auto; 赋给 .child-fluid

Plunker

OP愿望情况:

I need to have a fixed .box-header on top of .box and scrolling .box-content.

Plunker

关于html - 父 div 高度不适用于子 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34236794/

相关文章:

jquery - 最高元素后的尺寸列表

javascript - 使用 Angular 在鼠标悬停时显示兄弟元素

c# - 在不丢失默认样式的情况下更改 DataGridView 上的 RowHeader 背景颜色

html - 防止 float 元素换行

javascript - 在 Javascript 中向字符串添加格式方法。

html - 在 R 中将 HTML/CSS 转换为图像(png 或任何无损格式)

html - 使用 z-index 使 div 高于另一个 div

wpf - "{StaticResource {dxgt:GridRowThemeKey ResourceKey=RowStyle}}"的隐藏代码是什么

css - 智能 gwt 样式属性

java - 如何从不同级别的 div 定义 xpath