CSS font-family Property

CSS font-family Property

CSS font-family property specifies the font for an element.

This property can have several fallback fonts in its value. These values are separated by commas to indicate that they are alternatives. The browser will select the first font in the list that is installed or that can be downloaded using a @font-face at-rule.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> p{ font-family: cursive; } </style> </head> <body> <h1>CSS font-family Property</h1> <p>This is cursive.</p> </body> </html>

Syntax

Using CSS

element{ font-family: cursive; }

Using Javascript

object.style.fontFamily="cursive";

Animatable

No, font-family property is not animatable. CSS Animatable Properties Reference.

Default Value

Default value for CSS font-family property is depends on the user agent (browser).

Property Value

The following table provides a list of values for CSS font-family property.

ValueExplanation
family-name
generic-name
Specifies the name of a font family. Refer the list of all font-family in this example program.

All in One

In the following example, we will demonstrate all values of CSS font-family property.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> p:nth-child(3){ font-family: serif; } p:nth-child(5){ font-family: sans-serif; } </style> </head> <body> <h1>CSS font-family Property</h1> <h2>font-family: serif;</h2> <p>This is font-family serif.</p> <h2>font-family: sans-serif;</h2> <p>This is font-family sans-serif.</p> </body> </html>

Demonstrating fallback fonts

In the following example, we will demonstrate how fallback fonts are useful.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> h1+p{ font-family: blabla, cursive; } </style> </head> <body> <h1>CSS font-family Property</h1> <p>This is blabla, cursive.</p> </body> </html>

Using JavaScript

In the following example, we will demonstrate how to change the CSS font-family property of an element using JavaScript.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <body> <h1>CSS font-family Property</h1> <button onclick="myFunction()">Click Me</button> <script> var x = document.getElementsByTagName("h1")[0]; function myFunction(){ x.style.fontFamily = "Verdana"; } </script> </body> </html>

Reminder

Hi Developers, we almost covered 98.7% of CSS Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in CSS.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Meet the Author