HTML वेब फॉन्ट्स
In this page:
<style>
@font-face {
font-family: "FontName";
src: url("font-file.woff2") format("woff2");
}
body {
font-family: "FontName", fallback-font;
}
</style>
वेब फॉन्ट्स क्या हैं?
web fonts typeface files हैं जिन्हें browser network से download करके page के text पर लागू करता है, इसके बजाय कि वह visitor के device पर पहले से installed fonts पर निर्भर रहे। इससे designers ऐसी distinctive typography इस्तेमाल कर सकते हैं जो दुनिया में हर visitor के लिए एक जैसी दिखे।
Google Fonts सबसे लोकप्रिय free web font service है, जिसे दुनिया भर की लाखों websites Roboto जैसे तेज़, भरोसेमंद तरीके से hosted typefaces देने के लिए इस्तेमाल करती हैं।
उदाहरण: What Are Web Fonts?
<link href="https://fonts.googleapis.com/css2?family=Roboto" rel="stylesheet">
<p style="font-family: 'Roboto', sans-serif;">Styled with a web font</p>
@font-face से एक कस्टम फॉन्ट लोड करना
@font-face rule आपको अपने खुद के server पर hosted font files की ओर पॉइंट करके अपनी खुद की font family define करने देता है। एक बार declare हो जाने पर, उस family name को किसी system font की तरह ही अपनी stylesheet में कहीं भी इस्तेमाल किया जा सकता है।
उदाहरण: Loading a Custom Font with @font-face
<style>
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2');
}
p {
font-family: 'MyFont', sans-serif;
}
</style>
फॉन्ट फॉर्मेट्स और फॉलबैक्स देना
हर browser एक जैसे font formats support नहीं करता, इसलिए एक ही @font-face rule के अंदर कई formats लिस्ट करना और अपनी font-family list को हमेशा sans-serif या serif जैसे generic fallback के साथ खत्म करना अच्छी practice है।
उदाहरण: Providing Font Formats and Fallbacks
<style>
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2'),
url('myfont.woff') format('woff');
}
body {
font-family: 'MyFont', sans-serif;
}
</style>
font-display से फॉन्ट लोडिंग कंट्रोल करना
font-display property यह control करती है कि custom font अभी भी download हो रहा हो तो browser text को कैसे handle करे, जिससे आप तुरंत fallback text दिखाने या custom font के लिए थोड़ा इंतज़ार करने के बीच चुनाव कर सकते हैं।
उदाहरण: Controlling Font Loading with font-display
<style>
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2');
font-display: swap;
}
</style>
परफॉर्मेंस के लिए वेब फॉन्ट्स ऑप्टिमाइज़ करना
चूँकि font files extra network requests जोड़ती हैं, इसलिए अपनी font selection को lean रखना और compressed formats इस्तेमाल करना, page के जल्दी पढ़ने लायक महसूस होने की speed बेहतर बनाने का एक सबसे आसान तरीका है।
उदाहरण: Optimizing Web Fonts for Performance
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400" rel="stylesheet">
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- HTML Keyboard Shortcuts
- HTML Browser Support
- HTML Character Sets
- HTML Doctypes
- HTML Audio/Video Reference
- HTML Meta Tags
- HTML ARIA Roles
- HTML Input Validation
- HTML Iframe Security
- HTML Responsive Images
- HTML Web Fonts
- HTML Icon Fonts
- HTML Progress & Meter
- HTML Output Element
- HTML Datalist
- HTML Image Maps
- HTML Browser DevTools
- HTML Validation
- HTML Quiz
- HTML Interview Prep