CSS Font Formatting

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

When specifying multiple font choices in CSS using font-family, how does the browser determine which font to use?

  • The browser randomly selects a font from the list.
  • The browser uses the first font in the list that is installed on the user's system. (correct)
  • The browser uses the last font in the list that is installed on the user's system.
  • The browser selects the font that best matches the content.

Which CSS property is used to change the color of text?

  • font-color
  • text-color
  • text_color
  • color (correct)

What is the purpose of the text-indent property in CSS?

  • To create a hanging indent, where the first line is not indented but subsequent lines are.
  • To adjust the spacing between individual letters in a text.
  • To indent the first line of text within an element. (correct)
  • To align text to the left, right, center, or justify.

In CSS, what does the em unit refer to when setting the font-size?

<p>The height of the lowercase 'm' character of the current font. (D)</p> Signup and view all the answers

How do you apply a style to all HTML elements on a page using CSS?

<p>Use the <code>*</code> selector. (A)</p> Signup and view all the answers

What is the purpose of the !important rule in CSS?

<p>It ensures that the style is applied regardless of its position in the CSS file or specificity. (A)</p> Signup and view all the answers

Which CSS property controls the space between lines of text?

<p>line-height (C)</p> Signup and view all the answers

What do the terms 'baseline' and 'line-height' refer to in CSS text formatting?

<p>Baseline is the bottom of the text and line-height is the space between lines of text. (A)</p> Signup and view all the answers

What are the five generic font families in CSS?

<p>Serif, Sans-serif, Monospace, Cursive, and Fantasy. (D)</p> Signup and view all the answers

What is the effect of the following CSS?

em {font-style: italics; font-weight: bold;}
strong {font-style: italics; font-weight: 900;}```

<p><code>&lt;em&gt;</code> elements will be displayed in italics with a bold font, and <code>&lt;strong&gt;</code> elements will be displayed in italics using the thickest possible font weight. (A)</p> Signup and view all the answers

Flashcards

font-family

CSS property to set font family. Specifies prioritized fonts. If a font name contains spaces, enclose in quotes.

font-size

CSS property to define text size. Can be absolute (cm, mm, in, px, pt) or relative (em, ex, rem, %).

font-style

CSS property to set font style. Values: normal or italics.

font-weight

CSS property to set font weight. Values: normal, bold, or numeric (100-900).

Signup and view all the flashcards

color

CSS property to set text color using standard color names (black, white, etc.) or hexadecimal values.

Signup and view all the flashcards

line-height

Vertical space between text lines. Can be a number, length, or percentage.

Signup and view all the flashcards

text-align

CSS property to align text. Options: left, center, right, justify.

Signup and view all the flashcards

text-decoration

CSS property for text effects. Options: underline, overline, line-through, none.

Signup and view all the flashcards

text-indent

CSS property to indent the first text line. Accepts length or percentage values.

Signup and view all the flashcards

!important

Keyword in CSS rules to override normal precedence, forcing a style to be applied.

Signup and view all the flashcards

Study Notes

  • CSS is used to format text and fonts on web pages
  • CSS controls the formatting of characters including font, color, and style

CSS Font Formatting

  • CSS supports setting properties related to font choice (font-family), font size (font-size), font style (font-style), font weight (font-weight), and other attributes
  • font-family is used to choose a font

Font Families

  • serif: characters have serifs
  • sans-serif: characters don't have serifs
  • monospace: characters have fixed width
  • cursive: cursive-style fonts
  • fantasy: decorative fonts

Font-family usage

  • To specify a font for h1 tags, list fonts in order of preference
  • h1 {font-family: Times, "Times New Roman", Tahoma, serif;}
  • If a font name contains spaces, enclose it in double (or single) quotes
  • Browsers search for fonts from left to right
  • The browser selects a font if none are available

Font Size

  • font-size is used to set the font size, with the following syntax: selector {font-size: size;}
  • Font sizes can use absolute or relative units

Absolute Font Sizes

  • cm (centimeters)
  • mm (millimeters)
  • in (inches where 1 inch = 2.54 cm)
  • px (pixels, where 96 pixels = 1 inch)
  • pt (points, where 72 points = 1 inch)

Relative Font Sizes

  • em (relative to the font size of the current element)
  • ex (relative to the height of the letter "x" in the current font)
  • rem (relative to the font size of the root html element)
  • Percentage (%) of the parent element's font size
  • Keywords like xx-small, x-small, small, medium, large, x-large, xx-large (default is medium)
  • p {font-size: 1.2em;} sets the font size of all p elements to 1.2 times the current browser's font size
  • html {font-size: 100%;} sets the default font size for the entire web page

Font Style and Weight

  • font-style sets the text to normal or italic (normal or italics)
  • font-weight sets the boldness of the text
  • Options: normal, bold or numeric values from 100 to 900
  • em {font-style: italics; font-weight: bold;} sets em tags to italics and bold
  • strong {font-style: italics; font-weight: 900;} sets strong tags to italics with a stronger emphasis

CSS Text Color

  • color sets the text color
  • Common values: black, white, purple, blue, orange, red, green, yellow
  • h1 {color: red;} sets h1 text to red
  • em {color: green;} sets em text to green
  • * {color: black;} sets all other elements to black

CSS Text Line Formatting

  • These properties format text lines
  • Understanding baseline and line-height is necessary
  • baseline is the line on which letters stand
  • line-height is the distance between baselines in a paragraph
  • CSS defaults line-height to 2em

Line Height and Text Alignment

  • line-height sets the height of a line, using: p {line-height: 3;}
  • Alternatively, use: p {line-height: 2em;} which is the same as 2x the current line height
  • p {line-height: 200%;} sets the line height to 200% of the parent element's line height
  • text-align aligns text: left, center, right, justify
  • p {line-align: right;} aligns text to the right

Text Decoration and Indent

  • text-decoration adds text effects using none (default), underline, overline, and line-through
  • The property is not inherited
  • text-indent indents the first line, using positive or negative values
  • Negative values create a "hanging indent"

CSS Code Example

<style>
h1 {color: red; text-align: center;}
p {text-align: justify;}
</style>

CSS Inheritance

  • CSS properties are inherited
  • If a CSS style is applied to an HTML element, it is inherited by descendant elements, unless overridden

Inheritance Example

  • In the following example, only h1 has red text
  • h2 and p inherit the blue color from the body element
<style>
body {color: blue;}
h1 {color: red;text-align: center;}
</style>
<body>
<h1>Inheritance of CSS</h1>
<h2>1. HTML Tree Model</h2>
<p>This is the first paragraph...</p>
</body>

CSS Display Ranking

  • When multiple CSS rules apply to an HTML element, the browser applies the last rule written
  • This is the "cascading" feature of CSS

Display Ranking Example

  • h1 is left-aligned because the second rule overrides the first
<style>
body {color: blue;}
h1 {color: red;text-align: center;}
h1 {text-align: left;}
</style>
<body>
<h1>Inheritance of CSS</h1>

CSS Symbols

  • * selects any element, applying to everything not specified elsewhere
  • !important gives a CSS rule the highest priority, regardless of its position in the stylesheet

Symbol Example

  • text-align: center !important; in the first h1 rule makes this property highest priority
  • Colour is then specified
  • * sets a low priority
<style>
h1 {
    text-indent: 0em;
    color: blue;
    text-align: center !important;}
h1 { text-align: left; color: red;}
- {text-indent: 1em; color: blue; }
</style>

Applying CSS Rules

  • CSS rules are applied based on inheritance within the HTML tree
  • Last rule and keyword declarations always take precedence
  • Elements selected with * are applied but are the lowest priority
  • Rules declared !important are highest priority

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

CSS Font Family Property Guide
12 questions
CSS Text Formatting and Styling
13 questions
Font Styling and CSS Properties
10 questions
Use Quizgecko on...
Browser
Browser