Home > Chapter Review and Exercises > Chapter 12 Formatting Text
Typography - Lesson 1 (here is a PDF version of the lesson plan)
The reading assignment in Lesson 1 is available here as a PDF.
Your Assignment: Complete this form
The due date for this assignment is Friday, 20 December. We will discuss this lesson when you return from Christmas Break.
This dense chapter covers the properties related to manipulating the appearance of text, including fonts, text color, text line alignment, decorations (like underlines) and list styles. In addition, we’ll add Descendant, ID, and Class Name selectors to our selector toolkit, and take a more detailed look at specificity.
In Chapter 12, you will learn the following:
The properties related to specifying the font for a text element, primarily font-family, font-size, font-weight, font-style, font-variant, and the shorthand font property.
That fonts must be installed on the user’s machine or downloaded as a web font in order to be used. You can specify a list of preferred fonts, ending with a generic font family, to be used as backups.
The most widely used units for text size are rem and em. Pixels are also used, but are less flexible.
An introduction to some of the advanced font features introduced in the CSS Font Module Level 3.
Changing text color with the color property.
New selector types: descendant selectors (targeting elements only when they are contained within certain elements), ID, class, and universal selectors.
How to calculate specificity by totaling up the number of IDs, classes, and elements in the selector. More specific selectors will override less specific selectors when there are conflicting styles applied to an element.
Properties that affect whole lines of text, including line height (line-height), indents (text-indent), horizontal alignment (text-align).
Other text properties such as underlining (text-decoration), changing capitalization (text-transform), letter spacing (letter-spacing), word spacing (word-spacing), and adding shadows (text-shadow).
Properties for changing the presentation of bulleted and numbered lists (list-style-type, list-style-position, list-style-image).
typeface
A particular design of glyphs (characters) that share common design features, for example Baskerville. Also known as a font family.
font
One particular instance of a typeface that has a specific weight, style, condensation, slant, and so on. For example, Baskerville Bold Italic is one font of the typeface Baskerville. On computers, fonts are usually stored in different files.
rem
Root em unit; it is equivalent to the font size of the html element.
em
Em unit; it is equivalent to the font size of the current element.
posture
Whether the font is vertical or slanted (italic or oblique).
contextual selector
Selects elements based on their relationship to other elements (includes descendant, child, next-sibling, and subsequent-sibling selectors).
id selector
Selects an element by the value of its id attribute (indicated by the # symbol).
class selector
Selects an element or elements based on the value of their class attributes (indicated by the period [.] symbol).
universal selector
Selects any element, like a wildcard in programming languages.
12-1: Formatting a menu
A menu for the Black Goose Bistro will be embellished with text styles throughout the chapter as new properties are introduced. In this first exercise, all the text is set to the web-safe Verdana font, and the main heading is specified with a web font called Marko One.
12-2: Setting font size
In this step, the font size of the body text and headings is refined.
12-3: Making text bold and italic
This quick exercise formats list terms in bold and strong text in italic styles.
12-4: Using the shorthand font property
Here we merely convert a stack of font declarations into one declaration using the shorthand font property.
12-5: Using selectors
This exercise gives students a chance to try out descendent, ID, and class selectors to target specific elements in the bistro menu.
12-6: Finishing touches
Several more styles are added to the menu to practice using color, line-height, alignment, capitalization, shadow, as well as some new selector types.
It’s time to see how well you understand the font properties and selectors introduced in this chapter. Check Appendix A for the answers if you get stuck.
1. Match the style property with the text samples in FIGURE 12-24.
a. _______ {font-size: 1.5em;}
b. _______ {text-transform: capitalize;}
c. _______ {text-align: right;}
d. _______ {font-family: Verdana; font-size: 1.5em;}
e. _______ {letter-spacing: 3px;}
f. _______ {font: bold italic 1.2em Verdana;}
g. _______ {text-transform: uppercase;}
h. _______ {text-indent: 2em;}
i. _______ {font-variant: small-caps;}
2. Here is a chance to get a little practice writing selectors. Using the diagram shown here, write style rules that make each of the elements described here red (color: red;). Write the selector as efficiently as possible.
a. All text elements in the document
b. h2 elements
c. h1 elements and all paragraphs
d. Elements belonging to the class special
e. All elements in the “intro” section
f. strong elements in the “main” section
g. Extra credit: just the paragraph that appears after an h2
Near the end of Chapter 12, several additional selectors are introduced. The basic syntax is shown below.
1. The element sector, i.e. p { color:navy; }, applies the property to the p element only.
2. The grouped selectors, i.e., p, ul, td, th { color: navy; }, apply the properties to several elements at once. The elements are separated by commas.
3. Contextual selectors targets an element based on its context or relation to another element. Here are four of them.
A descendant selector targets elements that are contained within (and therefore are descendants of) another element. Descendant selectors are indicated in a list separated by a character space. For example,
li em { color: olive; }
targets emphasized text (em) elements, but only when they appear in list items (li).
Three more contextual selectors are the child selector, the next-sibling selector, and the subsequent-sibling selector.
A great explanation and demonstration of these four contextual selectors is here:
https://www.w3schools.com/css/css_combinators.asp
3. The id selectors use the id attribute to to apply a rule for just that list item using an id attribute.
The id attribute gives an element a unique identifying name (its id reference), for example:
<li id="sleestak">Sleestak T-shirt</li>.
You can write a style rule just for that list item using an ID selector, like so :
li#sleestak { color: olive; }.
Notice the # preceding the id reference
4. Class selectors.
Previously we have learned that the the class identifier is used to classify elements into a conceptual group.
Unlike the id attribute, multiple elements may share a class name.
You can target elements belonging to the same class with a class selector.
Class names are indicated with a period (.) at the beginning of the selector.
For example,
p.special { color: orange; }
targets all paragraphs with class="special".
(From 18 December 2018)
Using an online HTML and CSS demonstrator, enter this HTML:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Classes tied and not tied to an element</h1>
<p>Define a particular class for only one type of element<br>
p.special {<br>
}
<br>
</p>
<p>Define a class to be useable for more than one type of element<br>
.special {<br>}
</p>
<p>Apply CSS to multiple elements<br>
h1, h2, h3 {<br>
}
</p>
<h1>Why I love Washington DC</h1>
<h2>The sun is strong and the people are great.</h2>
<p>DC is a large town in the East Coast. This text should be red. </p>
<p class="important">Warning: We have no slow cars here.</p>
<h2 class="typewriter">This heading is in typewriter text.</h2>
<p class="typewriter">This paragraph is also in typewriter text.</p>
</body>
</html>
In the CSS portion of the online demonstrator, enter these styles:
p {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 1.2em;
color: green;
}
p.important {
font-size: 1.5em;
color: red;
}
h1 {
font-family: "Trebuchet MS", Helvetica, sans-serif;
font-size: 2em;
}
.typewriter {
font-family: "Courier New", Courier, monospace;
color: blue;
}
Look at the website this HTML and CSS creates:
The following are the web sites referenced in the video
W3Schools CSS Web Safe Fonts - most web browsers will display these correctly
CSS Font Stack - percentage of Windows and Mac users that have each font family on their PC's
Click on the folder icon.
The font-family: CSS code will be copied to your clipboard
Paste the CSS code into your website's <style> section, i.e. p {font-family: "Arial Black","Arial Bold",Gadget,sans-serif}
Google Fonts - select the special fonts that will be downloaded to users viewing your website
Click on the + symbol to select the fonts you want
On the bottom of the page, click on the Families Selected window
Copy the <link> element into your <head> section
Finally, copy the font-family code into your CSS
Select your font(s)
Copy the Javascript code it gives you into your <head> section
Use the font-family declarations it gives you in your website's CSS
The CSS that is used is:
p { margin-left: auto; margin-right: auto; width: 8em }
or
p.blocktext { margin-left: auto; margin-right: auto; width: 8em }
Enter this text in your text editor:
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<h2>Center Text</h2>
<p>
This rather narrow block of text is centered. Note that the lines inside the block are not centered (they are left-aligned).</p>
</body>
</html>
Use the CSS to center the text.
Use embedded styles to:
Set all text to grey.
Set paragraph text within the .content class to purple.
Set the background color of the header to black.
Here is the HTML that is in your Dropbox:
html>
<head>
<title>Challenge 1</title>
<style>
/* Put your styles here, between the style tags. */
</style>
</head>
<header class="header">
<h1>This is a challenge!</h1>
<p>This is a paragraph in the header</p>
</header>
<section class="content">
<h2>Fun with CSS</h2>
<p>Learning CSS is fun! You can make the web a prettier place.</p>
</section>
<footer class="footer">
<p>You could put a copyright here.</p>
</footer>
</html>