How to enforce the height of inner div to be equal to the height of the parent div, if the parent div has "min-height"?
Asked Answered
F

5

13

Why in the following example the height of the inner div is not like wrapper's div ?

Live demo here.

HTML:

<div class="wrapper">
    <div class="inner">Hello</div>
    <div class="inner">Peace</div>
</div>

CSS:

.wrapper {
    background-color: #000;
    min-height: 100px;
}
.inner {
    display: inline-block;
    background-color: #777;
    height: 100%;
}

If I change min-height: 100px; to height: 100px;, then it looks OK. But, in my case, I need min-height.

Floatage answered 4/5, 2011 at 6:59 Comment(4)
If the height of the wrapper isn't dynamic, then why do you need to use % when the inner could just have the same height that you specified for wrapper. Also, if inner was 100% high then how would having two of them within wrapper work..?Snath
thanks for that site btw Misha, it will be very helpful :)Mothball
@iain: Welcome to the club :)Floatage
@Marty: The height of the wrapper is dynamic, it can be 100px or more.Floatage
R
8

I believe this is the output you want: http://jsfiddle.net/xhp7x/

.wrapper {
    display: table;
    background-color: #000;
    height: 100px;
    width: 100%;
}
.wrapper2 {
    height: 100%;
    display: table-row
}
.inner {
    height: 100%;
    display: inline-block;
    background-color: #777;
    margin-right: 10px;
    vertical-align: top;
}

Had to add a second DIV wrapper2.

Tested on chrome and firefox.

Rumilly answered 4/5, 2011 at 7:1 Comment(5)
In your example the wrapper does not has min-height.Floatage
but it behave like so. the short text keep the wrapper at 100px, and the tall text extend itRumilly
OK... But the height of the inner wrappers is less than the height of the wrapper. I want them to be equal.Floatage
Edited. Sorry, was working on chrome. Now is working on firefox. Cant make it work in IE though... will keep tryingRumilly
hi.. it's impossible to work in IE, unless you specify the height of the containing div. sorry.Rumilly
C
10

Some properties in CSS inherit the value of the parent automatically, some don't. Minimum height must be explicitly stated when you want it to inherit the parent's value:

min-height: inherit;
Calix answered 30/3, 2012 at 9:11 Comment(0)
R
8

I believe this is the output you want: http://jsfiddle.net/xhp7x/

.wrapper {
    display: table;
    background-color: #000;
    height: 100px;
    width: 100%;
}
.wrapper2 {
    height: 100%;
    display: table-row
}
.inner {
    height: 100%;
    display: inline-block;
    background-color: #777;
    margin-right: 10px;
    vertical-align: top;
}

Had to add a second DIV wrapper2.

Tested on chrome and firefox.

Rumilly answered 4/5, 2011 at 7:1 Comment(5)
In your example the wrapper does not has min-height.Floatage
but it behave like so. the short text keep the wrapper at 100px, and the tall text extend itRumilly
OK... But the height of the inner wrappers is less than the height of the wrapper. I want them to be equal.Floatage
Edited. Sorry, was working on chrome. Now is working on firefox. Cant make it work in IE though... will keep tryingRumilly
hi.. it's impossible to work in IE, unless you specify the height of the containing div. sorry.Rumilly
M
1

You want to specify both, CSS height is not the same as min-height. You want to specify both height and min-height.

  • height = When used as a %, this is a percent of the window height

  • min-height = as you drag the window smaller, the DIV with a % height will continue to reduce until it hits the min-height

  • max-height = as you drag the window larger, the DIV with a % height will continue to increase until it hits the max-height

http://jsfiddle.net/gpeKW/2/ I've added a sample here with borders.

Slight change to the answer from your comment, you are pretty much correct from your original CSS.

The below HTML will have a minimum div height of 100px. As the size of the inner DIV increases, the wrapper will automatically expand. I have demonstrated this by adding a style attribute to the first inner class.

<html>
<head>
    <title>Untitled Page</title>
    <style type="text/css">
        .wrapper
        {
            background-color: #000;
            min-height:100px;
        }
        .inner
        {
            display: inline-block;
            background-color: #777;
            height: 100%;
        }
    </style>
</head>
<body>
    <div class="wrapper">
         <div class="inner" style="height:200px">test</div>
    <div class="inner">Peace</div>
    </div>
</body>
</html>
Mothball answered 4/5, 2011 at 7:5 Comment(4)
i tried that but if you put height the div dont grow if the content if larger than 100px, i guess that's a prerequisiteRumilly
In your example there is no min-height on the wrapper div.Floatage
@misha, i updated it, sorry i had trouble with the copy and paste :)Mothball
The problem is that I don't want to define wrapper's height. I want it to be 100px or more (in case case the inner div is too tall).Floatage
C
0

I know one way to set the div child height the same as its parent div height is to use relative for the parent and absolute position for the child.

.wrapper {
    background-color: #000;
    min-height: 100px;
    position: relative;
}
.inner {
    display: inline-block;
    background-color: #777;
    position: absolute;
    height: 100%;
}

But this way will cause some problem, you have to adjust the child element so that it will be displayed properly

P/s: Why don't you set it to the same height as its parent height? I mean, 100% is not x%... just thinking..

Anyway, happy coding ;)

Coldiron answered 4/5, 2011 at 7:19 Comment(2)
hm.. weird, it didn't expand the height for a high element jsfiddle.net/kBRVHRumilly
jsfiddle.net/kBRVH/3 as you can see, the first one with text in black, if you insert more lines, it will overflow the outside div, because the outside div is 100px high and this height will not increase when its child with absolute position increases height. On the other hand, the second one with white text, insert more lines and you will see ;)Coldiron
J
0

I certainly joined answers and the result using 'min-height' for the -main HTML tag- (class = "main-page-container"):

HTML:

<div id="divMainContent">
    <!-- before or after you can use multiples divs or containers HTML elements-->
    <div>   </div>
    <div>   </div>
    
    <main class="main-page-container">
        <div class="wrapper">
        1
            <div class="wrapper2">
            2
                <div class="child">3</div>
            </div>
        </div>
    </main>
    
    <!-- before or after you can use multiples divs or containers HTML elements-->
    <div class="footer-page-container bg-danger" > more relevant info</div>
</div>

CSS:

/*#region ---- app component containers ---- */
#divMainContent {
  height: 100%;
  display: flex;
  flex-direction: column;

  /*optional: max width for screens with high resolution*/
  max-width: 1280px;
  margin:0 auto;
}
.main-page-container {
  display: inline-table;
  height: 70%;
  min-height: 70%;
  width: 100%;
}
.footer-page-container{
  flex:1; /* important in order to cover the rest of height */

  /* this is just  for your internal html tags
  display: flex;
  flex-direction: column;
  justify-content: space-between; */
}

/*#endregion ---- app component containers ---- */


.wrapper {
  background: blue;
  max-width: 1280px;

  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;

  height: 100%;
}

.wrapper2 {
  width: 90%;
  margin: auto;
  background: pink;
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  height: 90%;
}

.child {
  min-height: 100px;
  min-width: 300px;
  background: orange;
  position: relative;
  width: 33%;
}
Jinny answered 5/11, 2022 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.