CSS页脚理论
我知道有1001个CSS页脚黑客需要多次修改才能满足最老的浏览器 但我想知道如何为符合现代标准的浏览器制作页脚。我希望下面的代码能够正常工作,但事实并非如此——页脚不在页面底部,而是在“内容”的正下方。为什么?CSS页脚理论,css,browser,footer,standards-compliance,Css,Browser,Footer,Standards Compliance,我知道有1001个CSS页脚黑客需要多次修改才能满足最老的浏览器 但我想知道如何为符合现代标准的浏览器制作页脚。我希望下面的代码能够正常工作,但事实并非如此——页脚不在页面底部,而是在“内容”的正下方。为什么? 页脚测试 html { 身高:100%; 保证金:0; 填充:0; } 身体 { 身高:100%; 最小高度:100%; 保证金:0; 填充:0; } #页面 { 位置:相对;/*#页脚 { 位置:固定; 宽度:100%; 底部:0;/*我仍然不确定为什么原始版本不起作用 但是下面的代
页脚测试
html
{
身高:100%;
保证金:0;
填充:0;
}
身体
{
身高:100%;
最小高度:100%;
保证金:0;
填充:0;
}
#页面
{
位置:相对;/*#页脚
{
位置:固定;
宽度:100%;
底部:0;/*我仍然不确定为什么原始版本不起作用
但是下面的代码是无黑客攻击的,似乎可以工作:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Footer Test</title>
<style type="text/css">
html
{
height: 100%;
margin: 0;
padding: 0;
}
body
{
margin: 0;
padding: 0;
}
#page
{
min-height: 100%;
width: 100%;
position: absolute;
background-color: #AAA;
}
#header
{
padding: 1em;
background-color: #F00;
}
#content
{
margin-bottom: 5em; /* <--- footer's height */
padding: 1em;
background-color: #0F0;
}
#footer
{
width: 100%;
position: absolute;
bottom: 0;
background-color: #00F;
}
#footer > *
{
padding: 1em; /* <--- additional rule for footer's content */
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script type="text/javascript">
$(document).ready(function() { $("button").click(function() { $("#hidden").slideToggle(1000); }) });
</script>
</head>
<body>
<div id="page">
<div id="header">
<p>This is the header!</p>
</div>
<div id="content">
<p>Yeah, some content!</p>
<p>Yeah, some content!</p>
<p>Yeah, some content!</p>
<button>Push the footer!</button>
<div id="hidden" style="display: none">
<p>Begin of extra content</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>Much hidden content!</p>
<p>End of extra content</p>
</div>
</div>
<div id="footer">
<p>This is the footer!</p>
</div>
</div>
</body>
</html>
页脚测试
html
{
身高:100%;
保证金:0;
填充:0;
}
身体
{
保证金:0;
填充:0;
}
#页面
{
最小高度:100%;
宽度:100%;
位置:绝对位置;
背景色:#AAA;
}
#标题
{
填充:1em;
背景色:#F00;
}
#内容
{
边距底部:5em;/*在这里您可以找到如下代码
将以下CSS行添加到样式表中。.wrapper中边距的负值与.footer和.push的高度相同。负边距应始终等于页脚的全高(包括您可能添加的任何填充或边框)
CSS中的:
* {
margin: 0;
}
html,body
{
height: 100%;
}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -4em;
}
.footer, .push {
height: 4em;
}
<div class="wrapper">
<p>Your website content here.</p>
<div class="push"></div>
</div>
<div class="footer">
<p>Copyright (c) 2013</p>
</div>
遵循此HTML结构。任何内容都不能位于.wrapper和.footer div标记之外,除非它完全使用CSS定位。在.push div中也不应该有任何内容,因为它是一个隐藏元素,可以向下“推”页脚,因此不会重叠任何内容
在HTML正文中:
* {
margin: 0;
}
html,body
{
height: 100%;
}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -4em;
}
.footer, .push {
height: 4em;
}
<div class="wrapper">
<p>Your website content here.</p>
<div class="push"></div>
</div>
<div class="footer">
<p>Copyright (c) 2013</p>
</div>
你的网站内容在这里
版权所有(c)2013
不幸的是,较长的内容不会将固定页脚向下推。位置:固定将始终相对于屏幕定位,因此无论内容如何,它都将定位在屏幕底部是的,但这不是我想要实现的页脚类型。我编辑了我的问题以澄清问题。顶部的CSS部分缺少某些内容