Set the font for an element:
document.getElementById("demo").style.fontFamily = "Impact,Charcoal,sans-serif";
Try it Yourself »
The fontFamily property sets or returns a list of font-family names and/or 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:
Tip: Always specify a generic-family name as the last alternative!
Note: Separate each value with a comma.
Note: If a font-family name contains whitespace, it must be quoted.
Tip: Look at Web safe fonts for commonly used font combinations.
Property | |||||
---|---|---|---|---|---|
fontFamily | Yes | Yes | Yes | Yes | Yes |
Return the fontFamily property:
object.style.fontFamily
Set the fontFamily property:
object.style.fontFamily
= "font1, font2, etc.|initial|inherit"
Value | Description |
---|---|
font1, font2, etc. | A comma-separated list of font-family names and/or generic-family names |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
Default Value: | not specified |
---|---|
Return Value: | A String, representing the font name of the text in the element |
CSS Version | CSS1 |
A demonstration of possible values:
var listValue = selectTag.options[selectTag.selectedIndex].text;
document.getElementById("demo").style.fontFamily = listValue;
Try it Yourself »
Return the font of an element:
alert(document.getElementById("demo").style.fontFamily);
Try it Yourself »
CSS tutorial: CSS Font
CSS reference: font-family property
HTML DOM reference: font property
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!