在不适用fixed的前提下,当内容较少时footer固定在页面底部

本文介绍了一种使用CSS实现网页中粘性页脚的方法。通过调整样式表中的特定属性,可以确保无论页面内容多少,页脚始终粘附在浏览器窗口底部。文章提供了具体的CSS代码示例及HTML结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用css,参考国外的一个解决方法:

http://ryanfait.com/resources/footer-stick-to-bottom-of-page/

How to use the CSS Sticky Footer on your website

Add the following lines of CSS to your stylesheet. The negative value for the margin in .wrapper is the same number as the height of .footer and .push. The negative margin should always equal to the full height of the footer (including any padding or borders you may add).

* {
    margin: 0;
}

html, body {
    height: 100%;
}

.wrapper {
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin: 0 auto -4em;
}

.footer, .push {
    height: 4em;
}

Follow this HTML structure. No content can be outside of the .wrapper and .footer div tags unless it is absolutely positioned with CSS. There should also be no content inside the .push div as it is a hidden element that "pushes" down the footer so it doesn't overlap anything.

<html>
    <head>
        <link rel="stylesheet" href="layout.css" ... />
    </head>
    <body>
        <div class="wrapper">
            <p>Your website content here.</p>
            <div class="push"></div>
        </div>

        <div class="footer">
            <p>Copyright (c) 2008</p>
        </div>

    </body>
</html>

  

  注意:

  1. 注意footer body 的border/padding值会影响显示;

  2. 当页面包含header时 .footer, .push 的设置会随之改变并不相等;

  

 

转载于:https://www.cnblogs.com/xx0405/p/5254986.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值