【问题标题】:CSS aside height 100% when using sticky footer使用粘性页脚时 CSS 高度 100%
【发布时间】:2012-12-18 15:03:13
【问题描述】:

我有一个带有标题和sticky footer 的布局。两者都是 40px 高。现在我需要添加一个滚动条,它将填充可用空间(垂直)。它应该是这样的:

有两个限制:

  • 没有 JavaScript
  • 没有 CSS3 calc() 函数

这个问题有什么严格的 CSS 解决方案吗?

【问题讨论】:

  • 右栏发生了什么,它是如何滚动的?
  • 这是一个内容。内容太多有卷子,但高度不必固定。

标签: css cross-browser sticky-footer


【解决方案1】:

这是我要解释的一个小演示:little link

首先,分别使用absolutefixed 定位您的headerfooter。在body 的顶部和底部添加填充的40px,并确保其box-sizingborder-box。将您的asideheight 设置为100%,并确保它是border-box。基本上;

HTML:

<header>
    I'm a header!
</header>
<aside>
    Lots and lots and lots of content!
    Glee is awesome!
</aside>
<footer>
    I'm a footer!
</footer>

CSS:

html {
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    height: 100%;
}
body {
    padding-top: 40px;
    padding-bottom: 40px;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    height: 100%;
}
header {
    height: 40px;
    width: 100%;
    position: absolute;
    top: 0px;
}
footer {
    height: 40px;
    width: 100%;
    position: fixed;
    bottom: 0px;
}
aside {
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    height: 100%;
    overflow: auto; /*make sure there are scrollbars when needed*/
}

【讨论】:

  • 酷!我以前没有听说过 box-sizing:)。我现在就测试一下。
  • @Abody97 你应该颠倒你的前缀属性的顺序。将规范属性放在最后,这样当浏览器开始支持规范而不是它们自己的前缀(-webkit 等)时,您就不需要更新 CSS。
【解决方案2】:

只有position: fixed,边距(42px 用于测试目的,因为,嗯,42 但实际上应该是 40px)和 #aside 内的额外 div,这里有一个小提琴:http://jsfiddle.net/PhilippeVay/xcuVv/2/ 在 Firefox 中工作、Chrome、Safari 4、IE8 和 Opera 11 或 12,但在 IE7 中失败,如果需要兼容性,则需要回退。

注意这个设计没有考虑到:

  • 上网本或智能手机上的垂直空间
  • 如果页眉和页脚占用超过 40 像素会发生什么情况(每个 WCAG 2.0 将文本缩放至 200%,在某些操作系统和浏览器上字体比平时大,等等)
  • 在某些移动浏览器中几乎不可能进行内部滚动(进入旁边)
  • 使用空格键或向下翻页键滚动一页的人现在将滚动一个半屏幕(80 像素太多),这会惹恼他们(错误,我们)。请允许您的条形图隐藏或缩小为单个按钮,然后通过简单地单击 JS 中管理的按钮返回条形图...

【讨论】:

  • 在您的演示中,如果您向右滚动,main 部分会部分覆盖aside
  • 对,“仅用于演示”的像素宽度是不够的,它需要width: auto。小提琴编辑
猜你喜欢
  • 2013-09-28
  • 1970-01-01
  • 1970-01-01
  • 2015-05-12
  • 1970-01-01
  • 2013-10-13
  • 1970-01-01
  • 2014-09-20
  • 2014-02-13
相关资源
最近更新 更多