问题
I'm having an issue in Chrome 40.0.2214.93 where if I override the justify-content
for an element I get some unexpected behavior.
I've created a JS Fiddle for this here: http://jsfiddle.net/n670tmeu/
html
<header id="top">
<div id="box1">
This is Box 1
</div>
<div id="box2">
This is Box 2
</div>
</header>
css
header {
background-color: #ccc;
width: 100%;
display: -webkit-flex;
display: -moz-flexbox;
display: -ms-flexbox;
display: flex;
-webkit-justify-content: flex-end;
-moz-justify-content: flex-end;
-ms-justify-content: flex-end;
justify-content: flex-end;
}
header#top {
-webkit-justify-content: space-between;
-moz-justify-content: space-between;
-ms-justify-content: space-between;
justify-content: space-between;
}
#box1, #box2 {
border-width: 1px;
border-style: solid;
border-color: red;
}
You'll notice that the more specific header#top
overrides the justify-content
however in Chrome it ends up pushing the first item to the flex-end
position then adds extra space for for the space-between
.
I've tried this in FireFox 35.0.1 and Safari 7.1.2 and they both work properly. Is this a bug in Chrome or did I do something wrong?
回答1:
This is a known bug that is fixed as of the latest canary build (42.0.2289.0):
https://code.google.com/p/chromium/issues/detail?id=452606
In the meantime, you can use this jQuery workaround as a temporary fix:
$('body *').each(function(i, el) {
var justifyContents = $(el).css('justify-content').split(' ');
var flexFlows = $(el).css('flex-flow').split(' ');
if (flexFlows[0] == 'row' && justifyContents.length > 1) {
if (justifyContents[0] == 'space-between' || justifyContents[0] == 'flex-start') {
$(el).css('justify-content', justifyContents[0]+' left');
} else if (justifyContents[0] == 'flex-end') {
$(el).css('justify-content', justifyContents[0]+' right');
}
}
});
来源:https://stackoverflow.com/questions/28203658/chrome-not-properly-overriding-justify-content-for-flex-box