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

Jun 27, 2017 am 09:51 AM
css accomplish Label 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>
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!

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 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.

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

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

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

See all articles