The fontStyle
property sets or gets font style.
The style of the font can be normal, italic or oblique.
fontStyle |
Yes | Yes | Yes | Yes | Yes |
Return the fontStyle property:
var v = object.style.fontStyle
Set the fontStyle property:
object.style.fontStyle='normal|italic|oblique|initial|inherit'
Value | Description |
---|---|
normal | Default. normal font. |
italic | italic font |
oblique | oblique font |
initial | Sets to default value. |
inherit | Inherits from parent element. |
Default Value: | normal |
---|---|
Return Value: | A string representing the font style |
CSS Version | CSS1 |
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:
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:
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: