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

标签 html css

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

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

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

为了使这一切更有意义,这里是 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 时遇到了问题

有什么方法可以实现我想要的吗?

最佳答案

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

评论后编辑

body, html{} 中移除 height: 100%

Demo

关于html - 无法设置其父元素的最小高度设置为 100% 的元素的百分比高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28698101/

相关文章:

javascript - 第一个单词用 <span> 换行,其余文本用另一个 <span> 换行,使用 Jquery 用 <br> 标记分隔

python - 允许python文件添加到不同的文件linux

CSS 仅在检查后工作

javascript - anchor 标记在列表项中不起作用

javascript - 表单在 IE 中不显示

javascript - HTML5 Canvas 中的 textAlign 属性在 IE 11 中无法正常工作

python - 使用 Python 2.7 解析 HTML - HTMLParser、SGMLParser 或 Beautiful Soup?

html - 在 IE7 中显示侧边栏导航的问题

html - 如何避免 CSS 影响动态添加的类

python - 媒体内容工作