search
HomeWeb Front-endHTML TutorialThe css implementation of the div footer tag is fixed at the bottom of the page

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

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>

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!

Statement
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
利用CSS怎么创建渐变色边框?5种方法分享利用CSS怎么创建渐变色边框?5种方法分享Oct 13, 2021 am 10:19 AM

利用CSS怎么创建渐变色边框?下面本篇文章给大家分享CSS实现渐变色边框的5种方法,希望对大家有所帮助!

css ul标签怎么去掉圆点css ul标签怎么去掉圆点Apr 25, 2022 pm 05:55 PM

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

css与xml的区别是什么css与xml的区别是什么Apr 24, 2022 am 11:21 AM

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

css3怎么实现鼠标隐藏效果css3怎么实现鼠标隐藏效果Apr 27, 2022 pm 05:20 PM

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

css怎么实现英文小写转为大写css怎么实现英文小写转为大写Apr 25, 2022 pm 06:35 PM

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

rtl在css是什么意思rtl在css是什么意思Apr 24, 2022 am 11:07 AM

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

css怎么设置i不是斜体css怎么设置i不是斜体Apr 20, 2022 am 10:36 AM

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

怎么设置rotate在css3的旋转中心点怎么设置rotate在css3的旋转中心点Apr 24, 2022 am 10:50 AM

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools