18

我在这里是因为其他类似的问题对我的特定问题没有帮助。

我需要right div一直保持 100% 的高度,parent高度取决于left div高度,高度取决于内部的内容。

这是html:

<div class="container clearfix">
<div class="left"></div>
<div class="right"></div>
</div>

这是CSS:

.container{
    min-height: 10px;
    width: auto;
    height: auto;
    background-color: #eeeeee;
}

.left{
    position: relative;
    float: left;
    min-height: 100px;
    width: 50px;
    background-color: #dddddd;
}

.right{   
    min-height: 20px;
    position: relative;
    float: left;
    height: 100%;
    width: 50px;
    background-color: #dddddd;
}

.

.clearfix:after
{
    content: ".";
    display: block;
    clear: both;
    visibility: hidden;
    line-height: 0;
    height: 0;
}
.clearfix {
    display: inline-block;
}

​注:
我正在使用clearfix.
如果你能在jsfiddle

这是 jsFiddle http://jsfiddle.net/C9Kmx/32/

4

7 回答 7

15

制作正确的 divposition:absolute;并制作父 div position:relative;,然后height:100%;将适用于正确的 div。确保您还相应地调整了它的 x 位置和宽度。在这个例子中,我给它一个left:50px以确保它出现在左列的右侧。

JSFiddle http://jsfiddle.net/e9mvD/

于 2012-07-10T08:46:37.233 回答
5

您可以使用 flexbox 和一些创意来完成此操作。

.container {
  display: flex;
  background: black;
  padding: 5px;
  width: 300px
}

.left {
  height: 200px;
  flex: 1 0 0px;
  background: blue;
}

.right {
  flex: 1 0 0px;
  overflow: auto;
  background: green;
}

.column {
  display: flex;
  flex-direction: column;
  width: 20%;
}
<div class="container">
  <div class="left"></div>
  <div class="column">
    <div class="right"></div>
  </div>
</div>

于 2016-12-28T22:38:52.220 回答
4

您可以在此布局中使用table-cell该属性的值并删除以下内容:displayfloat

.left, .right{
    display:table-cell;
}

现场演示http://jsfiddle.net/C9Kmx/34/

于 2012-07-10T08:48:09.467 回答
1

position:fixedheight:100%正确的 div。这将解决您的问题。

于 2012-07-10T08:46:11.493 回答
1

通过阅读您对其他解决方案的评论,我很清楚,您唯一的解决方案就是在您的代码中实现一些 JS。然而,这不是问题。

http://jsfiddle.net/b7TuP/

于 2012-07-14T15:47:11.563 回答
0

.parent{
    position: relative;
    width: 100px;
    height: auto;
}
.child{
    width: 50px;
    height: 100px;
    background-color: red;
}
.child_absolute{
    position: absolute;
    left: 50px;
    top: 0;
    bottom: 0;
    width: 50px;
    background-color: blue;
}
<div class="parent">
    <div class="child"></div>
    <div class="child_absolute"></div>
</div>

于 2020-09-01T15:17:45.297 回答
-1

尝试给容器的高度一个固定值

这也将解决问题。刚刚在 jsFiddle 中尝试过

http://jsfiddle.net/C9Kmx/35/

.container
{
    min-height: 10px;
    width: auto;
    height: 100px;
    background-color: #eeeeee;
}
于 2012-07-10T08:55:42.950 回答