HTML | DOM Style fontSize Property

The fontSize property is used to set or get the font size of characters in a word should appear.

Syntax:

  • It returns the fontSize property.

    object.style.fontSize
    
  • It sets the fontSize Property.

    object.style.fontSize = "value|initial|inherit"
    

Property Values:

Value Description
xx-small
x-small
small
medium
large
x-large
xx-large
Predefine sizes of font
smaller Decreases by one relative unit of the font-size
larger Increases by one relative unit of the font-size
length Font-size in length unit
% % of the parent element’s font size
initial Set default value
inherit Inherit property from its parent value

Return value: It returns the font size of the text of element.

Example-1: Change font size into small.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>DOM Style fontSize Property </title>
</head>
  
<body>
    <center>
        <p style="color: green;
                  width: 100%;
                  font-size: 30px; 
                  font-weight: bold;" 
           id="Geek1">
            GeeksForGeeks
        </p>
  
        <h2>DOM Style fontSize Property </h2>
        <br>
        <button type="button" onclick="myGeeks()">
            Click to change
        </button>
  
        <script>
            function myGeeks() {
                  
                
                //  Change font size in to small.
                document.getElementById(
               "Geek1").style.fontSize = "small";
            }
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:

  • Before click on button:
  • After click on button:

Example-2: Change font-size into xx-large.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>DOM Style fontSize Property </title>
</head>
  
<body>
    <center>
        <p style="color: green; 
                  width: 100%; 
                  font-size: 10px; 
                  font-weight: bold;" 
           id="Geek1">
            GeeksForGeeks
        </p>
  
        <h2>DOM Style fontSize Property </h2>
        <br>
        <button type="button" onclick="myGeeks()">
            Click to change
        </button>
  
        <script>
            function myGeeks() {
                
                //  change into xx-large.
                document.getElementById(
                  "Geek1").style.fontSize = "xx-large";
            }
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:

  • Before click on button:
  • After click on button:

Example-3: Change font-size using length unit.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>DOM Style fontSize Property </title>
</head>
  
<body>
    <center>
        <p style="color: green;
                  width: 100%;
                  font-size: 10px; 
                  font-weight: bold;"
           id="Geek1">
            GeeksForGeeks
        </p>
  
        <h2>DOM Style fontSize Property </h2>
        <br>
        <button type="button" onclick="myGeeks()">
            Click to change
        </button>
  
        <script>
            function myGeeks() {
                
                //  Change font size from 
                //  from 10px to 30px
                document.getElementById(
                  "Geek1").style.fontSize = "30px";
            }
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:

  • Before click on button:
  • After click on button:

Example-4: Change font-size using ‘%’

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>DOM Style fontSize Property </title>
</head>
  
<body>
    <center>
        <p style="color: green;
                  width: 100%; 
                  font-size: 10px; 
                  font-weight: bold;"
           id="Geek1">
            GeeksForGeeks
        </p>
  
        <h2>DOM Style fontSize Property </h2>
        <br>
        <button type="button" onclick="myGeeks()">
            Click to change
        </button>
  
        <script>
            function myGeeks() {
                
                //  Change font-size from 100% to 200%
                document.getElementById(
                  "Geek1").style.fontSize = "200%";
            }
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:

  • Before click on button
  • After click on button

Supported Browsers: The browser supported by HTML | DOM Style fontSize Property are listed below:

  • Google Chrome
  • Internet Explorer
  • Mozilla firefox
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.