Child div 100% height to parent auto height

2020-07-02 12:25发布

Im here because other similar questions couldn't help my particular problem.

I need right div to be 100% height all the time, where the parent height depends on left div height which depends on content inside.

Here is the html:

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

​Here is 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;
}

​Note:
I'm using clearfix.
And if you can show your answer in jsfiddle

Here is jsFiddle http://jsfiddle.net/C9Kmx/32/

标签: html css
6条回答
混吃等死
2楼-- · 2020-07-02 12:33

Try by giving the height of container a fixed value

this will also fix the issue. Just tried it in jsFiddle

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

.container
{
    min-height: 10px;
    width: auto;
    height: 100px;
    background-color: #eeeeee;
}
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-07-02 12:38

By reading your comments on other solutions it is clear to me that only solution for you is to implement some JS into your code. This is not a problem, however.

http://jsfiddle.net/b7TuP/

查看更多
闹够了就滚
4楼-- · 2020-07-02 12:39

You can use the table-cell value of the display property in this layout and remove the float like this:

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

live demo http://jsfiddle.net/C9Kmx/34/

查看更多
做个烂人
5楼-- · 2020-07-02 12:47

Make the right div position:absolute; and make the parent div position:relative; and then height:100%; will work for the right div. Make sure you also adjust its x-position and width accordingly. In this example I gave it a left:50px to make sure it appears to the right of the left column.

JSFiddle http://jsfiddle.net/e9mvD/

查看更多
你好瞎i
6楼-- · 2020-07-02 12:55

You can accomplish this using flexbox and some creativity.

.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>

查看更多
女痞
7楼-- · 2020-07-02 12:58

Give position:fixed and height:100% for the right div. This will solve your issue.

查看更多
登录 后发表回答