HTML Heading 2 Element

HTML Heading 2 <h2> tag is used to define heading of type 2 in an HTML document.

Example

A simple Heading 2 element is shown in the following example.

index.html

</>
Copy
<!DOCTYPE html>
<html>
    <body>
        <h2>Hello World</h2>
    </body>
</html>

Note: HTML Heading 2 element starts with the tag <h2> and ends with an end tag </h2>.

Default CSS for HTML <h2>

By default, following CSS properties are set for a Heading 2 element.

</>
Copy
display: block;
font-size: 1.5em;
margin-block-start: 0.83em;
margin-block-end: 0.83em;
margin-inline-start: 0px;
margin-inline-end: 0px;
font-weight: bold;

Inline Style for HTML Heading 2 Element

We can change the style of Heading 2 element through inline styling using style attribute.

In the following example, we have set the color as red for the Heading 2 element.

index.html

</>
Copy
<!DOCTYPE html>
<html>
    <body>
        <h2 style="color:red;">Hello World</h2>
    </body>
</html>

Apply CSS for HTML Heading 2 Element

We can apply CSS for all Heading 2 elements using the h2 tag.

index.html

</>
Copy
<!DOCTYPE html>
<html>
    <head>
        <style>
            h2 {
              color:red;
            }
          </style>
    </head>
    <body>
        <h2>Hello World</h2>
    </body>
</html>

Conclusion

In this HTML Tutorial, we learned about HTML <h2> tag and went through different examples that cover defining an HTML Heading 2 element, and styling it.