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
HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

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.