Javascript Reference - HTML DOM Style fontStyle Property








The fontStyle property sets or gets font style.

The style of the font can be normal, italic or oblique.

Browser Support

fontStyle Yes Yes Yes Yes Yes

Syntax

Return the fontStyle property:

var v = object.style.fontStyle 

Set the fontStyle property:

object.style.fontStyle='normal|italic|oblique|initial|inherit'

Property Values

Value Description
normal Default. normal font.
italic italic font
oblique oblique font
initial Sets to default value.
inherit Inherits from parent element.




Technical Details

Default Value: normal
Return Value: A string representing the font style
CSS Version CSS1

Example

The following code shows how to set the font style to "italic".


<!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  va  2s .c  om-->
    document.getElementById("myP").style.fontStyle = "italic";
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

A demonstration of various possible values for font style.


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

<select onchange="myFunction(this);">
  <option>normal</option>
  <option>italic</option>
  <option>oblique</option>
</select>
 
<script>
function myFunction(selectTag) {
    var listValue = selectTag.options[selectTag.selectedIndex].text;
    document.getElementById("myP").style.fontStyle = listValue;
}
</script>
</body>
</html>

The code above is rendered as follows:

Example 3

The following code shows how to get the font style.


<!DOCTYPE html>
<html>
<body>
<p id="myP" style="font-style:italic;">This is a paragraph.</p>
<button type="button" onclick="myFunction()">test</button>
 <!--from  w  w w.  j  av a 2 s  .co  m-->
<script>
function myFunction() {
    console.log(document.getElementById("myP").style.fontStyle);
}
</script>

</body>
</html>

The code above is rendered as follows: