CSS实现Sticky Footer的示例代码

yizhihongxing

当网页内容不足以占满整个页面时,如果页面的底部有一行始终停留在页面的底部,这种效果称为"Sticky Footer"(粘性页脚)。

以下是实现Sticky Footer的代码:

<!DOCTYPE html>
<html>
<head>
    <title>Sticky Footer Example</title>
    <style type="text/css">
        * { margin: 0; }
        html, body { height: 100%; }
        .container {
            min-height: 100%;
            margin-bottom: -50px;
        }
        .footer { 
            height: 50px; 
            background-color: #333; 
            color: #fff;
            text-align: center;
        }
        .footer, .container:after {
            content: "";
            display: block;
        }
        .container:after {
            height: 50px;
        }
    </style>
</head>
<body>
    <div class="container">
        <p>Your website content goes here.</p>
    </div>
    <div class="footer">
        <p>Your sticky footer content goes here.</p>
    </div>
</body>
</html>

以上代码中实现Sticky Footer的关键点如下所述:

1.使用min-height属性使包含内容的容器(.container)至少和视口一样高。

2.margin-bottom属性值要和footer高度相同,这个负边距作用是将容器向上移动,从而使用这个高度生成的空间。

3.使用伪元素::after创建与footer相同高度的虚拟元素,使其距离内容的div上边缘留出空间。这样,当内容的div增加其起始高度时,虚拟元素也一起增加其高度。

4.为footer设置样式,以确保它始终位于容器底部。

另外,注意到上述示例中的html、body元素最小高度都被设定为100%,这是为了确保页面内容足够满屏,否则会影响Sticky Footer效果。

示例1:

下面是实现带有多个内容区(.content)的Sticky Footer的代码:

<!DOCTYPE html>
<html>
<head>
    <title>Sticky Footer Example</title>
    <style type="text/css">
        * { margin: 0; }
        html, body { height: 100%; }
        .container {
            min-height: 100%;
            margin-bottom: -50px;
        }
        .content {
            margin-bottom: 50px;
        }
        .footer { 
            height: 50px; 
            background-color: #333; 
            color: #fff;
            text-align: center;
        }
        .footer, .container:after {
            content: "";
            display: block;
        }
        .container:after {
            height: 50px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="content">
            <p>Your website content goes here.</p>
        </div>
        <div class="content">
            <p>Your website content goes here.</p>
        </div>
        <div class="content">
            <p>Your website content goes here.</p>
        </div>
    </div>
    <div class="footer">
        <p>Your sticky footer content goes here.</p>
    </div>
</body>
</html>

示例2:

下面是实现Sticky Footer时呈现始终完整的背景图像的代码:

<!DOCTYPE html>
<html>
<head>
    <title>Sticky Footer Example</title>
    <style type="text/css">
        html, body {
            height: 100%;
            margin: 0;
            padding: 0;
            background-image: url(background.jpg);
            background-size: cover;
            background-attachment: fixed;
        }
        .container {
            min-height: calc(100% - 50px);
            margin-bottom: -50px;
            padding: 20px;
            color: #fff;
            text-shadow: 1px 1px #333;
        }
        .footer {
            height: 50px;
            background-color: rgba(0, 0, 0, 0.8);
            color: #fff;
            text-align: center;
        }
        .footer, .container:after {
            content: "";
            display: block;
        }
        .container:after {
            height: 50px;
        }
    </style>
</head>
<body>
    <div class="container">
        <p>Your website content goes here.</p>
    </div>
    <div class="footer">
        <p>Your sticky footer content goes here.</p>
    </div>
</body>
</html>

此示例中,网页背景图像会营造一个完整而高端的感觉,而Sticky Footer与内容的div永远都是全幅屏幕的。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:CSS实现Sticky Footer的示例代码 - Python技术站

(0)
上一篇 2023年6月9日
下一篇 2023年6月9日

相关文章

  • CSS3 对过渡(transition)进行调速以及延时

    CSS3 提供了对过渡(transition)进行调速以及延时的功能,使元素的动画效果更加流畅和自然。在进行过渡调速以及延时时,需要借助两个 CSS 属性:transition-timing-function 和 transition-delay。 transition-timing-function transition-timing-function 用…

    css 2023年6月10日
    00
  • 详解vue样式穿透的几种方式

    下面是详解 “Vue 样式穿透的几种方式” 的完整攻略。 什么是 Vue 样式穿透 Vue 样式穿透是一种方式,可以让你在组件内部使用外部样式表中定义的样式规则。常见的情况是,你想要在组件内部修改或者扩展外部的 CSS 样式。 Vue 提供了多种方式支持样式穿透,下面我们将详细介绍这些方式。 方式一:使用 /deep/ 或者 ::v-deep 如果你使用的是…

    css 2023年6月9日
    00
  • Bootstrap 实现表格样式、表单布局的实例代码

    下面是关于“Bootstrap 实现表格样式、表单布局的实例代码”的攻略: Bootstrap 表格样式 Bootstrap 提供了多种表格样式,使用 table 类即可。如下是一个漂亮的、具有不同表格样式的 HTML 表格: <table class="table table-bordered table-hover table-strip…

    css 2023年6月10日
    00
  • js 本地预览的简单实现方法

    下面就为大家分享一下JS本地预览的简单实现方法。 如何实现JS本地预览 1. 安装浏览器插件 Chrome浏览器已经集成了一个本地预览的插件,我们可以直接在Chrome浏览器中打开本地HTML文件进行预览,但是其他浏览器可能不具备此功能,我们可以手动安装一个浏览器插件。目前比较常用的插件有“Live Server”和“Web Server for Chrom…

    css 2023年6月11日
    00
  • css下div下同行多元素右对齐

    要实现CSS下div下同行多元素右对齐,可以使用以下两种方式: 使用Flex布局 Flex布局是CSS的一个强大布局方式,可以实现简单的对齐需求。 要实现多个元素右对齐,可以将这些元素放在一个Flex容器中,添加justify-content: flex-end样式即可。同时,还需要给这些元素指定宽度,保证它们不会抢占Flex容器的空间。 下面是一个示例代码…

    css 2023年6月10日
    00
  • HTML+CSS+JS模仿win10亮度调节效果的示例代码

    让我们来详细讲解如何实现“HTML+CSS+JS模仿win10亮度调节效果的示例代码”。 该示例主要需要三个部分来实现:HTML、CSS和JS。我们将通过以下步骤来实现它: 创建HTML文件并添加必要的结构。此处我们需要一个大容器,用于容纳中央的亮度控件。 <div class="container"> <div cla…

    css 2023年6月10日
    00
  • css中height和line-height区别

    关于 CSS 中 height 和 line-height 的区别,我来为您做个详细的讲解。 height 和 line-height 的定义 首先,我们要了解一下 height 和 line-height 的定义: height: 元素的高度,设定一个具体数值或百分比,表示元素的高度是多少。 line-height: 行高,用于指定行内元素行框的高度。 h…

    css 2023年6月9日
    00
  • html+css实现环绕倒影加载特效

    实现环绕倒影加载特效的过程中,我们需要使用到HTML和CSS。HTML用于创建基础结构,CSS则用于定义样式并实现倒影特效。 步骤如下: 1.创建基础结构 首先在HTML文件中创建一个<DIV>容器用于包含图片和倒影,并设置容器的宽高。代码如下: <div class="reflect"> <img src=…

    css 2023年6月9日
    00
合作推广
合作推广
分享本页
返回顶部