There are several techniques, the two that I like the most are the ones I'm going to quote because they do not depend on fixed size.
Technique 1 with transform:translate
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
background: url(https://www.placecage.com/c/500/500) no-repeat center;
background-size: cover;
}
.centro {
height: 100px;
width: 100%;
background-color: rgba(0, 0, 0, .5);
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.conteudo {
height: 50px;
width: 50px;
background-color: red;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
<div class="centro">
<div class="conteudo"></div>
</div>
Technique 2, this one I think is very good, because it uses display:flex
and you can make margin:auto
work both width and height!
html, body {
width: 100%;
height: 100%;
display: flex;
margin: 0;
padding: 0;
background: url(https://www.placecage.com/c/500/500) no-repeat center;
background-size: cover;
}
.centro {
margin: auto;
background-color: rgba(0, 0, 0, .5);
height: 100px;
width: 100%;
display: flex;
}
.conteudo {
height: 50px;
width: 50px;
background-color: red;
margin: auto;
}
<div class="centro">
<div class="conteudo"></div>
</div>
[]'s