Categories
JavaScript Answers

How to Change Font Size with JavaScript?

Spread the love

To change font size with JavaScript, we can set the style.fontSize property of the element.

For instance, if we have the following HTML:

<span>hello</span>

Then we write the following JavaScript code:

const span = document.querySelector("span");
span.style.fontSize = "25px";

to select the span element and set the font size of its content.

We select the element with document.querySelector and assign the returned element to span.

Then we set span.style.fontSize to '25px' to set the font size of its content to 25px.

Now we should see larger text displayed.

Conclusion

To change font size with JavaScript, we can set the style.fontSize property of the element.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *