2017-08-07 8 views
0

Following code作品はオフ少しです:Safariでフレックスボックスコンテナの固定コンテンツを中心に置く方法は? ChromeとFirefoxのではなく、Safariの子コンテナに(ブロックを中心に)期待通り

#container { 
    width: 100%; 
    background: yellow; 
    height: 20px; 
    position: absolute; 
    top: 0; 
    left: 0; 
    display: flex; 
    justify-content: center; 
} 

#content { 
    padding: 0px; 
    background: linen; 
    position: fixed; 
} 

enter image description here enter image description here

私の質問になります - どのように中央に「位置:固定Safariの "display:flexbox"親の要素ですか?

答えて

1

要素がposition: fixed(またはposition: absolute)の場合、SafariではChrome/Firefoxと同じように動作しません。あなたが使用する必要がSafariでフレックス項目を中央に

スタックが

#container { 
 
    width: 100%; 
 
    background: yellow; 
 
    height: 20px; 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
    display: flex; 
 
    justify-content: center; 
 
} 
 

 
#content { 
 
    padding: 0px; 
 
    background: linen; 
 
    position: fixed; 
 
    left: 50%; 
 
    transform: translateX(-50%); 
 
}
<div id="container"> 
 
    <div id="content">THIS CONTENT IS NOT CENTERED IN SAFARI</div> 
 
</div>

+0

うんスニペットtransform: translate

Updated codepen

、これは安い、有効な解決策のように見えます。私はSafariとChromeでこれがなぜ違うのだろうと思っています - それは意図的なもの(CSS仕様の解釈の違いなど)かバグだけですか? – shabunc

+1

@shabunc Flexboxモデルでは変更されていますが、一部のブラウザではまだまだキャッチアップされていません。ここでは、徹底的な説明https://stackoverflow.com/questions/32991051/absolutely-positioned-flex-item-is-not-removed-from-normal-flow-in-firefox-ie1 ...同様に資格を与えるかもしれない重複して – LGSon

関連する問題