Using Web Fonts
Using Web Fonts అంటే ఏమిటి?
Web Fonts అంటే visitor computer లో ముందుగా install చేయని font ను కూడా webpage లో ఉపయోగించడం. CSS లో సాధారణంగా @font-face rule ద్వారా web font ను define చేస్తాము.
@font-face ఎలా పనిచేస్తుంది?
@font-face లో మనం ఒక font family పేరు మరియు ఆ font file location ను చెబుతాము. ఆ తర్వాత font-family property ద్వారా ఆ font ను HTML text కు apply చేస్తాము.
సాధారణంగా web font files కు .woff2 format ఉపయోగిస్తారు. ఈ format చిన్న file size తో మంచి browser support ఇస్తుంది.
ముఖ్యమైన syntax
@font-face {
font-family: "MyWebFont";
src: url("fonts/my-web-font.woff2") format("woff2");
}
.title {
font-family: "MyWebFont", sans-serif;
}
ఇక్కడ MyWebFont అనేది మనం ఇచ్చిన పేరు. src లో font file location ఉంటుంది. ఆ font load కాకపోతే sans-serif fallback font గా పనిచేస్తుంది.
ఈ lesson లోని runnable example
Browser editor లో external font file అవసరం లేకుండా example run కావడానికి క్రింది code లో local() source ఉపయోగించాం. ఇది computer లో ఇప్పటికే ఉన్న font ను custom web-font family పేరు ద్వారా apply చేస్తుంది. నిజమైన website లో local source స్థానంలో project లోని .woff2 file path ను ఉపయోగించవచ్చు.
Web Fonts ఎందుకు ఉపయోగిస్తారు?
- Brand design కు సరిపోయే ప్రత్యేక font ను ఉపయోగించడానికి.
- అన్ని devices లో text appearance ను దగ్గరగా ఉంచడానికి.
- Website headings మరియు content కు మంచి visual identity ఇవ్వడానికి.
- Visitor system లో ఆ font install చేయకపోయినా font file ను browser load చేయడానికి.
Simple Explanation
ఒక font మన computer లో లేకపోయినా webpage లో కనిపించాలంటే web font ఉపయోగిస్తాము. ముందుగా @font-face తో font ను పేరు పెట్టి define చేయాలి. తర్వాత ఆ పేరును font-family లో ఉపయోగించాలి.
Font load కాకపోతే text పూర్తిగా కనిపించకుండా ఉండకూడదు. అందుకే sans-serif వంటి fallback font ను చివరలో ఉంచడం మంచిది.
Real-Life Example
ఒక restaurant తన logo మరియు menu కోసం ప్రత్యేకమైన brand font ఉపయోగిస్తుందని అనుకోండి. Customer computer లో ఆ font లేకపోయినా menu అదే style లో కనిపించాలి. Restaurant ఆ font file ను website తో పాటు అందించి, CSS లో @font-face ద్వారా ఆ font ను load చేస్తుంది.
Code Examples
Using a Custom Font Family with @font-face
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Web Fonts Example</title>
<style>
@font-face {
font-family: "DemoWebFont";
src: local("Trebuchet MS"), local("Arial");
}
body {
background-color: #f3f7fb;
margin: 30px;
}
h1 {
font-family: "DemoWebFont", sans-serif;
color: #123b63;
}
p {
font-family: "DemoWebFont", sans-serif;
color: #333333;
font-size: 18px;
}
</style>
</head>
<body>
<h1>Beautiful Web Typography</h1>
<p>This text uses a custom font family defined with @font-face.</p>
</body>
</html>
@font-face rule లో DemoWebFont అనే custom family name ను define చేశాం. local() browser లో ఉన్న font ను ఉపయోగిస్తుంది, కాబట్టి ఈ example కు external font file అవసరం లేదు. తర్వాత h1 మరియు p elements కు అదే custom family ను font-family ద్వారా apply చేశాం. Font అందుబాటులో లేకపోతే sans-serif fallback గా ఉపయోగించబడుతుంది.
ఒక light background పై dark blue heading మరియు gray paragraph కనిపిస్తాయి. Heading మరియు paragraph రెండూ DemoWebFont family ద్వారా apply చేసిన readable font style లో కనిపిస్తాయి.
Line-by-Line Explanation
@font-face rule ప్రారంభమవుతుంది. ఈ rule ద్వారా CSS లో ఒక custom font family ను define చేస్తాము.
Font కు DemoWebFont అనే పేరు ఇస్తుంది. తర్వాత ఇదే పేరును font-family property లో ఉపయోగిస్తాము.
Browser ముందుగా computer లోని Trebuchet MS font ను వెతుకుతుంది. అది లేకపోతే Arial ను ఉపయోగిస్తుంది. నిజమైన downloadable web font కోసం ఇక్కడ url(“fonts/my-web-font.woff2”) వంటి path ఇవ్వవచ్చు.
@font-face declaration ముగుస్తుంది.
అన్ని h1 headings కు styles ఇవ్వడానికి h1 selector ప్రారంభమవుతుంది.
h1 text కు DemoWebFont ను apply చేస్తుంది. ఆ font లభించకపోతే browser sans-serif fallback font ను ఉపయోగిస్తుంది.
Heading text కు dark blue రంగు ఇస్తుంది. ఇది web font concept కు అదనపు visual clarity కోసం ఉపయోగించిన style.
h1 style rule ముగుస్తుంది.
Paragraph elements కోసం CSS rule ప్రారంభమవుతుంది.
Paragraph text కు కూడా అదే custom font family ను apply చేస్తుంది.
Paragraph text size ను 18 pixels గా ఉంచుతుంది, దాంతో text సులభంగా చదవవచ్చు.
Paragraph style rule ముగుస్తుంది.
Try It
Try this CSS code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Try Web Fonts</title>
<style>
@font-face {
font-family: "PageFont";
src: local("Georgia"), local("Times New Roman");
}
h1,
p {
font-family: "PageFont", serif;
}
h1 {
color: #194d33;
}
</style>
</head>
<body>
<h1>My Font Practice</h1>
<p>Change the font family name or local font and test the result.</p>
</body>
</html>
Practice
Practice: పై practice_code లోని h1 మరియు p రెండింటికీ ReadingFont family ను apply చేయండి. Font load కాకపోతే sans-serif fallback ఉండేలా రాయండి.
Hint: h1 మరియు p rules లో font-family: “ReadingFont”, sans-serif; declaration ను చేర్చండి.
Common Mistakes
రెండు చోట్లా exact same font family name ఉపయోగించండి. ఉదాహరణకు రెండింటిలోనూ “ReadingFont” అని రాయండి.
src లో సరైన source ఇవ్వండి. ఉదాహరణకు src: local(“Arial”); లేదా project file కోసం src: url(“fonts/site-font.woff2”) format(“woff2”); ఉపయోగించండి.
Custom font తర్వాత sans-serif లేదా serif వంటి fallback font ను comma తో చేర్చండి.
HTML file కి సంబంధించి సరైన relative path ఇవ్వండి. File పేరు, folder పేరు మరియు .woff2 extension ను check చేయండి.
Interview Questions
@font-face rule ఎందుకు ఉపయోగిస్తారు?
Answer: Visitor computer లో install చేయని font ను webpage లో ఉపయోగించడానికి @font-face rule ఉపయోగిస్తారు.
@font-face లో font-family property ఏమి చేస్తుంది?
Answer: Custom font కు ఒక పేరు ఇస్తుంది. తర్వాత అదే పేరును ఇతర selectors లో font-family property ద్వారా ఉపయోగిస్తాము.
@font-face లో src property పని ఏమిటి?
Answer: Font source ఎక్కడ ఉందో src చెబుతుంది. అది local font కావచ్చు లేదా .woff2 వంటి font file URL కావచ్చు.
Fallback font అంటే ఏమిటి?
Answer: Custom font load కాకపోయినప్పుడు browser ఉపయోగించే alternative font. ఉదాహరణకు sans-serif లేదా serif.
Exam Questions
Web font ను define చేయడానికి ఉపయోగించే CSS rule ఏది?
Answer:
@font-face rule.
క్రింది code లో custom font ను paragraph కు ఎలా apply చేస్తారు? @font-face { font-family: “SiteFont”; src: local(“Arial”); }
Answer:
p { font-family: “SiteFont”, sans-serif; }
Web font file కు సాధారణంగా ఉపయోగించే మంచి format ఏది?
Answer:
.woff2 format.
Quick Revision
- @font-face ద్వారా custom web font ను define చేస్తాము.
- font-family లో custom font కు ఇచ్చిన exact name ను ఉపయోగించాలి.
- src property font source లేదా font file location ను సూచిస్తుంది.
- సాధారణ web font file format .woff2.
- Font load కాకపోతే sans-serif లేదా serif fallback ఇవ్వాలి.
- నిజమైన project లో font file path ను సరైన విధంగా check చేయాలి.
