DIV を垂直方向と水平方向の中央に配置する方法はありますか。ただし、ウィンドウがコンテンツよりも小さい場合にコンテンツが切り取られないこと が重要です。div には背景色と幅と高さが必要です。
提供されている例のように、絶対位置と負のマージンでdivを常に中央に配置しました。ただし、コンテンツを上に切り取るという問題があります。この問題なく div .content を中央に配置する方法はありますか?
ここに再生する例があります: http://jsbin.com/iquviq/1/edit
CSS:
body { margin: 0px; }
.background {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
background-color: yellow;
}
/*
is there a better way than the absolute positioning and negative margin to center the div .content: div with background color a width and a hight?:
*/
.content {
width: 200px;
height: 600px;
background-color: blue;
position:absolute;
top:50%;
left:50%;
margin-left:-100px;/* half width*/
margin-top:-300px;/* half height*/
}
HTML:
<div class="background">
<div class="content"> some text </div>
</div>
私の質問は、「要素を水平方向および垂直方向に中央揃えにする方法」の複製ではありません。 1-私の質問は以前に尋ねられました。(日付を確認してください)。2-私の質問は非常に明確に、条件として黒で尋ねます:「ウィンドウがコンテンツよりも小さい場合、コンテンツはカットされません」