Style fontFamilyProperty

Style 对象参考手册Style Object

Definition and Usage

The fontFamily property sets or returns a list of font family names/generic family names for text in an element.

The browser will use the first value it recognizes.

There are two types of font family values:

  • font-family: A font family name, such as "verdana" or "arial".
  • generic-family: A generic family name, such as "serif" or "sans-serif".

Tip:Always specify a generic family name as the last choice!

Note:If the font family name contains spaces, enclose it in quotation marks.

Tip:SeeWeb Safe Fontsfor common font combinations.

Syntax

Set the fontFamily property:

Object.style.fontFamily="font1, font2, etc.|inherit"

Return the fontFamily property:

Object.style.fontFamily

Value Description
font1, font2, etc. A comma-separated list of font family names/generic family names.
inherit The value of the fontFamily property is inherited from the parent element.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the fontFamily property.

Note:IE7 and earlier versions do not support the "inherit" value. IE8 supports "inherit" only if a !DOCTYPE is specified. IE9 supports "inherit".


Example

Example

Change the font of text:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example Tutorial(example.com)</title>
<script>
function displayResult(){
    document.getElementById("p1").style.fontFamily="Impact,Charcoal,sans-serif";
}
</script>
</head>
<body>

<p id="p1">This is some text.</p>
<br>
<button type="button" onclick="displayResult()">Change font</button>

</body>
</html>

Try it yourself »


Style 对象参考手册Style Object Other Extensions