Home > Web Front-end > HTML Tutorial > The css implementation of the div footer tag is fixed at the bottom of the page

The css implementation of the div footer tag is fixed at the bottom of the page

巴扎黑
Release: 2017-06-27 09:51:54
Original
2719 people have browsed it

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>
Copy after login

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;}
Copy after login


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 tags: The height (height) must be set to "100%" in the html and body tags, so We can set the percentage height on the container, and at the same time, we need to remove the margin and padding of html and body, that is, the margin and padding of html and body are both 0;

p#container container: p# The container container must set a minimum height (min-height) of 100%; this mainly allows it to maintain a height of 100% when there is little (or no content). However, min-height is not supported in IE6. So in order to be compatible with IE6, we need to do certain compatibility processing for min-height. For details, you can see the previous code. In addition, we also need to set a "position:relative" in the p#container container to facilitate the of the elements inside. After absolute positioning , the p#container container will not run;
p#page container: The p#page container has a very key setting. It is necessary to set a padding-bottom value on this container, and this value It should be equal to (or slightly larger than) the height (height) value of the footer p#footer. Of course, you can use border-bottom and water-width to replace padding-bottom in p#page, but there is one thing to note. Here you Never use margin-bottom instead of padding-bottom, otherwise the effect will not be achieved;

p#footer container: The p#footer container must set a fixed height, and the unit can be px (or em). p#footer also needs to be absolutely positioned and set bottom:0; let p#footer be fixed at the bottom of the container p#container, so that the effect we mentioned earlier can be achieved. When the content is only one point, p#footer is fixed at the bottom of the container p#container. The bottom of the screen (because p#container sets a min-height:100%), when the content height exceeds the height of the screen, p#footer is also fixed at the bottom of p#container, that is, fixed at the bottom of the page. You can also add a "width:100%" to p#footer to extend it across the entire page;
Other p: As for other containers, you can set them according to your own needs, for example The previous p#header, p#left, p#content, p#right, etc.
Advantages:
The structure is simple and clear, no js or any hack is needed to achieve compatibility under various browsers, and it is also suitable for iPhone.
Disadvantages:
The disadvantage is that you need to set a fixed height for the p#footer container. This height can be set according to your needs. The unit can be px or em, and It is also necessary to set the padding-bottom (or border-bottom-width) of the p#page container to a size equal to (or slightly larger than) the height of p#footer to operate normally.
Method 2:
This method uses the negative margin-top value of the footer to achieve the effect that the footer is always fixed at the bottom of the page. Let’s see how it is implemented in detail.
HTML code

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>
Copy after login

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 */
Copy after login

上面的代码是最基本的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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template