【问题标题】:Why is this flexbox layout not working in IE11? [duplicate]为什么这个 flexbox 布局在 IE11 中不起作用? [复制]
【发布时间】:2019-06-19 09:40:25
【问题描述】:

我尝试用于网页的 flexbox 布局具有以下基本 HTML/CSS:

body, div, html, h1, h2, li, p, ul {
    margin: 0;
    padding: 0;
}
html,
body {
    height: 100%;
}
.container {
    display: flex;
    flex-direction: column;
    min-height: 100%;
}
.headerContainer {
    background: darkblue;
    color: white;
    padding: 10px;
}
.sidebarAndContentContainer {
    display: flex;
    flex: 1;
}
.sidebar {
    background: gray;
    padding: 10px;
}
.nav {
    color: white;
    list-style: none;
}
.content {
    padding: 10px;
}
<div class="container">
    <div class="headerContainer">
        <h1>Site Title</h1>
    </div>
    <div class="sidebarAndContentContainer">
        <div class="sidebar">
            <ul class="nav">
                <li>Nav Link #1</li>
                <li>Nav Link #2</li>
                <li>Nav Link #3</li>
                <li>Nav Link #4</li>
                <li>Nav Link #5</li>
            </ul>
        </div>
        <div class="content">
            <h2>Page Title</h2>
            <p>Content</p>
            <p>Content</p>
            <p>Content</p>
            <p>Content</p>
        </div>
    </div>
</div>

布局看起来像我在 Chrome、Firefox 和 Edge 中想要的,但是当我将其加载到 IE11 中时,侧边栏 div 并没有像应有的那样拉伸页面的垂直长度。事实上,它只有 20px 高(这只是填充)。

为什么在 IE11(我认为它支持 flexbox)中会发生这种情况,更重要的是,我该如何解决?谢谢。


编辑:在找出可行的解决方案并将其发布后,我认为这个问题与问题顶部链接的问题不重复,因为最终,其他帖子确实做到了没有相同的问题或回答我问的问题。

【问题讨论】:

    标签: html css layout flexbox internet-explorer-11


    【解决方案1】:

    IE11 仅部分支持 flexbox。

    如果您还没有,请查看此网站:https://caniuse.com/#feat=flexbox

    除非它非常关键,否则我认为不值得为支持 Microsoft 已长期使用的 2 代旧浏览器而失去太多睡眠。

    【讨论】:

    • 有些人无法选择放弃对它的支持,直到它达到 MS 的生命支持结束,也就是企业开发的美妙世界。 ;)
    • @ChrisW。该死,这很粗糙。
    • 因此,为什么微软应该偶尔提供拿起酒吧标签来强迫我们中的一些人喝哈哈。 ;)
    • @ChrisW。好吧,我想有人必须确保在阁楼中放置 10 年的桌面在启动时仍然可以运行公司网站而不会出现问题。
    【解决方案2】:

    尝试将其从 flex:1; 更改为 flex: 1 0 0;

    这个链接会解释https://stackoverflow.com/a/22883146/6288545

    【讨论】:

    • 这在 IE11 中效果很好,但是当内容足够溢出屏幕时,它会导致侧边栏背景颜色在 Chrome 中的height: 100%; 处被切断。有什么想法吗?
    • 您是否尝试过使用flex: 1 0 auto;flex: 1 0 100%;
    • 我能够将您的答案与 Chris W.'s 一起使用来获得我需要的东西。非常感谢。
    【解决方案3】:

    问题只是 min-heightheight 语义。试试看;

    body, div, html, h1, h2, li, p, ul {
        margin: 0;
        padding: 0;
    }
    html,
    body {
        height: 100%;
    }
    .container {
        display: flex;
        flex-direction: column;
        /* min-height: 100%;  <---- Your Culprit */
        height: 100%;
    }
    .headerContainer {
        background: darkblue;
        color: white;
        padding: 10px;
    }
    .sidebarAndContentContainer {
        display: flex;
        flex: 1;
    }
    .sidebar {
        background: gray;
        padding: 10px;
    }
    .nav {
        color: white;
        list-style: none;
    }
    .content {
        padding: 10px;
    }
    <div class="container">
        <div class="headerContainer">
            <h1>Site Title</h1>
        </div>
        <div class="sidebarAndContentContainer">
            <div class="sidebar">
                <ul class="nav">
                    <li>Nav Link #1</li>
                    <li>Nav Link #2</li>
                    <li>Nav Link #3</li>
                    <li>Nav Link #4</li>
                    <li>Nav Link #5</li>
                </ul>
            </div>
            <div class="content">
                <h2>Page Title</h2>
                <p>Content</p>
                <p>Content</p>
                <p>Content</p>
                <p>Content</p>
            </div>
        </div>
    </div>

    【讨论】:

    • 总的来说,我喜欢这个答案,但我看到的一个问题是,当内容滚动离开页面时,侧边栏背景颜色会停止在初始(未滚动)屏幕停止的位置(即@987654326 @)。有什么想法吗?
    • 是的,你最终需要学习 how flex worksgrowshrinkbasisorder 并改变你的整体结构。我并没有假设您希望您当前的 UI 内容更改到那种程度,只是在回答为什么它没有在 IE11 中拉伸示例页面的垂直长度的问题 :)
    • 我觉得我非常了解 flexbox 及其大部分属性,但flex(以及简写的flex-growflex-shrinkflex-basis 属性)真的让我很困惑。我认为这三者如何结合在一起以及浏览器之间的差异使得这变得非常困难。在一天结束时,我能够将您的答案与 Max Hughes 的答案一起使用,并且只是为了得到我想要的东西(我将在一分钟内发布最终解决方案)。再次感谢您的帮助,但链接的 CSS 技巧文章中的 flex-basis 解释仍然让我感到困惑。
    • 是的,这有点让人头疼。因此,为什么我个人通常只为核心布局而不是 flex 选择良好的老式盒模型样式布局。很高兴你得到了补救,周末愉快!
    【解决方案4】:

    首先,非常感谢 Chris W. 和 Max Hughes 的回答和建议。没有它,我永远找不到答案。话虽如此,我最终不得不结合使用这两个答案来获得我正在寻找的东西。

    在我提供答案之前,这是我一直在寻找但以前没有得到的东西:

    • 侧边栏始终拉伸 100% 的屏幕垂直空间(标题未占用任何内容)。
    • 无论页面内容是否导致页面滚动,侧边栏都会拉伸到视图窗口底部。
    • 适用于 Chrome、Firefox、Edge、IE10 + IE11 的东西。

    话虽如此,我原始答案中对代码的以下两个更改是满足上述所有条件的:

    .container {
        display: flex;
        flex-direction: column;
        height: 100%; /* Changed from min-height. */
    }
    ...
    .sidebarAndContentContainer {
        display: flex;
        flex: 1 0 auto; /* Changed from flex: 1. */
    }
    

    【讨论】:

    • 有更清洁、更高效的解决方案。查看我发布的副本。
    • Michael_B,您能否就“更清洁、更高效的解决方案”的含义提供更多指导?我看到的两件事是使用flex: auto 而不是flex: 1 0 auto 并制作html 和/或body 元素display: flex。首先,IE11 中仍然存在一些奇怪之处,因此flex: autoflex: 1 0 auto 不同,我在几个地方读过(我同意)将display: flex 添加到htmlbody 元素可能并不理想。我错过了什么?谢谢。
    猜你喜欢
    • 2015-10-27
    • 2019-03-09
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多