Flexbox centering and overflow bug in IE10?

2019-08-08 17:21发布

问题:

What I want to do basically is have a header and a container always centered if they're small than the screen (body with 100% height). In case the content and header are bigger than the screen, then allow to scroll inside its container (notice that the header shouldn't be scrollable).

I managed to make it work in Chrome but not in IE10. This is the 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/

回答1:

You need to add flex: 0 1 auto; to the .content div and overflow: auto; to the .screen container. IE10 uses the 2012 syntax which defaults to flex: 0 0 auto instead of the modern value of 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>

Browserstack screenshot of Win7/IE10: