繁体   English   中英

如何使子div占据父div高度

[英]how to make child div occupy the parent div height

我想知道子div占据父div高度的可能性。

下面的代码是我的html:

<div class="container">
        <div class="header width100p">
            <h2>
                Header
            </h2>
        </div>
        <div class="content width100p">
            <div class="width29p npv">
                <div class="width100p inner">

                        <p>
                            navigation
                        </p>
                        <p>
                            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s
                        </p>

                </div>

            </div>
            <div class="rtb">
                <div class="width100p ql">
                    <p>
                        div one
                    </p>
                    <p>
                        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s
                    </p>
                </div>
                <div class="width100p mtbs">
                    <p>
                        div two
                    </p>
                    <p>
                        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s
                    </p>
                </div>
                <div class="floatL width100p widdiv">
                    <div class="floatL width100p">
                        <div class="floatL width40p incont">
                            <p>
                            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the     industry's standard dummy text ever since the 1500s
                            </p>
                            <p>
                            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the     industry's standard dummy text ever since the 1500s
                            </p>
                            <p>
                            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the     industry's standard dummy text ever since the 1500s
                            </p>
                            <p>
                            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the     industry's standard dummy text ever since the 1500s
                            </p>
                        </div>
                        <div class="floatL width40p incont">
                            <p>
                            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the     industry's standard dummy text ever since the 1500s
                            </p>
                        </div>
                    </div>
                    <div class="floatL width100p">
                        <div class="floatL width40p incont">
                            <p>
                            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the     industry's standard dummy text ever since the 1500s
                            </p>
                        </div>

                        <div class="floatL width40p incont">
                            <p>
                            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the     industry's standard dummy text ever since the 1500s
                            </p>
                            <p>
                            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the     industry's standard dummy text ever since the 1500s
                            </p>
                            <p>
                            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the     industry's standard dummy text ever since the 1500s
                            </p>
                            <p>
                            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the     industry's standard dummy text ever since the 1500s
                            </p>
                        </div>

                    </div>
                </div>
                <div class="clear"></div>
            </div>


        </div>
        <div class="footer width100p">
            <h2>
                Footer
            </h2>
        </div>

    </div>

各自的样式是:

<style>
        *,html{
            margin: 0;
            padding: 0;
        }
        html{
            height: 100%;
        }
        .container{
            width:960px;
            margin:20px auto;
        }
        .header h2,.footer h2{
            text-align: center;
        }
        .floatL{
            float: left;
        }
        .floatR{
            float: right;
        }
        .clear{
            clear:both;
        }
        .width100p{
            width: 100%;
        }
        .width29p{
            width: 29%;
        }
        .width70p{
            width: 70.8%;

        }
        .header,.footer,.content{
            border:1px solid #000;
        }
        .npv{
            border-right: 1px solid #000;


        }
        .ql,.mtbs{
            border-bottom: 1px solid #000;
        }
        .content{
            display: table;


        }
        .npv, .rtb{
            display: table-cell; 



        }
        .width40p{
            width: 40%;
        }

        .incont{
            margin: 4%;
            background: #ccc;
            border:1px solid red;

        }

    </style>

我想知道(.inner)子div如何占据其父div(.npv)的高度。 我已经尝试对.inner和.npv应用height:100%!important,但是仍然不能解决child div占用父div高度的问题。 占用问题

标记

<html>
  <body>
    <div class="outer">
      <div class="inner">
    </div>
  </body>
</html>

的CSS

html, body {
  height: 100%;
width: 100%;
}
    .outer {
       height: 100%; // or set height and width in pixels
       width: 100%;
    }

    .outer .inner {
       height: 100%;
       width: 100%;
    }

为了使百分比高度起作用,

您需要设置其父级中每个父级的高度。

包括html和body标签。

将高度和宽度设置为100%:

.width100p{
   height: 100%; // or set height and width in pixels
   width: 100%;
}

.width100p .inner {
   height: 100%;
   width: 100%;
}

或在html中指定尺寸,并将其显示为块:

.width100p {
  display:block;
.width100p .inner {
  display:block;
}

在“ 将nested(child)div的高度增加到其父div的高度 ”上有一个类似的示例,您可以将其用作参考。

建议:
-对于父级div设置位置:绝对
-对于子div设置位置:绝对; 高度:100%;
要么
位置:绝对的; 顶:0; 底部:0;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM