带有额外元素的 CSS 粘性页脚

发布于 2024-12-20 19:11:28 字数 2350 浏览 1 评论 0原文

我正在尝试为我的艺术作品集创建一个自己的网站,并且我遇到了 http://ryanfait.com /粘性页脚/。我无法为其添加额外的元素。

我有以下 HTML 结构:

<html>
<head>
    <link rel="stylesheet" href="style.css" ... />
    <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>
    <img src="image.png>    
    </div>
</body>

以及以下 style.css:

.wrapper {
position: relative;
width: 800px;
margin: 0 auto -50px;
}

.footer {
position: relative;
width: 100%;
margin: 0 auto;
padding: 0;
background-color:#000000;
text-align:center;
}

.footer img {
position: relative;
width: 400px;
top: -238px;
margin: 0 auto;
} 

.footer a {
color: #fff;
text-decoration: underline;
border: 0;
}

.footer p {
position: absolute;
left: 0;
bottom: 4px;
width: 100%;
padding: 0;
color: #fff;
font: 0.8em arial,sans-serif;
}

和 layout.css:

* {
margin: 0;
}
html, body {
height: 100%;
}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -50px; /* the bottom margin is the negative value of the footer's        height */}
.footer { height: 50px; /* .push must be the same height as .footer */} 

.push {
height: -100px; /* .push must be the same height as .footer */
}

我将图像设置为负数,以便在调整窗口大小时它将与主要内容重叠。另外,我想要在图像下方有一个粘性底部“边框”。然而,无论我如何弄乱边距或高度,我都无法摆脱上面代码创建的负空间。您有什么建议吗?

**我想通了。 粘性页脚教程制作了一个停在主体边界的粘性页脚。我想要的是一个粘性页脚,它是一个顶部“层”,它将覆盖主体并在底部有一个边框元素。 我不应该使用“top:-238px”。相反,我在 html 和 css 的页脚下嵌套了一个类。

<div class="footer">        
    <img src="Image.png" width="400" height="238" />
    <div class="bottom-border">
        <p>Copyright (c) 2008</p>
    </div>
</div>

然后

.footer img {
position: relative;
width: 400px;
margin: 0 auto;} 

.bottom {
position: relative;
width: 100%;
height: 20px;
margin: 0 auto 0;
padding: 0;
text-align:center;
background-color: #000000;}

,根据“layout.css”注释中粘性页脚的说明,我保持 .wrapper、.footer、.push 高度都相同。**

I'm trying to create a website on my own for my art portfolio and I ran across http://ryanfait.com/sticky-footer/. I'm having trouble adding an extra element for it.

I have the following HTML structure:

<html>
<head>
    <link rel="stylesheet" href="style.css" ... />
    <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>
    <img src="image.png>    
    </div>
</body>

And the following style.css:

.wrapper {
position: relative;
width: 800px;
margin: 0 auto -50px;
}

.footer {
position: relative;
width: 100%;
margin: 0 auto;
padding: 0;
background-color:#000000;
text-align:center;
}

.footer img {
position: relative;
width: 400px;
top: -238px;
margin: 0 auto;
} 

.footer a {
color: #fff;
text-decoration: underline;
border: 0;
}

.footer p {
position: absolute;
left: 0;
bottom: 4px;
width: 100%;
padding: 0;
color: #fff;
font: 0.8em arial,sans-serif;
}

with the layout.css:

* {
margin: 0;
}
html, body {
height: 100%;
}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -50px; /* the bottom margin is the negative value of the footer's        height */}
.footer { height: 50px; /* .push must be the same height as .footer */} 

.push {
height: -100px; /* .push must be the same height as .footer */
}

I set the image negative so that it will overlap the main content when the window is resized. Also, I would like a sticky bottom "border" right below the image. However, no matter how much I mess with the margins or heights, I cannot get rid of the negative space that the above code creates. Do you have any suggestions?

**I figured it out.
The sticky-footer tutorial makes a sticky footer that stops at the border of the main body. What I wanted was a sticky footer that was a top "layer" that will go over the main body AND have a border element on the bottom.
I should not have used the 'top:-238px'. Instead, I nested a class under footer in html and css.

<div class="footer">        
    <img src="Image.png" width="400" height="238" />
    <div class="bottom-border">
        <p>Copyright (c) 2008</p>
    </div>
</div>

and

.footer img {
position: relative;
width: 400px;
margin: 0 auto;} 

.bottom {
position: relative;
width: 100%;
height: 20px;
margin: 0 auto 0;
padding: 0;
text-align:center;
background-color: #000000;}

Then, per sticky-footer's instructions in the 'layout.css' comments, I kept the .wrapper, .footer, .push height's all the same.**

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人练习一个人 2024-12-27 19:11:28

虽然有点晚了,但我还是可以回答:)问题的发生是因为即使您的图像相对放置在页脚上方,但它仍然占据页面中的相同位置。您想使用position:absolute;

这里正在工作

我做了以下更改:

.footer img { 
  position:absolute; 
}

.footer p {
  position: relative;
  height:4em;
}

使用 position:absolute;会将元素放置在最后一个非静态(默认)元素的位置,并将其从“页面流”中删除。因此,在本例中,它将其放置在 .footer 并将其从页面中取出,这样它就不再占用任何空间。

编辑:此外,我还通过将其更改为绝对来打破居中,因为 margin:0 auto; 不适用于 position:absolute; 元素。添加这些规则来解决这个问题。

.footer img {
  left:50%;
  margin-left:-200px;
}

Bit late but I can answer it anyway :) The problem is occurring as even though your image is relatively placed above the footer it still occupies the same place in the page. You want to use position:absolute;.

Here it is working

I made the following changes:

.footer img { 
  position:absolute; 
}

.footer p {
  position: relative;
  height:4em;
}

Using position:absolute; will place the element in the position of the last non-static (default) element and remove it from the 'flow of the page'. So in this case it places it at .footer and takes it out of the page so it doesn't take up any space anymore.

EDIT: Also I broke the centering by changing it to absolute as margin:0 auto; won't work on position:absolute; elements. Add these rules to fix that.

.footer img {
  left:50%;
  margin-left:-200px;
}
浅沫记忆 2024-12-27 19:11:28

我有最简单的粘页脚解决方案。请简单地在 body 和 html 上添加 height: 100% 。然后包装显示: table 。要添加元素,您可以在 .w1 element 内添加任何内容/元素。

它的页脚也很灵活。

这是代码

html{height:100%;}
body{
    margin:0;
    height:100%;
  background: #ccc;
}
#wrapper{
    width:100%;
    height:100%;
    display:table;
    margin:0 auto;
}
#footer{
    width:100%;
    overflow:hidden; /*for FF on Windows 7*/
    display:table-footer-group;
    height:1%;
  background: #333;
  color: #fff;
}
<div id="wrapper"> <!-- table -->
    <div class="w1">
        <p>header and content of the page</p>
    </div>
    <div id="footer"> <!-- table-footer-group -->
            <p>footer content</p>
    </div>
</div>

I have easiest solution for sticky footer. Please simple add height: 100% on body and html. Then wrapper display: table . For adding element you can add any content/element inside of .w1 element .

And its footer flexible too.

Here is the code

html{height:100%;}
body{
    margin:0;
    height:100%;
  background: #ccc;
}
#wrapper{
    width:100%;
    height:100%;
    display:table;
    margin:0 auto;
}
#footer{
    width:100%;
    overflow:hidden; /*for FF on Windows 7*/
    display:table-footer-group;
    height:1%;
  background: #333;
  color: #fff;
}
<div id="wrapper"> <!-- table -->
    <div class="w1">
        <p>header and content of the page</p>
    </div>
    <div id="footer"> <!-- table-footer-group -->
            <p>footer content</p>
    </div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文