div with min-height and the child div have the hei

2019-06-27 12:53发布

问题:

Div with min-height and the child div have the height of parent.

Is it possible with only CSS or javascript is needed?

Example:

#main {
    min-height: 300px;
    width: 100%;
}

#menu, #content {
    float: left;
    width: 50%;
}

#menu {
    height: 150px;
    background-color: red;
}

#content {
    height: 100%;
    background-color: green;
}


<div id="main">
            <div id="menu">menu</div>
            <div id="content">content</div>
<div>

回答1:

may be you can give position:absolute to your content div to make it's equal height of it's parent

css:

#main {
    min-height: 300px;
    width: 100%;
    position:relative;
    background-color: yellow;
}

#menu, #content {
    float: left;
    width: 50%;
    overflow:hidden;
}

#menu {
    height: 150px;
    background-color: red;
}
#content {
    height: 100%;
    background-color: green;
    position:absolute;
    top:0;
    right:0;
}

check this http://jsfiddle.net/sandeep/hKttB/

EDIT there are other option also . You can give min-height to your content div also if the content increase the height of the main div also increase.

#content {
    height: 100%;
    min-height:300px;
    background-color: green;
}

check this fiddle http://jsfiddle.net/sandeep/SRJrF/2/



回答2:

I would try this:

In this case I forced same min height for the child div when there is less content. If content exceeds, the automatic overflow.

#content {
    min-height: 300px;
    overflow:auto;
    background-color: green;
}

This is how we have content div in our nested master page. Hope same will work for you.

Thanks



回答3:

i think its not possible.

but you can try something like this. jsFiddle

#main {
    width: 100%;
    height:300px;
    min-height:300px;
}

#menu,
#content{
    float: left;
    display:inline;
    width: 50%;
    position:relative;
}

#menu {
    height: 150px;
    background-color: red;
}

#content {
    height: 100%;
    background-color: green;
}

also please read this min-height regarding min-height.



标签: css html height