I am trying to place some text at the bottom-center of an html page but it is not aligned.
main {
position: relative;
text-align: center;
max-width: 240px;
margin: 0 auto;
}
.bottom {
position:fixed;
bottom:0;
}
<main>
<div>
<h1>Hello</h1>
</div>
<div class=bottom>
<span>Bottom text</span>
</div>
</main>
I have tried various options including position:absolute; (text not at bottom) and margin: 0 auto; in bottom css (no effect)
What am I missing?
4
Answers
Add
left: 50%;
andtransform: translateX(-50%);
tobottom
to center it.left: 50%;
moves the left edge of the element to the middle of its container andtransform: translateX(-50%);
moves it back to the left by 50% of its own width – those two in combination center it horizontally.Try something like this:
Ensure that the bottom spans the full width of its parent container, occupying 100% of the available horizontal space. When setting its position to fixed at the bottom of its relative parent, make sure the starts from the left corner and extends horizontally to cover the entire width of the parent container.
Using
display: flex
(makes it independent fromtext-align: center
onmain
):