My goal is to center the text within the background image, keeping it centered on all devices. Initially, I used a fixed pixel margin-top value, but I need it to be a percentage so that it remains centered as the screen size changes. However, when I changed the margin to a percentage, the text would shift upwards when the screen was minimized. I want the text to always stay 50% from the top of the background picture.
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;
}