无法设置其父元素的最小高度设置为100%的元素的百分比高度

时间:2015-02-24 14:15:09

标签: html css

所以我做了一些研究,结果发现这个问题已经解决了几次。如果父元素的最小高度设置为百分比值,则无法以百分比设置高度,这听起来很合乎逻辑。

这个问题的答案是将父元素设置为固定的像素数。但是,这对我来说是一个问题,因为我希望页面的百分比值设置为高度。

我有一个标题和body元素,它们堆叠在父元素中,这样我就可以给父元素一个box-shadow。这样,阴影不会重叠,看起来很奇怪。所以我的解决方案是根本不设置父元素的高度,因此它是可变的,但是这样我就不能将子元素设置为百分比,因为,没有设置父元素高度。

为了使这一切更有意义,这里是HTML和CSS:

[HTML]

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/main.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script src="js/animateheader.js"></script>
    </head>

    <body>
        <div id="content_parent">
            <!-- Header -->
            <div id="header_parent">

            </div>

            <!-- Body (homepage) -->
            <div id="body_parent">

            </div>
        </div>

        <!-- Footer -->
        <div id="footer_parent">

        </div>

    </body>
</html>

[CSS]

html,body {
    height:100%;
}

* {
    margin:0 auto;
    padding:0;
}

#content_parent {
    min-height:100%;
    max-width:1250px;
    min-width:750px;
    box-shadow:0 0 10px 2px rgb(100,100,100);
}

#header_parent {
    position:fixed;
    right:0;
    left:0;
    margin-left:auto;
    margin-right:auto;
    max-width:1250px;
    min-width:750px;
    height:50px;
    background-color:rgb(50,50,50);
    box-shadow:-6px 0 rgba(0,0,0,0), 6px 0 rgba(0,0,0,0), 0 6px 4px -2px rgb(100,100,100);
    -webkit-box-shadow:-6px 0 rgba(0,0,0,0), 6px 0 rgba(0,0,0,0), 0 6px 4px -2px rgb(100,100,100);
    -moz-box-shadow:-6px 0 rgba(0,0,0,0), 6px 0 rgba(0,0,0,0), 0 6px 4px -2px rgb(100,100,100);
    border-bottom-left-radius:2px;
    -webkit-border-bottom-left-radius:2px;
    -moz-border-bottom-left-radius:2px;
    border-bottom-right-radius:2px;
    -webkit-border-bottom-right-radius:2px;
    -moz-border-bottom-right-radius:2px;
    border-top-left-radius:0;
    -webkit-border-top-left-radius:0;
    -moz-border-top-left-radius:0;
    border-top-right-radius:0;
    -webkit-border-top-right-radius:0;
    -moz-border-top-right-radius:0;
}

#body_parent {
    height:100%;
    max-width:1250px;
    min-width:750px;
    background-color:rgb(245,245,245);
}

此外,还有Fiddle

为了清楚起见,我在设置#body_parent的高度时遇到了问题,而父元素是#content_parent

有什么方法可以达到我的目的吗?

1 个答案:

答案 0 :(得分:3)

add following css

#content_parent {
    min-height:100%;
    max-width:1250px;
    min-width:750px;
    box-shadow:0 0 10px 2px rgb(100, 100, 100);
    height:100%; /* add this */
}

Demo

评论后编辑

height: 100%

中删除body, html{}

Demo