bg-textを外側のdiv内に配置するだけで、すべてのデバイスの中央にh1タグがある背景に画像が表示されます。私はもともとマージントップが固定ピクセルだった場所にこれを持っていました。私はパーセンテージにする必要があるので、センターにとどまります。しかし、私がマージントップをパーセンテージで置き換えたとき、最小限のスクリーンとしてh1エレメントが上にスライドします。私はマージントップ:私のバックグラウンド画像の50%を常に維持したい。div内のdivを背景画像で中心合わせする方法
HTML
<div class= 'bg'>
<div class='bg-text'>
<h1>Text</h1>
</div>
</div>
CSS
。
.bg {
display: table;
width: 100%;
height: 50%;
background-image: image-url('pic.jpg');
background-attachment: scroll;
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
}
.bg-text{
color: #fff;
text-align: center;
text-shadow: 1px 1px 12px rgba(0,0,0,0.5);
margin-top: 150px;
}
.bg-text h1{
font-size: 50px;
font-weight: 700;
}
[縦DIVでDIVをセンタリング]の可能な重複(http://stackoverflow.com/questions/9307566/vertically-center-align-a-div-within-anothe-div?s = 6 | 2.3735)とSOを検索して見つかった他のホスト – Rob
[divのdivを水平方向に中央に配置](http:// stackoverflow。com/questions/114543/horizontal-center-a-div-in-a-div) –