How to Add a Subtitle in HTML and JavaScript?

Estimated read time 1 min read

To add a subtitle in HTML and JavaScript, you can use the h2 element as a subheading for your page or section. Here’s an example of how to add an h2 element with a subtitle using both HTML and JavaScript:

HTML:

<body>
  <h1>Main Title</h1>
  <h2 id="subtitle">Subtitle</h2>
</body>

JavaScript:

// Get the subtitle element by its ID
var subtitle = document.getElementById("subtitle");

// Update the subtitle text content
subtitle.textContent = "New Subtitle";

In this example, we have an h1 element with the main title, and an h2 element with the subtitle. We give the h2 element an ID of “subtitle” so we can access it using JavaScript.

To update the subtitle text using JavaScript, we first use the document.getElementById() method to get the h2 element with the “subtitle” ID, and assign it to the subtitle variable. We then update the text content of the subtitle element using the textContent property.

You can customize the styles of the h2 element in your CSS file to change its appearance and position on your page.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply