父换行div中的100%高度子div

时间:2013-10-16 19:58:30

标签: html css

我在这里,因为其他类似的问题无法解决我的特殊问题。

#right div高度如何制作100%? 只有css解决方案需要。谢谢。

http://jsfiddle.net/elturko/86nX9/

HTML

<div id="wrap">
 <div id="header"></div>
 <div id="left"></div>
 <div id="right"></div>
 <div id="footer"></div>
</div>

CSS

* {
    margin: 0;
}
html, body {
    height: 100%;
}
#wrap{
    min-height: 100%;
    height: auto !important;
    height: 100%;
    position: relative;
    background:#ddd
}
#header{
    height:104px;
    background:#d5a1b3;
}
#left{
    float:left;
    width:219px;
    background:#a2d025;
}
#right{
    min-height: 100%;
    height: auto !important;
    height: 100%;
    position: relative;
    overflow:hidden;
    background:#FFF;
    margin:0 15px;
    -webkit-border-radius: 7px;
    -moz-border-radius: 7px;
    border-radius: 7px;
    padding:14px;
}
#footer{ 
    clear:both;
    height:15px;
    background:#ed653a;
}

enter image description here

5 个答案:

答案 0 :(得分:6)

这是2个纯CSS解决方案

不固定任何高度(页眉/页脚)或宽度(左列)。

我实际上更喜欢第二种解决方案。 (即使他的浏览器支持较少)

1 - 使用CSS技巧

这是一个完全响应的设计,适用于所有浏览器(IE10,FF,Chrome,Safari,Opera,移动浏览器)

Working Fiddle

<强> HTML:

<div class="Container">
    <div class="Header">
    </div>
    <div class="HeightTaker">
        <div class="Wrapper Container Inverse">
            <div>
                <div class="Footer">
                </div>
            </div>
            <div class="HeightTaker">
                <div class="Wrapper">
                    <div class="LeftMenu">
                    </div>
                    <div class="Content">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<强> CSS:

*
{
    margin: 0;
    padding: 0;
}
html, body, .Container
{
    height: 100%;
}
    .Container:before
    {
        content: '';
        height: 100%;
        float: left;
    }
.HeightTaker
{
    position: relative;
    z-index: 1;
}
    .HeightTaker:after
    {
        content: '';
        clear: both;
        display: block;
    }
.Wrapper
{
    position: absolute;
    width: 100%;
    height: 100%;
}
.Inverse, .Inverse > *
{
    -moz-transform: rotateX(180deg);
    -ms-transform: rotateX(180deg);
    -o-transform: rotate(180deg);
    -webkit-transform: rotateX(180deg);
    transform: rotateX(180deg);
}
.LeftMenu
{
    height: 100%;
    float: left;
}
.Content
{
    overflow: auto;
    height: 100%;
}

/*For demonstration only*/
p
{    
    font-size: 1.3em;
}

.Important
{
    font-weight: bolder;
    color: white;
}

body > .Container
{
    text-align: center;
}

.Header
{
    background-color: #bf5b5b;
}
.LeftMenu
{
    background-color:  #bdbe4c;
}

.Content
{
    background-color: #90adc1;
}
.Footer
{
    background-color: #b5a8b7;
}

2 - 使用Flex

这种布局也可以使用flex实现,但目前的浏览器支持是纯粹的。 这是Working Fiddle 唯一 FF,Chrome,IE10。

HTML:(更简单)

<header>
</header>
<section class="Middle">
    <div class="LeftMenu">
    </div>
    <div class="Content">
    </div>
</section>
<footer>
</footer>

<强> CSS:

*
{
    margin: 0;
    padding: 0;
}
html, body
{
    height: 100%;
    text-align: center;
}

body
{
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;

    -webkit-flex-direction: column;
    -ms-flex-direction: column;
    flex-direction: column;
}

.Middle
{    
    -webkit-flex: 1 1 auto;
    -ms-flex: 1 1 auto;
    flex: 1 1 0;

    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;

    overflow: hidden;
}

.Content
{   
    -webkit-flex: 1 1 auto;
    -ms-flex: 1 1 auto;
    flex: 1 0 0;

    overflow: auto;
}

/*For demonstration only*/
p
{    
    font-size: 1.3em;
}

.Important
{
    font-weight: bolder;
    color: white;
}

header
{
    background-color: #bf5b5b;
}
.LeftMenu
{
    background-color:  #bdbe4c;
}

.Content
{
    background-color: #90adc1;
}
footer
{
    background-color: #b5a8b7;
}

答案 1 :(得分:1)

#right{}

中取出css
margin:0 15px;

这将使其宽100%。我对100%的身高感到有些困惑。你的意思是广泛吗?

答案 2 :(得分:0)

重要的是会覆盖其他CSS属性,而auto会使div尽可能大到适合内容。

height: auto !important; 
height: 100%;

答案 3 :(得分:0)

试试这个:

#right{
    min-height: 100%;
    height: auto !important;
    height: 100%;
    position: relative;
    overflow:hidden;
    background:#FFF;
    margin:0 15px;
    -webkit-border-radius: 7px;
    -moz-border-radius: 7px;
    border-radius: 7px;
    padding:14px;

    top: 0px;
    bottom: 0px;
}

top: 0px; bottom: 0px;添加到您的#right css

答案 4 :(得分:0)

我重新调整了你的html。这是你想要的结果吗?

jsfiddle

<div id="container">
<div id="wrap">
    <div id="header">
        header
    </div>

    <div id="body">
        <div id="left">
            left
        </div>

        <div id="right">
            right<br>
            right<br>
            right<br>
            right<br>
            right<br>
        </div>
    </div>

</div>
</div>

<div id="footer">
    <p>footer</p>
</div>

CSS

html, body {
    height: 100%;
    padding: 0px;
    margin: 0px;
}

#container {
    height: 100%;
    height: auto !important;
    min-height: 100%;
}

#wrap {
    overflow: auto;
    padding-bottom: 16px;
}

#header {
    height: 104px;
    background:#d5a1b3;
    border-bottom: 1px solid #000;
}

#body {
    overflow: hidden;
    height: 100%;
}

#right {
    margin: 0px 0px 0px 220px;
    background:#FFF;
}

#left {
    width: 219px;
    float: left;
    background:#a2d025;
}

#footer {
    background:#ed653a;
    border-top: 1px solid #000;
    position: relative;
    height: 20px;
    margin-top: -21px;
    clear: both;
}
#footer p {
    margin: 0px;
}
相关问题