Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center a <h2>?

Tags:

html

css

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?

like image 803
Rick van Baalen Avatar asked Jan 03 '17 08:01

Rick van Baalen


People also ask

How do you center an h2?

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.

How do I center H tag in CSS?

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.

How do you center the middle of an element?

Center Align Text To just center the text inside an element, use text-align: center; This text is centered.

How do you center a h3 in HTML?

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.


1 Answers

h2.headertekst {
  text-align: center;
}
<h2 class="headertekst">Test 1</h2>
like image 95
aavrug Avatar answered Oct 12 '22 20:10

aavrug