【问题标题】:IE 10 bug with display table CSS when height is 100%?高度为 100% 时显示表格 CSS 的 IE 10 错误?
【发布时间】:2017-05-13 22:09:44
【问题描述】:

我已经尝试了 3 天来避免在我的新响应式设计中使用表格,主要是因为每个人都说他们是邪恶的。另一方面,在对 SEO 和 table 进行更多研究的同时,有些人甚至说它提高了他们的知名度。

无论如何,div 和 HTML5 的语义要好得多,所以我真的很想让下面的这个例子工作。主要问题是在 IE9 & 10 中,“section row”会因为“height 100%”值而溢出。所有其他浏览器似乎反应良好。

有谁知道如何解决这个问题(最好不用 Javascript?):

JSFiddle:http://jsfiddle.net/fvqvdske/1/

IE10 仿真模式下测试的全尺寸页面:http://jsfiddle.net/fvqvdske/1/show

唯一的问题是溢出和IE9, 10 - 中间行需要填满页眉和页脚留下的所有空间(并且页眉和页脚需要能够调整高度到动态适应他们的内容,例如没有固定的,没有绝对的位置)。

HTML

<body>
<header role="banner">
    <div>
        <div>
            This is the top banner
        </div>
    </div>
</header>
<section role="main">
    <div>
        <div>
            <div>
               <div>
                   <div style="border: 2px solid red;">
                        This is the content<br/>
                        This is the content<br/>
                        This is the content<br/>
                        This is the content<br/>
                        This is the content<br/>
                   </div>
                </div>
            </div>
        </div>
    </div>
</section>
<footer role="contentinfo">
    <div>
        <div>
            This is the footer
        </div>
    </div>
</footer>
</body>

CSS

    html, body {
        height: 100%;
        width: 100%;
        margin: 0;
        padding: 0;
    }
    body {
        height: 100%;
        width: 100%;
        display: table;
        position: absolute;
        border-spacing: 0;
        border-collapse: collapse;
    }

    header, footer, section {
        display: table-row;
        text-align: center;
        vertical-align: middle;
        height: 1px;
    }

    header > div, section > div, footer > div {
        display: table-cell;
        vertical-align: middle;
    }

    header > div > div,
    section > div > div,
    section > div > div > div,
    footer > div > div {
        max-width: 400px;
        width: 100%;
        margin: auto;
        background-color: brown;
    }

    section,
    section > div,
    section > div > div,
    section > div > div > div,
    section > div > div > div > div {
        box-shadow: inset 0 1em 1em -1em #222, inset 0 -1em 1em -1em #222;
        height: 100%;
        vertical-align: middle;
    }

    section > div {
        display: table-cell;
        width: 100%;
        height: 100%;
    }

    section > div > div {
        display: table;
        margin: auto;
    }

    section > div > div > div {
        display: table-row;
    }

    section > div > div > div > div {
        display: table-cell;
    }


    section {
    }

    /* temp */
    header {
        background-color: pink;
    }

    section {
        background-color: yellow;
    }

    footer {
        background-color: orange;
    }

【问题讨论】:

  • 这是一个丑陋的错误,你试过用弹性盒子吗? display: flex;jsfiddle.net/GXe9m/2
  • 我的项目对 Flexbox 的支持不够:caniuse.com/#search=flexbox - 你确定这是一个错误……我们无能为力吗?我做了一个小更新来清理我的 css 并使它更容易理解。
  • 就个人而言,如果我必须使用 display:table、table-row 和 table-cell,那么我会使用实际的 HTML 表格。对我来说似乎很困惑。从未听说过 SEO 论点。你有更多相关信息的链接吗?
  • @nbouvrette 尝试设置max-height:100%;height: 100%; 有区别吗?
  • 这里是 SEO 链接:webmasters.stackexchange.com/questions/6890/… - 我使用表格的唯一原因是中间部分。当没有足够的内容时,我希望它填充 100% 的高度。我有 2 个其他线程在没有表格的情况下解决这个问题,没有人能弄清楚。我有一个表格布局也可以很好地工作,但从语义上讲这个更简单(但正如你所说,可能不太容易理解)。

标签: css html internet-explorer css-tables


【解决方案1】:

在这个问题上花了几天时间之后,似乎对于这个特定的布局(尤其是 100% 高度),使用表格是最好的选择。这 3 个不同的线程对问题的不同转折无法得到结果(所以我怀疑这个 IE 错误是在 2014 年继续使用表格的原因之一):

通过阅读有关表格和 SEO 的更多信息,我也找不到任何使用单个表格会产生任何影响的相关信息或有形数据。我能找到的唯一有形信息是,只有在客户端下载了完整的 HTML 后,表格才会显示。考虑到 HTML 在网页整体大小中的大小,这在 2014 年也不是什么大问题。 CSS 可以用于所有表格的样式,这解决了我看到的关于表格的其他一些抱怨。这篇文章可能最能反映这里的情况:

http://olav.dk/articles/tables.html

2014 年的表可能还可以 :)

【讨论】:

  • 每当您使用table 进行布局时,您都应该使用role="presentation" W3C: 4.9.1 The table element: [...]Tables should not be used as layout aids.[...] If a table is to be used for layout it must be marked with the attribute role="presentation"[...] 作为除1empty 之外的边框值对 html5 无效,您不应使用 border=0 来表示。
  • @t.niese 好收获!我刚刚将此添加到我的代码中。谢谢
猜你喜欢
  • 2015-03-28
  • 2014-10-06
  • 2015-02-07
  • 2010-10-27
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 2018-08-14
  • 1970-01-01
相关资源
最近更新 更多