Lesson 18 / الدرس 18

Fonts / الخطوط

Choose a typeface with font-family, then set its size, weight and style — and always give the browser a fallback.

اختر نوع الخط بـ font-family، ثم اضبط حجمه ووزنه ونمطه — وأعطِ المتصفح خطًا بديلًا دائمًا.

The font-family property picks the typeface. You do not give it one name — you give it a list, in order of preference. The browser walks the list and uses the first font it can actually find on the reader's device.

body {
    font-family: Georgia, "Times New Roman", serif;
}
This asks for Georgia first. If the device does not have it, Times New Roman; if it has neither, whatever serif font it does have. That last entry is the safety net.

Size, weight and style

font-size sets how big the text is — reach for rem, as lesson 14 covered. font-weight sets how heavy it looks, either by keyword (normal, bold) or by number from 100 to 900. And font-style: italic slants it.

h1   { font-size: 2rem; font-weight: 700; }
.note { font-style: italic; font-weight: 300; }
Change the numbers and watch the weight shift. Not every font has every weight — if it only ships regular and bold, asking for 300 gets you the closest one it has.

The font shorthand

The font shorthand sets several of these at once, but its order is strict: style, weight, then size, then family — and size and family are both required. Many people find the separate properties easier to read, and there is nothing wrong with using them.

p {
    font: italic 600 1.1rem Georgia, serif;
}
The same result as four separate declarations. Leave out the size or the family and the whole shorthand is thrown away — a quiet way to lose your styling.
PropertyExample valueWhat it does
font-familyGeorgia, serifWhich typeface, with fallbacks
font-size1.25remHow big
font-weight700How heavy — 100 to 900, or normal/bold
font-styleitalicUpright or slanted
fontitalic 600 1rem Georgia, serifAll of the above, in that exact order

The font properties you will use most often.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. Why does font-family take a list of names?

2. Which font name must be written in quotes?

3. What happens if the font shorthand leaves out the size?

Your task / مهمتك

Style a short book quote. Give the page a font-family with at least two fallbacks ending in a generic family, make the quote itself italic, and set the author's name in a heavier weight than the quote.

صمّم اقتباسًا قصيرًا من كتاب. أعطِ الصفحة font-family فيها بديلان على الأقل وتنتهي بعائلة عامة، واجعل الاقتباس نفسه مائلًا، واجعل اسم المؤلف بوزن أثقل من الاقتباس.

  • font-family lists at least three names, ending in a generic family font-family تسرد ثلاثة أسماء على الأقل وتنتهي بعائلة عامة
  • The quote uses font-style: italic الاقتباس يستخدم font-style: italic
  • The author's name uses a heavier font-weight اسم المؤلف يستخدم font-weight أثقل
How do you want to submit? / كيف تريد التسليم؟