Font and Text
After covering the general way CSS works, it's time to go through a few specific areas. We'll cover text-related styles, colors, sizing, the box model and spacing of elements.
font
Many common styles are related to the font of an item. Here's an example:
.dramatic {
font-style: italic;
font-size: large;
font-family: "Impact";
}
If you add the class dramatic to a paragraph:
<p class="dramatic">Dracula</p>
You'll get
Dracula
font-family
is used for setting the actual font of text. You should specify more than one font in case a user doesn't have the first font installed. (The browser will use the display the first font that's available.)
font-family: Impact, Charcoal, sans-serif;
Shorthand Properties
Often, related CSS properties can be combined into shorthand properties. The above CSS properties could be replaced with one font
shorthand property:
.dramatic {font: italic large "Impact"}
Shorthand properties let you set the values of several related CSS properties simultaneously. CSS defines shorthand properties for groups of related properties, such as font, background and margin. The properties in bold on the CSS reference page are all shorthand properties.
Google Fonts
Modern CSS lets you include actual fonts with your page so you know the user will have them. This is done with the @font-face rule and a font file, but you don't need to include the font yourself. Instead, you can generate a CSS style with Google Fonts and include the CSS they provide in your web page.
text
There are various styles to adjust the look of text without changing the font. For example, text-align
sets the alignment of text, and can either be to the left
, right
, or center
. Here's an example of each:
<p style="text-align: left">hello world</p>
hello world
<p style="text-align: right">hello world</p>
hello world
<p style="text-align: center">hello world</p>
hello world
(See the Reference page for other text styles.)
Store Challenge
Create a new style for the <h1>
heading on your site that sets the font-family to "Arial Black". In case the user doesn't have "Arial Black", it should use Arial.
Challenge
Add a style below that changes the font-family of h1
to "Arial Black" and Arial.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Cher
Sep 11, 3:34 AMh1{ font-family:"Arial Black", Arial;} --> why my code is incorrect?
Learneroo
Sep 11, 9:53 AMHey thanks for asking, looks like there may have been an issue checking some CSS, please try again.
Cher
Sep 11, 10:50 AMI think the problem happens because I am using Mozilla Firefox, when I do some research on Google I found this: https://www.codecademy.com/en/forum_questions/515eb975982c3bd445002775
Cheers
Learneroo
Sep 11, 11:40 AMYou're right, different browsers use slightly different formats. I updated the grader to be more lenient so it passes on both browsers. The preview on the right is the best way to see how your CSS looks.
Cher
Sep 11, 11:48 AMThank man, keep up your great works!