I have a page layout with some text-element inside a div, as shown below:
body {
background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url(../public/images/lake.jpg) no-repeat center center fixed;
background-size: cover;
font-family: 'inherit';
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
p,
q,
button {
position: absolute;
margin: 0 auto;
left: 50%;
/* transform: translateX(-50%); */
text-align: center;
}
q {
width: 90%;
top: 75px;
}
p {
width: 90%;
top: 150px;
}
<div>
<q>Lorem ipsum dolor sit amet consectetur adipisicing elit. Possimus vitae facere dolor cupiditate placeat quas.</q>
<p>
Lorem, ipsum dolor.
</p>
<button>New Quote</button>
</div>
I know the transform: translateX(-50%); will solve this problem and make the p, q, and button elements centered.
- But my question is why doesn't the
left: 50%property solve the case spontaneously, is it because the point of reference oftop/bottom/left/rightproperty is not a top left of thediv?- If yes, is there any way to modify the point of reference? how?
- If no, what is the reason?
- Is there any suggestion or source of study for best practice for centering a non-100% width element inside a
divelement?
