Can I calculate with width of <h1>
(depends on text) and center this <h1>
horizontally in <body>
?
Thanks for any help and hint!
Robin Zigmond is correct in their comment. You can center a block-level element by setting it's margin-left
and margin-right
to auto
. It will appear centered if it's width is not 100% of it's parent container:
h1 {
margin-left: auto;
margin-right: auto;
}
If its width is 100% of its parent container, you could use text-align
to set the text to center
:
h1 {
text-align: center;
}
did not move. `position: absolute; top: 20px; margin-left: auto; margin-right: auto;`
– John Doe May 18 '19 at 21:17