search
HomeWeb Front-endHTML Tutorial将footer固定在页面底部的实现方法_html/css_WEB-ITnose

方法一:footer高度固定+绝对定位

HTML结构:

<body>    <header>header</header>    <main>main content</main>    <footer>footer</footer></body>

CSS设置:

html{height:100%;}body{min-height:100%;margin:0;padding:0;position:relative;}header{background-color: #ffe4c4;}main{padding-bottom:100px;background-color: #bdb76b;}/* main的padding-bottom值要等于或大于footer的height值 */footer{position:absolute;bottom:0;width:100%;height:100px;background-color: #ffc0cb;}

首先,设置body的高度至少充满整个屏幕,并且body作为footer绝对定位的参考节点;

其次,设置main(footer前一个兄弟元素)的padding-bottom值大于等于footer的height值,以保证main的内容能够全部显示出来而不被footer遮盖;

最后,设置footer绝对定位,并 设置height为固定高度值。

方法二:footer高度固定+margin负值

HTML结构:

<body>    <div class="container">        <header>header</header>        <main>main content</main>    </div>    <footer>footer</footer></body>

CSS设置:

html,body{height:100%;margin:0;padding:0;}.container{min-height:100%;}header{background-color: #ffe4c4;}main{padding-bottom:100px;background-color: #bdb76b;}/* main的padding-bottom值要等于或大于footer的height值 */footer{height:100px;margin-top:-100px;background-color: #ffc0cb;}/* margin-top(负值的)高度等于footer的height值 */

此方法把footer之前的元素放在一个容器里面,形成了container和footer并列的结构:

首先,设置.container的高度至少充满整个屏幕;

其次,设置main(.container最后一个子元素)的padding-bottom值大于等于footer的height值;

最后,设置footer的height值和 margin-top负值。

这种方法没有使用绝对定位,但html结构的语义化不如方法一中的结构清晰。

也可以设置负值的margin-bottom在.container上面,此时html结构变化如下:

<body>    <div class="container">        <header>header</header>        <main>main content</main>        <div class="empty"></div>    </div>    <footer>footer</footer></body>

CSS设置:

html,body{height:100%;margin:0;padding:0;}.container{min-height:100%;margin-bottom:-100px;}.empty,footer{height:100px;}

使用一个空的div把footer容器推到页面最底部,同时给container设置一个负值的margin-bottom,这个margin-bottom与footer和.empty的高度相等。

虽然多了一个空的div,语义上也不怎么好,但是相比前面为main元素设置padding-bottom的方法有一个明显的好处:当网页内容不满一屏的时候,如果需要为main元素设置边框、背景色的时候,padding-bottom硬生生地撑出了一片空白,真真是有点丑,但是加个空的div之后,布局方式作用在.empty上,对main的css设置就不影响了,算是一个好处吧!

方法三:footer高度任意+js

HTML结构:

<body>    <header>header</header>    <main>main content</main>    <footer>footer</footer></body>

CSS设置:

html,body{margin:0;padding: 0;}header{background-color: #ffe4c4;}main{background-color: #bdb76b;}footer{background-color: #ffc0cb;}/* 动态为footer添加类fixed-bottom */.fixed-bottom {position: fixed;bottom: 0;width:100%;}

js代码:

$(function(){    function footerPosition(){        $("footer").removeClass("fixed-bottom");        var contentHeight = document.body.scrollHeight,//网页正文全文高度            winHeight = window.innerHeight;//可视窗口高度,不包括浏览器顶部工具栏        if(!(contentHeight > winHeight)){            //当网页正文高度小于可视窗口高度时,为footer添加类fixed-bottom            $("footer").addClass("fixed-bottom");        } else {            $("footer").removeClass("fixed-bottom");        }    }    footerPosition();    $(window).resize(footerPosition);});
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: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

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.

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor