← Back to CSS Course | Chapter 4: Text & Fonts | Lesson 6 of 10

CSS Custom Fonts

हर design हर device में पहले से मौजूद मुट्ठी भर 'web safe' fonts में fit नहीं होता। @font-face किसी page को आपके अपने server पर hosted एक पूरी तरह custom typeface load और इस्तेमाल करने देता है, ताकि page ठीक वही font render करे जो किसी designer ने चुना था, न कि visitor की device के पास जो भी substitute हो।
Syntax
css
@font-face {
  font-family: "CustomName";
  src: url("font-file.woff2") format("woff2");
}

selector {
  font-family: "CustomName", fallback-family;
}

@font-face Rule

@font-face एक या ज़्यादा font files की तरफ़ point करके और उस family को एक नाम देकर एक नया custom font family define करता है जिसे आप फिर किसी और font-family value की तरह reference कर सकते हैं। यह एक बार declare होता है, आमतौर पर किसी stylesheet के top के पास, किसी भी selector के उस नाम को इस्तेमाल करने से पहले।

Note:
  • Accepted values:
  • @font-face — एक custom font declare करता है
  • font-family — आपके चुने गए नाम
  • src — url("file.woff2") format("woff2"), या किसी installed font के लिए local()
  • font-display — auto | block | swap | fallback | optional
  • font-weight | font-style — file किस variant को provide करती है
  • unicode-range — file किन characters को cover करती है

उदाहरण: The @font-face Rule

css
<style>
@font-face {
  font-family: "MyFont";
  src: url("myfont.woff2");
}
p {
  font-family: "MyFont";
}
</style>
<p>Uses the custom font family defined above</p>

Font File Formats

Modern browsers मुख्य रूप से WOFF2 (सबसे छोटा, सबसे अच्छा compression) चाहते हैं, थोड़े पुराने browsers के लिए WOFF fallback के साथ; TTF/OTF आज web के लिए शायद ही कभी चाहिए होते हैं। एक ही @font-face block में कई src formats list करना browser को वह पहला format चुनने देता है जो वह असल में support करता है।

उदाहरण: Font File Formats

css
<style>
@font-face {
  font-family: "MyFont";
  src: url("myfont.woff2") format("woff2"),
       url("myfont.woff") format("woff");
}
</style>
<p>Browser picks the first format it supports</p>

@font-face में font-weight और font-style

एक अकेला font family अक्सर कई अलग files के रूप में ship होता है, हर weight/style combination (regular, bold, italic) के लिए एक। हर variant को matching font-weight/font-style values के साथ अपना @font-face block चाहिए ताकि बाद में किसी element पर font-weight: bold इस्तेमाल करना regular file को fake-bold करने के बजाय सही तरीके से bold file को select करे।

Note:
  • Accepted values:
  • normal | bold — keywords
  • 1 से 1000 — numeric weights, आमतौर पर 100 से 900
  • एक range — जैसे variable fonts के लिए 100 900
  • font-style: normal | italic | oblique

उदाहरण: font-weight and font-style in @font-face

css
<style>
@font-face {
  font-family: "MyFont";
  src: url("myfont-bold.woff2");
  font-weight: bold;
}
strong {
  font-family: "MyFont";
}
</style>
<strong>Loads the real bold file, not a fake bold</strong>

Custom Fonts के लिए Fallback Stacks

क्योंकि किसी custom font file को render होने से पहले download होना ज़रूरी है, हमेशा font-family में उसके बाद एक जैसा दिखने वाला system font (या sans-serif जैसी generic family) list करें, ताकि उस छोटे loading window के दौरान या download पूरी तरह fail होने पर भी text पढ़ने लायक बना रहे।

उदाहरण: Fallback Stacks for Custom Fonts

css
<style>
p {
  font-family: "MyFont", Arial, sans-serif;
}
</style>
<p>Falls back to Arial while MyFont loads, or if it fails</p>

font-display और Loading Behavior

font-display property (जैसे swap) control करती है कि किसी custom font के अभी भी download होने के दौरान text का क्या होता है — swap तुरंत fallback font दिखाता है और तैयार होने पर custom font को swap कर देता है, invisible text से बचते हुए और perceived performance बेहतर करते हुए।

Note:
  • Accepted values:
  • auto — browser तय करता है (initial value)
  • block — थोड़ी देर invisible text, फिर swap
  • swap — तुरंत fallback font, load होने पर swap
  • fallback — बहुत छोटा block, छोटा swap
  • optional — browser font को skip कर सकता है

उदाहरण: font-display and Loading Behavior

css
<style>
@font-face {
  font-family: "MyFont";
  src: url("myfont.woff2");
  font-display: swap;
}
</style>
<p style="font-family: 'MyFont', sans-serif;">Fallback shows immediately, swaps once ready</p>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. src को गलत file path पर point करना, इसलिए font चुपचाप default font पर fall back हो जाता है।
  2. font-family में कोई fallback font न देना, इसलिए file load होने के दौरान text गलत दिखता है।
  3. element में एक ऐसी font-weight इस्तेमाल करना जिसकी कोई matching font file नहीं है, इसलिए browser एक bold fake करता है।
ब्राउज़र सपोर्ट

हर modern browser में supported: Chrome, Firefox, Safari, Edge और Opera।

🔒

Chapter Quiz — Complete all 10 topics to unlock

0/10 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.