CSS
CSS: Which Property Is Used to Define the Font of Element Text?
Fonts play a crucial role in web design, influencing readability, aesthetics, and user experience. Whether you’re designing a professional website, a blog, or an application interface, choosing the right font can make all the difference.
So, which CSS property is used to define the font of an element’s text?
👉 The answer is: font-family
✅ The Property: font-family
The font-family
property in CSS specifies the typeface used for text in an HTML element. It allows you to choose between web-safe fonts, custom fonts, or fallback fonts.
📌 Syntax:
selector {
font-family: "Font Name", fallback-font, generic-family;
}
✅ Example 1: Set a Specific Font
p {
font-family: "Arial", sans-serif;
}
This sets the paragraph text to Arial. If Arial is not available, the browser will use a similar sans-serif font.
✅ Example 2: Multiple Fallback Fonts
body {
font-family: "Roboto", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
This ensures the best user experience across devices by using fallback fonts in order of preference.
✅ Example 3: Apply a Custom Web Font
To use custom fonts (like Google Fonts):
📌 HTML:
<link href="https://fonts.googleapis.com/css2?family=Open+Sans&display=swap" rel="stylesheet">
📌 CSS:
h1 {
font-family: 'Open Sans', sans-serif;
}
✅ Shorthand: font
You can also use the font
shorthand property to set multiple font-related properties at once:
p {
font: italic small-caps 16px/1.5 "Georgia", serif;
}
This includes:
font-style
font-variant
font-size
andline-height
font-family
⚠️ Always include at least the
font-size
andfont-family
when using shorthand.
🧾 Summary
Goal | CSS Property |
---|---|
Set the font type | font-family |
Add fallback fonts | Comma-separated list |
Use Google or custom fonts | Import and apply font-family |
Set all font properties at once | font (shorthand) |
🧠 Conclusion
The font-family
property is the core CSS property for defining the font used in your website’s text. It gives you control over the style and feel of your content while ensuring accessibility and compatibility across browsers.
Pro Tip: Always include a generic font family (like serif
, sans-serif
, or monospace
) at the end of your font stack as a safety net.