JavaScript - Change the font size of button
In this tutorial, we are going to learn about how to change the font size of a button using JavaScript.
Consider, that we have the following button
element in our HTML:
<button id="btn">Login</button>
Now, we want to change the above button font size using JavaScript.
Changing the button font size
To change the font size of a button, first we need to access the button
element object inside the JavaScript using the document.getElementById()
method.
const btn = document.getElementById("btn");
Now, set its style.fontSize
property to your desired size (eg: 10px or 20px or 25px, etc).
btn.style.fontSize = "18px";
It changes the font size of a button element to 18px
.
Full example:
<button id="btn">Login</button>
JavaScript:
const btn = document.getElementById("btn");
btn.style.fontSize = "18px"; // changes the font size to 18px