html - 为什么在删除其父包装元素的属性 "position:relative"时页脚元素位置错误

标签 html css

我尝试制作一个底部始终带有页脚元素的 html 页面,我遵循了这个 article一切都很完美。

但我发现,如果我从 CSS 类“wrapper”中删除属性“position:relative”,页脚的位置就会错误,如下图所示。

因为我对CSS不是很熟悉,我想知道为什么在这种情况下需要“position:relative”。

source - JSFiddle

CSS:

html,
body {
	margin:0;
	padding:0;
	height:100%;
}
#wrapper {
	min-height:100%;
    position:relative;
}
#header {
	background:#ededed;
	padding:10px;
}
#content {
	padding-bottom:100px; /* Height of the footer element */
}
#footer {
	background:#ffab62;
	width:100%;
	height:100px;
	position:absolute;
	bottom:0;
	left:0;
}
<div id="wrapper">
		<div id="header">
		</div><!-- #header -->
		<div id="content">
            <script>
                for (var i=0; i<=100; i++) {
                    document.write("<p>line " + i + "</p>");
                }
            </script>
		</div><!-- #content -->
		<div id="footer">
		</div><!-- #footer -->
</div><!-- #wrapper -->

The position of footer is in the middle of the page rather than bottom

最佳答案

因为属性position#footerabsolute .

这意味着,相对于将具有相对位置的第一个父级(在本例中为 <div id="wrapper">),它将具有绝对位置

如果没有 parent 拥有这个属性,它将是关于 body 的绝对值

关于html - 为什么在删除其父包装元素的属性 "position:relative"时页脚元素位置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33188669/

相关文章:

javascript - 使用单选按钮更改图像

javascript - 在 jQuery 中单击“提交”按钮更新全局计数器变量

html - 我的下拉菜单圆形底部出现问题

javascript - Ajax 不工作。我需要页眉和页脚不要每次都重新加载。你能帮助我吗?

html - 父DIV高度:0px but child element still shows

javascript - 使用我的静态页面上线后,图像消失或显示质量不佳

javascript - 如何在 jQuery 中用开始和结束元素包装元素?

html - 如何在不弄乱边框的情况下将 <div> 保留在页眉内?

php - Seo Friendly URL 导致 CSS IMG 和 JS 不工作

javascript - jQuery - 选择选项更改时,显示/隐藏 DIV