CSS:固定到底部并居中
我需要将页脚固定在页面底部并将其居中。 页脚的内容可能随时变化,所以我不能仅仅通过 margin-left: xxpx; 将其居中。 右边距:xxpx;
问题是,由于某种原因,这不起作用:
#whatever {
position: fixed;
bottom: 0px;
margin-right: auto;
margin-left: auto;
}
我爬行了网络,但什么也没找到。 我尝试制作一个容器 div 和 nada。 我尝试了其他组合和gurnisht。 我怎样才能做到这一点?
谢谢
I need my footer to be fixed to the bottom of the page and to center it. The contents of the footer may change at all time so I can't just center it via margin-left: xxpx; margin-right: xxpx;
The problem is that for some reason this doesn't work:
#whatever {
position: fixed;
bottom: 0px;
margin-right: auto;
margin-left: auto;
}
I crawled the web and found nothing. I tried making a container div and nada. I tried other combinations and gurnisht. How can I make this happen?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您应该使用粘性页脚解决方案,例如:
还有其他类似的解决方案;
与html:
You should use a sticky footer solution such as this one :
There are others like this;
with the html:
修改 Daniel Kanis 的代码:
以下行:
只需更改 CSS和 html 中的
revised code by Daniel Kanis:
just change the following lines in CSS
and in html:
jQuery 解决方案
有时,实现 JS 比修改旧的 CSS 更容易。
http://jsfiddle.net/gLhEZ/
A jQuery solution
Sometimes it's easier to implement JS than to hack old CSS.
http://jsfiddle.net/gLhEZ/
问题出在
position: static
上。 静态意味着根本不对该位置进行任何操作。position:absolute
就是你想要的。 不过,将元素居中仍然很棘手。 以下应该有效:或
但我推荐第一种方法。
我使用了这个答案中的居中技术:
如何在div中将绝对定位的元素居中?
The problem lies in
position: static
. Static means don't do anyting at all with the position.position: absolute
is what you want. Centering the element is still tricky though. The following should work:or
But I recommend the first method.
I used centering techniques from this answer:
How to center absolutely positioned element in div?
我遇到了一个问题,典型的
position:fixed
和bottom:0
不起作用。 发现了一个巧妙的功能:position:sticky
。 请注意,它是“相对”新的,因此 IE/Edge 15 及更早版本不会出现这种情况。这是 w3schools 的示例。
I ran into a problem where the typical
position: fixed
andbottom: 0
didn't work. Discovered a neat functionality withposition: sticky
. Note it's "relatively" new so it won't with IE/Edge 15 and earlier.Here's an example for w3schools.
我发现有 2 个潜在问题:
1 - IE 在定位方面遇到了麻烦:过去已修复。 如果您使用带有有效文档类型的 IE7+ 或非 IE 浏览器,这不是问题的一部分
2 - 如果您希望页脚对象居中,则需要指定页脚的宽度。 否则,它默认为页面的整个宽度,并且左右的自动边距设置为 0。如果您希望页脚栏占据宽度(如 StackOverflow 通知栏)并将文本居中,那么您需要将“text-align:center”添加到您的定义中。
There are 2 potential issues that I see:
1 - IE has had trouble with position:fixed in the past. If you are using IE7+ with a valid doctype or a non-IE browser this isn't part of the problem
2 - You need to specify a width for the footer if you want the footer object to be centered. Otherwise it defaults to the full width of the page and the auto margin for the left and right get set to 0. If you want the footer bar to take up the width (like the StackOverflow notice bar) and center the text, then you need to add "text-align: center" to your definition.
我已将“问题 div 放在另一个 div 中”让我们将此 div 称为封闭 div...
使 css 中的封闭 div 的宽度为 100%,位置固定,底部为 0...
样子
然后将有问题的 div 插入到封闭的 div 中,这就是它在 html 中的
...好了!
-超文本
I have encased the 'problem div in another div' lets call this div the enclose div...
make the enclose div in css have a width of 100% and postion fixed with a bottom of 0...
then insert the problem div into the enclose div this is how it would look
then in html...
There ya go!
-Hypertextie
根据@Michael 的评论:
我去寻找解释,它归结为:
更多详细信息请访问 http://css-tricks.com/absolute-positioning-inside -相对定位/
Based on the comment from @Michael:
I went digging for the explanation and it boils down to this:
More details at http://css-tricks.com/absolute-positioning-inside-relative-positioning/
这是一个使用 CSS 网格的示例。
你可以使用 css grid:一个具体的例子
here is an example using css grid.
you can use css grid:a concrete example
对于仍在寻求解决方案的人来说,有一个非常简单的方法可以做到这一点:
一个重要的考虑因素是,因为它粘在窗口底部,所以它会隐藏在其后面滚动的内容。 所以你需要确保它有背景颜色。
此外,您还需要为父级提供一些超出固定位置元素高度的底部填充。 如果
.target-element
的高度为 4rem,则底部内边距应为 4rem + 滚动内容末尾与固定元素顶部之间所需的空间量。For anyone still seeking a solution for this, there’s a really simple way to do this:
An important consideration is that because this sticks to the bottom of the window, it will cloak content scrolling behind it. So you’ll want to make sure it has a background color.
Also, you’ll want to give the parent some bottom padding that extends beyond the height of the fixed-position element. If
.target-element
is 4rem high, your bottom padding should be 4rem + the amount of space you want between the end of your scrolling content and the top of the fixed element.