<div>
com CSSmargin: 0 auto;
.caixa {
width: 300px;
margin: 0 auto;
}
.example2 {
display: flex;
justify-content: center; /* centraliza horizontal */
align-items: center; /* centraliza vertical */
height: 150px;
}
.caixa {
background-color: #e74c3c;
padding: 1rem 2rem;
border-radius: 8px;
color: white;
font-weight: bold;
}
.example3 {
display: grid;
place-items: center;
height: 150px;
}
.caixa {
background-color: #2ecc71;
padding: 1rem 2rem;
border-radius: 8px;
color: white;
font-weight: bold;
}
.example4 {
position: relative;
height: 150px;
}
.centro {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #9b59b6;
padding: 1rem 2rem;
border-radius: 8px;
color: white;
font-weight: bold;
}
Use Flexbox ou Grid sempre que possÃvel. São os métodos mais modernos, responsivos e fáceis de manter.
Evite usar position: absolute
como padrão, pois pode causar problemas em layouts responsivos.