This webpage content is a comprehensive guide on CSS web fonts, colors, and related properties, offering practical examples, best practices, and interactive learning resources for frontend developers.
Abstract
The provided web content is Chapter 18 of a complete frontend developer textbook for beginners, focusing on CSS properties related to web fonts and colors. It explains how to set font families, sizes, styles, variants, weights, and line heights, with an emphasis on using web fonts like those available from Google Fonts. The text introduces the concept of relative units and the importance of defining font properties at the root level for cascading effects. It also covers the color property, the use of the currentColor keyword, and the significance of creating color themes. Interactive quizzes, coding exercises, and flashcards are offered to reinforce learning, with progress tracking for a full course curriculum. The chapter concludes with a preview of the next unit, which will delve into CSS Flexbox and JavaScript functions.
Opinions
The text advocates for the use of web fonts for consistent typography across different user systems.
It emphasizes the importance of specifying a list of font-family names, from most specific to most generic, to ensure fallbacks if the preferred font is not available.
The author suggests that using relative units for font sizes (like percentages, ems, or rems) is beneficial for responsive design and accessibility.
The use of the currentColor keyword is presented as a powerful and efficient way to maintain color consistency across elements without repetitive code.
The text encourages the use of interactive learning tools, such as quizzes and coding exercises, to enhance understanding and retention of the material.
The author expresses enthusiasm for the upcoming unit on CSS Flexbox and JavaScript, hinting at the practical applications and interactive features that learners will develop.
There is a call to action for readers to join Medium using the author's referral link, with the added incentive that membership fees will support the construction of a campus for children in Ukraine and Cambodia.
Chapter 18: CSS Web Fonts and Colors
A Complete Frontend Developer Textbook for Beginners (2023 Edition)
To set a font for the entiredocument, it’s customary to declare it at the root level:
html {
}
The font is like metadata that describe the overall document, so this makes sense. Moreover, by declaring it at the highestlevel of the HTML Tree, all the children and their descendants inherit all the fontproperties by default.
There are sixproperties that affect the font. Let’s begin with the font-family.
Font Family
html {
font-family:
}
The most genericfontfamilies are serif and sans-serif.
The default is serif:
html {
font-family: serif;
}
As you can see, the maincharacteristic of this family is the pointy edges of each letter.
Now, compare this with sans-serif:
html {
font-family: sans-serif;
}
As you can see, this family has no pointy edges. Consequently, each letter is nice and smooth. As a matter of fact, sans is a Latin word that means without.
serif= with pointy edges
sans-serif= without pointy edges
There are many fonttypefaces that make use of these serifs:
To recap, the best practice is to specify the font-size as an absoluteunit at the root of the HTML Tree. Relative to this font-size at the highest ancestor, we specify the font-size of all the descendants in relativeunits.
Structured in this responsive and accessible way, one simple change at the toptricklesdown to all the descendants automatically.
Font Shorthand
Let’s now look at the remainingfont properties.
By default, font-style is set to normal:
html {
font-family: Roboto, Geneva, sans-serif;
font-size: 10vw;
font-style: normal;
}
If we want, we can change it to:
html {
font-family: Roboto, Geneva, sans-serif;
font-size: 10vw;
font-style: italic;
}
By default, font-variant is set to normal as well:
html {
font-family: Roboto, Geneva, sans-serif;
font-size: 10vw;
font-style: italic;
font-variant: normal;
}
If we want, we can change it to:
html {
font-family: Roboto, Geneva, sans-serif;
font-size: 10vw;
font-style: italic;
font-variant: small-caps;
}
By default, font-weight is also set to normal:
html {
font-family: Roboto, Geneva, sans-serif;
font-size: 10vw;
CSS will keep looking up the HTML Tree until it finds a color property. If it reaches the root tag without finding a color property, it will use the defaultcolor of black.
Let’s prove this by defining a color property on the body:
body {
color: green;
}
As you can see, it works!
To drive this point home, let’s try to change the color of a to green as well by using currentColor. We can achieve this using the :linkpseudo-class:
a:link {
color: currentcolor;
}
Awesome!
Again, this works because the a tag is a descendant of the body tag where the color property is defined.
Why is having the keyword currentColor useful?
currentColor acts like a variable that contains the value of the color property. And we can use it on anydescendants.
By simply changing the color here at the top of the HTML Tree in only one location:
body {
color: red;
}
We can quickly change allthe colors of the descendants in the HTML Tree.
In effect, this makes creating color themes effortless. CSSvariables like currentColor are a very powerful concept which we will explore in great detail in Unit 4.
The style, variant, and weight must appear before the size.
The line-height must appear immediatelyafter the size separated by a slash.
The family must appear last.
At a minimum, the family and size must be specified.
Lastly, we looked at a special keyword called currentColor which acts like a variable that points to the value of the color property of the nearestancestor.
By changing just the value of the color property, all descendants that use the variablecurrentColor will inherit it automatically.
<div style="color:blue; border: 1px dashed currentColor;">
The color of this textis blue.
<div style="background:currentColor; height:9px;"></div>
This block is surrounded by a blue border.
</div>
Consequently, this makes creating color themeseffortless!
Concept Quiz
Take my Programming Concept Quiz to check your understanding! For every correct choice, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!
Sample Quiz Questions for Lesson 18:
Question 1:
A best practice is to declare defaultfont properties at what level of the HTML Tree?
html
body
main
Question 2:
Which font-family has pointy edges?
serif
sans-serif
Question 3:
Which font-family has smooth edges?
serif
sans-serif
Question 4:
It is a best practice to always include a genericfont-family.
True
False
Question 5:
A web font is NOT guaranteed to be supported everywhere.
True
False
Question 6:
To use a web font, we need to @import it.
True
False
Question 7:
Which is NOT an absolute unit?
px
pt
xx-large
vw
Question 8:
Which is NOT a relative unit?
%
em
rem
vh
xx-small
Question 9:
50% is equivalent to:
50em
5em
0.5em
Question 10:
Which unit is relative to the font-size of the root element?
em
rem
Question 11:
Which unit is relative to the font-size of the parent element?
em
rem
Question 12:
In the shorthand, which font property cannot appear before the font-size?
font-style
font-variant
font-weight
font-family
Question 13:
In the shorthand, the line-height must appear immediatelyafter the font-size separated by a slash.
True
False
Question 14:
In the shorthand, which font property is NOT required?
font-size
font-family
line-height
Question 15:
The variablecurrentColor is linked to which property?
background-color
border-color
color
Question 16:
Changing the value of the color property will affect which tags that use currentColor?
all ancestors above the color property
all descendants below the color property
Question 17:
The variablecurrentColor points to the value of the color property of the nearest:
Check out my Interactive Coding Exercises to put to practice what you have learned! There, you will also find interactive hints to help you understand each line of code. Likewise, for every correct solution, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!
Review what you have learned by playing my Syntax Flashcard Game! These flashcards are designed to help you commit to memory all the newcodesyntaxes you learned in this lesson. Likewise, for every correct answer, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!
Now that we know how to properlystructure and layout our HTML code, in Unit 3 our focus can shift to learning about the latestCSS syntax in depth. But like usual, HTML and JavaScript are interleaved to reinforce our understanding of their interrelationships.
Again, as a model and for inspiration, we will look at Flat’s beautifulmusic editor. Notice the menubar in the header, the musicscore in the main, and the virtualpiano in the footer. As we progress through this Unit, we will build these sections piece by piece. By the end, all three sections will be connected and interactive. For example, clicking on anypiano key will generate the correspondingmusical note on the score sheet instantly! Sounds awesome, right? To make it play the actual sound, however, you will need to wait until Unit 4: JavaScript Concerto 😆
Next Lesson
In the first lesson of this Unit, we will begin by creating the overalllayout of our music editor. To make this easy and responsive, we will use CSS Flexbox not only for our layout, but for aligning our elements and textNodes both horizontally and vertically. To start, we will apply these new concepts to perfectlydistribute and center our menu options. Finally, we will end by introducing the most important programming construct in JavaScript: the function. Get ready to write increasingly more JavaScript code going forward!
When you use my referral link above 👆 to become a Medium member, all proceeds will be donated towards the construction of the Silicon Wat Campus for children in Ukraine and Cambodia ❤️