如何在 HTML 和 CSS 中使用 html 对象编写粘性页脚?
我正在为我正在设计的两个网站使用 Less Framework 4。在这两种设计中,我想在文档的顶部和底部应用 5 像素边框。
问题:由于应用于 body 的样式,我将 border-bottom 和 border-top 应用于 html 对象,因此底部边框永远不会粘在页面底部就像通常的粘页脚情况一样。
以下是两种情况的屏幕截图:
这是我用于 html 和 body 的(较少)CSS:pastie.org/private/us5x1vhjnwzq6zsiycnu2g
html {
border-top: solid @black 10px;
border-bottom: solid @black 10px;
background: url('img/bg.png') repeat;
}
body {
width: @8col;
margin: 0px auto;
padding: 100px 48px 84px;
background: @white;
color: rgb(60,60,60);
-webkit-text-size-adjust: 100%; /* Stops Mobile Safari from auto-adjusting font-sizes */
font: 13px/20px 'OftenRegular';
-webkit-tap-highlight-color: @green;
}
我尝试过对 body
使用 height: 100%
和html
对象,但它看起来是这样的:http://prikonline.be/prikactie/
我应该如何将边框粘到页面底部?
I'm using Less Framework 4 for two websites I'm designing. In both designs I want to apply a 5 pixel border both on top and bottom of the document.
The problem: because of the styles applied to body, I'm applying the border-bottom and border-top to the html object, so the bottom border never sticks to the bottom of the page like it would happen in a usual sticky footer situation.
Here are the screenshots for the two cases:
Here's the (LESS) CSS I'm using for html and body: pastie.org/private/us5x1vhjnwzq6zsiycnu2g
html {
border-top: solid @black 10px;
border-bottom: solid @black 10px;
background: url('img/bg.png') repeat;
}
body {
width: @8col;
margin: 0px auto;
padding: 100px 48px 84px;
background: @white;
color: rgb(60,60,60);
-webkit-text-size-adjust: 100%; /* Stops Mobile Safari from auto-adjusting font-sizes */
font: 13px/20px 'OftenRegular';
-webkit-tap-highlight-color: @green;
}
I've tried using height: 100%
both for the body
and html
objects, but here's how it looks live: http://prikonline.be/prikactie/
How should I stick the border to the bottom of the page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用这样的页脚包装器。
然后将其插入到
或其他内容之前
You could instead use a footer wrapper like this.
and just insert this right before
</body>
or somehting您可以使用
position:fixed;
和bottom:0px;
始终将其固定在底部,无论您的滚动状态和内容高度如何。You can use
position:fixed;
andbottom:0px;
to always, regardless of your scrolling state and content height, fix it to the bottom.尝试将其更改为:
用于您的 HTML CSS。
Try changing it to:
for your HTML CSS.
嗯...将
min-height: 100%
放在页面上的html
元素上(在 Web Inspector 中进行操作)在 Chrome 中立即对我有用;你在测试什么?然而,由于边框的高度,这种方法确实有点超过 100%,您可以在 IE8+/Gecko/WebKit 中使用 CSS
box-sizing 属性(使用值
border-box
)。对于 IE7 和 IE6,如果您想让它们呈现相同的效果,那么编写一些 JavaScript 会很容易,在加载或调整大小时,检查窗口高度,与文档高度进行比较,并在必要时强制 HTML元素到窗口高度减 20。
Hmmm... Putting
min-height: 100%
on thehtml
element on your page (manipulating in Web Inspector) worked for me right away in Chrome; what are you testing in?This approach does, however, go a little bit over 100% because of the height of the border, which you can correct for in IE8+/Gecko/WebKit with the CSS
box-sizing
property (use the valueborder-box
).For IE7 and IE6, if you care to make them render the same, it'd be pretty easy to write a little JavaScript that, on load or on resize, checks the window height, compares to document height, and if necessary forces the HTML element to the window height minus 20.
看起来您正在使用某种动态样式表工具(如 LESS)。通常,动态样式表工具允许您使用 JavaScript。因此,您可以将高度定义为:
然后添加类似的内容
当然,这样做的问题是,如果用户要调整他/她的浏览器窗口的大小,布局将无法适当更新。您可以使用 window.onresize 回调来处理该问题。
当然,您可以使用 JavaScript 来处理整个事情。诚然,有些人强烈反对使用 JavaScript 来进行样式设置(行为、内容和样式的分离),当尝试诸如粘性页脚之类的东西时,有时只编写两行 JavaScript 比尝试想出一些聪明的方法更容易CSS 可能在您尝试定位的每个浏览器中工作,也可能不工作。如果用户关闭了 JavaScript,那么在内容较少的页面上,页面就不会填充页面的整个高度。
It looks like you're using some sort of dynamic stylesheet tool (like LESS). Usually the dynamic stylesheet tools let you use JavaScript. So you could define height as:
And then add something like
Of course, the problem with this is that if the user were to resize his/her browser window, the layout would not update appropriately. You could use the
window.onresize
callback to handle that.Of course, you could use JavaScript to handle the whole thing. Granted, some vehemently oppose the use of JavaScript to do styling (separation of behavior, content, and style), when attempting things like a sticky footer, sometimes its easier to just write two lines of JavaScript than to try to come up with some clever CSS that may or may not work in every browser you're trying to target. If the user has JavaScript turned off, then the page just doesn't fill the whole height of the page on pages with less content.
我不建议您将 CSS 应用于 html 元素。相反,创建具有相似样式的 div。
一般情况下,你的代码应该是这样的:
HTML
CSS
这在所有浏览器中都可以完美工作,甚至在 IE6 中:)
I do not advise you to apply CSS to html element. Instead create div with similar styles.
In general case your code sould be like this:
HTML
CSS
This works perfect in all browsers, even in IE6 :)
您始终可以实现此有效的粘性页脚CSS(我添加了内联社交栏):
HTML:
只需将hrefs编辑为您自己的个人url,将图像src编辑为您想要的社交风格图像(或包含字体awesome包如果有的话)。
You can always implement this working sticky-footer CSS (I've added with inline social bar):
HTML:
Just edit the hrefs to your own personal urls, and the image src to the social style images you want (or include the font awesome package if you have it).
以下是我在底部添加主体边框的方法:
关键是
min-height: 100vh
,它确保主体高度至少是视口的高度。由于box-sizing: border-box
,body 的边框将计入 body 高度。仍然存在内容边距将边框推到视口下方的问题,但可以使用任意padding-top
值来修复。Here is how I added a body border at the bottom:
The key is
min-height: 100vh
, which ensures that body height will at least be height of the viewport. Because ofbox-sizing: border-box
, border of the body will be accounted in the body height. There is still a problem of content margins pushing the border below viewport, but that can be fixed with an arbitrarypadding-top
value.