【问题标题】:Flexbox centering and overflow bug in IE10?IE10 中的 Flexbox 居中和溢出错误?
【发布时间】:2015-11-07 11:10:21
【问题描述】:

我想要做的基本上是有一个标题和一个容器,如果它们比屏幕小(100% 高度的主体),它们总是居中。如果内容和标题大于屏幕,则允许在其容器内滚动(注意标题不应该是可滚动的)。

我设法让它在 Chrome 中工作,但在 IE10 中没有。这是 JSFiddle。

var p = $('p');
$('button').click(function(){
	for(var i=0; i<30; i++){
        $('.content').append(p.clone());
    }
});
.screen{
    border: 1px solid red;
    padding: 3px;
    height: 300px;
    display: flex; 
    display: -ms-flexbox; 
    justify-content: center; 
    -ms-flex-pack: center;
    flex-direction: column; 
    -ms-flex-direction: column;
}

.header{border: 1px solid blue; padding: 10px;}
.content{ border: 1px solid green; background: #aaa; overflow: auto;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Add more content</button>
<div class="screen">
    <div class="header">
        Header
    </div>
    <div class="content">
        <p>This is the content</p>
    </div>
</div>

http://jsfiddle.net/w8zg8hr2/2/

【问题讨论】:

    标签: css flexbox internet-explorer-10


    【解决方案1】:

    您需要将 flex: 0 1 auto; 添加到 .content div 并将 overflow: auto; 添加到 .screen 容器。 IE10 使用 2012 语法,默认为 flex: 0 0 auto,而不是现代值 flex: 0 1 auto;

    var p = $('p');
    $('button').click(function () {
        for (var i = 0; i < 30; i++) {
            $('.content').append(p.clone());
        }
    });
    .screen {
        border: 1px solid red;
        padding: 3px;
        height: 300px;
        display: flex;
        display: -ms-flexbox;
        justify-content: center;
        -ms-flex-pack: center;
        flex-direction: column;
        -ms-flex-direction: column;
        overflow: auto;
    }
    .header {
        border: 1px solid blue;
        padding: 10px;
    }
    .content {
        border: 1px solid green;
        background: #aaa;
        overflow: auto !important;
        flex: 0 1 auto; /* Add this property value */
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button>Add more content</button>
    <div class="screen">
        <div class="header">Header</div>
        <div class="content">
            <p>This is the content</p>
        </div>
    </div>

    Win7/IE10的Browserstack截图:

    【讨论】:

    • 感谢您的回答,但在 IE10 中仍然无法使用。当我添加更多内容时,内容框会一直延伸到屏幕框之外,而不是像 Chrome 中那样允许滚动。
    • @ErwinGO 你确定你是在 IE10 上测试,而不是其他版本或其他文档模式?这个 sn-p 在 IE10 中的工作方式与在 Chrome/Firefox 中的工作方式相同。
    • @ErwinGO 我忘记了容器;它还需要一个溢出属性...答案已更新。
    • 它接近解决方案:) 但标题应该始终在顶部,并且它没有固定的高度,所以我不能“定位:绝对”它。
    • @ErwinGO 啊,我忽略了问题的那一部分。是的,使用 IE10 和 flexbox,这将是真的棘手。肯定需要一些 JavaScript 才能使其正常运行。但是,IE10 一直是常青树;自从 IE11 首次问世以来,它应该会自动提示用户更新到 IE11,所以希望这对许多用户来说不会成为问题。
    猜你喜欢
    • 2017-01-10
    • 2015-12-08
    • 2013-09-08
    • 2017-12-08
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 2018-02-14
    相关资源
    最近更新 更多