I have an h2
header text with the class name "headertekst". I want to center it. I tried to use margin-left: auto
, margin-right: auto
and width: 100%
but nothing happened.
I can give it a margin-left: 20%
which will center it but that's just on my laptop. It will not be centered on other screen sizes and on mobile devices.
How do I center it the correct way?
Centering an HTML Element Type with the Text-Align Property For example, say you want the headings centered on a page, but the paragraphs to be left-aligned. Then you'd use the type selector h2 and set the text-align property to center.
To center align an h1 element, you can use the text-align property. All you need to do is apply text-align: center; on the h1 element and it will automatically center align the h1.
Center Align Text To just center the text inside an element, use text-align: center; This text is centered.
To center, align all objects with the <h3> tag applied, redefining it with styles, and then select the Block category. Select Center from the Text Align drop-down menu, as shown in Figure 15.6, and click the OK button. Immediately after you click OK, the h3 text in your Web page should jump to center alignment.
h2.headertekst {
text-align: center;
}
<h2 class="headertekst">Test 1</h2>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With