The "footer" part of the Web page floats up and is in the middle of the page. It has a great impact on the visual effect and makes your page look ugly, especially now that widescreens are becoming more and more popular. This phenomenon is even more common. This article will introduce two solutions. Friends who need to know more can refer to
As a pager, you must have encountered: When an HTML page contains less content At this time, the "footer" part of the Web page floats up and is in the middle of the page, which has a great impact on the visual effect and makes your page look ugly, especially now that there are more and more widescreens. , this phenomenon is even more common. So how to fix the "footer" part of a Web page to the bottom of the page forever? Let’s take a look at the code below first
This is the first solution, there are several more
HTML codes
The code is as follows:
<p class="container"> <p class="header">这是头部</p> <p class="page clearfix"> <p class="left">left sidebar</p> <p class="content">main content</p> <p class="right">right sudebar</p> </p> <p class="footer">footer section</p> </p>
CSS code
The code is as follows:
html,body{margin:0;padding:0;height:100%} .container{min-height:100%;height:auto !important;height:100%;/*ie6不识别min-height,如上述处理*/position:relative;} .header{background:#ff0;padding:10px;} .page{width:960px;margin:0 auto; padding-bottom :60px;/*padding等于footer的高度*/} .footer{position:absolute;bottom:0;width:100%;height:60px;/*footer的高度*/background:#6cf;clear:both;} .left{width:220px;height:800px;float:left; margin-right :20px;background:lime;} .content{background:orange;width:480px;float:left;margin-right:20px;} .right{width:220px;float:right;background:green;} .clearfix:after, .clearfix:before{content:"";display:table} .clearfix:after{clear:both;overflow:hidden} .clearfix{zoom:1;}
To achieve this footer is always fixed at the bottom of the page, we only need four p, where p#container is a container, in this container Among them, we include p#header (header), p#page (main part of the page, we can add more p structures to this p, as shown in the code above), p#footer (footer part )
Let’s take a look at the implementation principle of this method:
and
The code is as follows:
<p id="header">header</p> <p id="page" class="clearfix"> <p id="left">left sidebar</p> <p id="content">main content</p> <p id="right">right sidebar</p> </p> </p> <p id="footer">footer</p>
CSS code
The code is as follows:
html,body{height:100%;margin:0;padding:0;} #container{min-height:100%;height:auto !important;height:100%;} #page{padding-bottom:60px;/*等于或者大于footer的高度*//*border-bottom-width:60px;边框宽度也可以*/} #header{padding:10px;background:lime;} #footer{position:relative;margin-top:-60px;/*等于footer的高度*/height:60px;clear:both;background:#c6f;} #left{width:18%;float:left;margin-right:2%;background:orange;} #content{width:60%;float:left;margin-right:2%;background:yellow;} #right{width:18%;float:right;background:green;} .clearfix:after{ visibility :hidden;height:0;font-size:0;display:block;content:" ";clear:both;} * html .clearfix{zoom:1;}/* ie6 */ * :first-child +html .clearfix{zoom:1;} /* ie7 */
上面的代码是最基本的HTML Code,同时你也发现了p#footer和p#container是同辈关系,不像方法一,p#footer在p#container容器内部。当然你也可以根据你的需要把内容增加在p#container容器中,如:一个三列布局,而且还带有header部分。
方法一和方法二有几点是完全相同的,比如说方法一中的1-3三点,在方法二中都一样,换句话说,方法二中也需要把html,body高度设置为100%,并重置margin,padding为0;其二p#container也需要设置min-height:100%,并处理好IE6下的min-height兼容问题;其三也需要在p#page容器上设置一个padding-bottom或border-bottom-width值等于p#footer高度值(或略大于)。那么两种方法不同之处是:
p#footer放在p#container容器外面,也就是说两者是同级关系,如果你有新元素需要放置在与p#container容器同级,那你需要将此元素进行绝对定位,不然将会破坏p#container容器的min-height值;
p#footer进行margin-top的负值设置,并且此值等于p#footer的高度值,而且也要和p#page容器的padding-bottom(或border-bottom-width)值相等。
优点:
结构简单清晰,无需js和任何hack能实现各浏览器下的兼容。
缺点:
要给footer设置固定值,因此无法让footer部分自适应高度。
The above is the detailed content of The css implementation of the div footer tag is fixed at the bottom of the page. For more information, please follow other related articles on the PHP Chinese website!