Javascript Reference - HTML DOM Style fontSize Property








The fontSize property sets or gets the text font size.

Browser Support

fontSize Yes Yes Yes Yes Yes

Syntax

Return the fontSize property:

var v = object.style.fontSize 

Set the fontSize property:

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

Property Values

Value Description
xx-small
x-small
small
medium
large
x-large
xx-large
Set to fixed sizes, from xx-small to xx-large
smaller Make the font-size smaller by one relative unit
larger Make the font-size bigger by one relative unit
length Set the font-size in length units
% Set the font-size to a percent of the parent's font size
initial Set to default value.
inherit Inherits from parent element.




Technical Details

Default Value: medium
Return Value A string representing the font size
CSS Version CSS1

Example

The following code shows how to set the font size to "xx-large".


<!DOCTYPE html>
<html>
<body>
<p id="myP">This is a paragraph.</p>
<button type="button" onclick="myFunction()">test</button>
 <script>
function myFunction() {<!--from  w  w w . ja v a2s .  co m-->
    document.getElementById("myP").style.fontSize = "xx-large";
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

A demonstration of various possible values:


<!DOCTYPE html>
<html>
<body>
<!--from   w ww.  ja va 2 s.  c  o m-->
<p id="myP">This is a paragraph.</p>

<select onchange="myFunction(this);" size="11">
  <option>xx-small</option>
  <option>x-small</option>
  <option>small</option>
  <option>medium</option>
  <option>large</option>
  <option>x-large</option>
  <option>xx-large</option>
  <option>100%</option>
  <option>250%</option>
  <option>2cm</option>
  <option>100px</option>
</select>
 
<script>
function myFunction(selectTag) {
    var listValue = selectTag.options[selectTag.selectedIndex].text;
    document.getElementById("myP").style.fontSize = listValue;
}
</script>

</body>
</html>

The code above is rendered as follows:

Example 3

The following code shows how to get the font size.


<!DOCTYPE html>
<html>
<body>
<p id="myP" style="font-size:200%">This is a paragraph.</p>
<button type="button" onclick="myFunction()">test</button> 
<script>
function myFunction() {<!--  w w w.  ja va2s  . c  o m-->
    console.log(document.getElementById("myP").style.fontSize);
}
</script>

</body>
</html>

The code above is rendered as follows: