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.