Home » Tech Tutorials » Web Development » CSS » CSS Text and Font Shorthand

CSS Text and Font Shorthand

CSS Text and Font Shorthand

CSSలో text appearance మార్చడానికి font-related properties ఉపయోగిస్తాము. font shorthand ద్వారా font-style, font-variant, font-weight, font-size, line-height మరియు font-family వంటి విలువలను ఒకే declaration లో రాయవచ్చు.

Font shorthand syntax

సాధారణ syntax:

font: font-style font-variant font-weight font-size/line-height font-family;

ఈ shorthandలో font-size మరియు font-family తప్పనిసరిగా ఉండాలి. మిగిలిన విలువలు optional. సాధారణంగా font-size ను font-family ముందు రాయాలి. line-height ఇవ్వాలంటే font-size తరువాత slash ఉపయోగించాలి.

ఉదాహరణ

font: italic 700 30px/1.3 Georgia, serif;
  • italic text style ను italic చేస్తుంది.
  • 700 text weight ను bold చేస్తుంది.
  • 30px font size ను నిర్ణయిస్తుంది.
  • 1.3 line-height ను నిర్ణయిస్తుంది.
  • Georgia, serif font family ను నిర్ణయిస్తుంది.

Shorthandలో ఏ optional value ఇవ్వకపోతే, ఆ value browser యొక్క default లేదా initial value కి reset కావచ్చు. అందువల్ల shorthand declaration రాసేటప్పుడు అవసరమైన values అన్నీ స్పష్టంగా ఇవ్వడం మంచిది.

Complete example

క్రింది ఉదాహరణలో heading మరియు paragraph కు font shorthand ఉపయోగించబడింది:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Font Shorthand Example</title>
  <style>
    body {
      margin: 24px;
      color: #222222;
    }

    h1 {
      font: 700 32px/1.2 Arial, sans-serif;
    }

    p {
      font: italic 400 18px/1.6 Georgia, serif;
    }
  </style>
</head>
<body>
  <h1>Typography Practice</h1>
  <p>This paragraph uses CSS font shorthand.</p>
</body>
</html>

ఇక్కడ h1 ఒకే lineలో bold font, 32px size, 1.2 line-height మరియు Arial font family పొందుతుంది. p italic style, normal weight, 18px size, 1.6 line-height మరియు Georgia font family పొందుతుంది.

Simple Explanation

Font shorthand అంటే fontకు సంబంధించిన అనేక CSS properties ను ఒకే lineలో రాయడం.

ఉదాహరణకు, ఈ declarations:

font-style: italic;
font-weight: 700;
font-size: 18px;
line-height: 1.6;
font-family: Georgia, serif;

ఇవే విషయాలను ఇలా చిన్నగా రాయవచ్చు:

font: italic 700 18px/1.6 Georgia, serif;

ఇది code ను చిన్నగా, చదవడానికి సులభంగా చేస్తుంది. Browser ఈ shorthand declaration ను విడిగా ఉన్న font properties లా అర్థం చేసుకుంటుంది.

Real-Life Example

ఒక magazine లేదా news card ను ఊహించండి. Headline పెద్దగా మరియు boldగా ఉండాలి. Article text కొంచెం చిన్నగా, readable line spacingతో ఉండాలి. ప్రతి property ను విడిగా రాయడం కంటే font shorthand ద్వారా ప్రతి text element యొక్క పూర్తి typography style ను ఒకే lineలో నిర్వచించవచ్చు.

Code Examples

Font shorthandతో heading మరియు paragraph styling

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Font Shorthand Example</title>
  <style>
    body {
      margin: 24px;
      color: #222222;
    }

    h1 {
      font: 700 32px/1.2 Arial, sans-serif;
    }

    p {
      font: italic 400 18px/1.6 Georgia, serif;
    }
  </style>
</head>
<body>
  <h1>Typography Practice</h1>
  <p>This paragraph uses CSS font shorthand.</p>
</body>
</html>

ఈ complete HTML documentలో h1 మరియు p elementsకు font shorthand ఉపయోగించబడింది. h1 కోసం bold Arial font, p కోసం italic Georgia font ఇవ్వబడింది. Slash తరువాత ఉన్న value line-height ను సూచిస్తుంది.

The browser displays a large bold Arial heading reading “Typography Practice” and an italic Georgia paragraph below it with comfortable line spacing.

Line-by-Line Explanation

h1 {
ఈ selector అన్ని h1 elementsను ఎంచుకుంటుంది. Heading textకు font shorthand వర్తింపజేయడానికి దీనిని ఉపయోగిస్తున్నాము.
font: 700 32px/1.2 Arial, sans-serif;
ఇది h1కు font shorthand declaration. 700 bold weightను, 32px font sizeను, 1.2 line-heightను, Arial మరియు fallbackగా sans-serif fontలను నిర్ణయిస్తుంది.
p {
ఈ selector అన్ని paragraph elementsను ఎంచుకుంటుంది.
font: italic 400 18px/1.6 Georgia, serif;
ఇది paragraph textను italicగా చేస్తుంది. 400 normal weightను, 18px sizeను, 1.6 line-heightను మరియు Georgia fontను నిర్ణయిస్తుంది. Georgia అందుబాటులో లేకపోతే serif fallback ఉపయోగించబడుతుంది.

Typography Practice


ఇది browserలో heading contentను చూపిస్తుంది. h1 selector కారణంగా ఈ textకు shorthandలో ఇచ్చిన font settings వర్తిస్తాయి.

This paragraph uses CSS font shorthand.


ఇది paragraph contentను చూపిస్తుంది. p selector కారణంగా ఈ text italic Georgia styleలో కనిపిస్తుంది.

Try It

Try this CSS code:

Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Try Font Shorthand</title>
  <style>
    .card-title {
      font: 700 28px/1.2 Arial, sans-serif;
    }

    .card-text {
      font: normal 400 17px/1.6 Georgia, serif;
    }
  </style>
</head>
<body>
  <h1 class="card-title">Learning CSS</h1>
  <p class="card-text">Font shorthand makes typography declarations shorter.</p>
</body>
</html>

Try It Yourself »

Practice

Practice: practice_codeలో p selector క్రింద ఉన్న ఐదు font declarationsను ఒకే font shorthand declarationగా మార్చండి. Browserలో paragraph style మారకుండా ఉందో పరీక్షించండి.

Hint: Orderను ఇలా ఉంచండి: font-style, font-weight, font-size/line-height, font-family. చివర semicolon పెట్టడం మర్చిపోవద్దు.

Common Mistakes

font shorthandలో font-size లేదా font-familyను వదిలివేయడం.
font shorthandలో font-size మరియు font-family తప్పనిసరి. ఉదాహరణకు font: bold 20px Arial, sans-serif; అని రాయండి.
font-size మరియు line-height మధ్య slash పెట్టకపోవడం.
line-height ఇవ్వాలంటే font-size/line-height రూపంలో slash ఉపయోగించాలి. ఉదాహరణ: 18px/1.5.
font-family పేర్ల మధ్య comma పెట్టకపోవడం.
Fallback fontsను commaతో వేరు చేయాలి. ఉదాహరణ: Georgia, serif.
font shorthand declaration చివర semicolon పెట్టకపోవడం లేదా font propertyను font-familyగా తప్పుగా రాయడం.
సరైన property పేరు font. Declaration చివర semicolon పెట్టండి: font: italic 18px Georgia, serif;
ముందు ఇచ్చిన font-weight లేదా font-style విలువ shorthandలో మళ్లీ ఇవ్వకపోవడం.
Shorthand కొన్ని omitted font valuesను reset చేయవచ్చు. అవసరమైన style, weight, size, line-height మరియు family valuesను స్పష్టంగా ఇవ్వండి.

Interview Questions

CSS font shorthand అంటే ఏమిటి?

Answer: Font-related propertiesను ఒకే declarationలో రాసే విధానాన్ని font shorthand అంటారు. ఇది codeను చిన్నగా మరియు సులభంగా నిర్వహించడానికి ఉపయోగపడుతుంది.

Font shorthandలో తప్పనిసరిగా ఉండాల్సిన values ఏవి?

Answer: font-size మరియు font-family తప్పనిసరిగా ఉండాలి. font-style, font-variant, font-weight మరియు line-height optional values.

Font shorthandలో line-heightను ఎలా రాస్తారు?

Answer: font-size తరువాత slash పెట్టి line-heightను రాస్తారు. ఉదాహరణ: font: 18px/1.5 Arial, sans-serif;

ఈ declarationలో 700 ఏమి సూచిస్తుంది: font: 700 30px Arial, sans-serif;?

Answer: 700 font-weightను సూచిస్తుంది. ఇది సాధారణంగా bold textను సూచిస్తుంది.

Exam Questions

font-style: italic; font-weight: 700; font-size: 24px; font-family: Arial, sans-serif; ను shorthandగా రాయండి.

Answer:

font: italic 700 24px Arial, sans-serif;

font: normal 400 16px/1.5 Georgia, serif; declarationలో 16px/1.5 ఏమి సూచిస్తుంది?

Answer:

16px font-sizeను, 1.5 line-heightను సూచిస్తుంది.

Font shorthand ఉపయోగించడం వల్ల కలిగే ఒక ప్రయోజనం ఏమిటి?

Answer:

అనేక font declarationsను ఒకే lineలో రాయడం వల్ల CSS code చిన్నగా మరియు నిర్వహించడానికి సులభంగా ఉంటుంది.

Quick Revision

  • font shorthand అనేక font propertiesను ఒకే declarationలో కలుపుతుంది.
  • font-size మరియు font-family తప్పనిసరి values.
  • font-size మరియు line-height మధ్య slash ఉపయోగించాలి.
  • సాధారణ order: font-style font-variant font-weight font-size/line-height font-family.
  • Fallback fontsను commaతో వేరు చేయాలి.
  • Shorthandలో omitted values default లేదా initial valueకు reset కావచ్చు.

Test Your Knowledge

Take a short quiz based on this lesson.

10 Questions • Results
Scroll to Top