MGU Computer Science Series — Web Development: HTML, CSS, and JavaScript
Part III: CSS — Style and Layout
Color · Typography · Spacing · Borders · Hierarchy
Chapter 13 of 30
13
MGU Computer Science Series  ·  Web Development: HTML, CSS, and JavaScript  ·  Library Textbook Edition

Chapter 13:
Colors, Typography,
Spacing, and Borders

The foundational visual properties of CSS — color, type, spacing, and borders — and how they combine to build readable, accessible, well-organized pages

■ Part III of VII ■

■ 12 Sections  •  Chapter 13 of 30 ■

Builds directly on Chapters 11–12 — selectors and the cascade required

Chapter Introduction and Learning Objectives

Chapter 12 taught you how CSS selects elements and how the cascade decides which rule wins when several rules apply. Now you have selectors that reliably reach the right elements. Chapter 13 teaches you what to do once you get there: how to control color, typography, spacing, and borders in ways that make pages readable, clear, and visually organized.

These are the foundational visual properties of CSS. Every page you build will use them. Every layout problem you encounter later — in Flexbox, in Grid, in responsive design — will assume you already understand how margins, padding, borders, and typography behave. Getting comfortable with these properties now will make everything that follows easier.

This chapter also introduces a distinction that runs through all of CSS: the difference between properties that affect how an element looks and properties that affect how much space it occupies. Typography mostly affects appearance. Spacing affects layout. Borders affect both. Learning to tell these apart will help you read CSS more clearly and debug it more effectively.

Opening Question

Two pages can contain the exact same HTML and yet feel completely different to read — one cramped and exhausting, the other calm and inviting. Nothing about the content changed. What changed is color, type, spacing, and borders: the four properties this chapter teaches. By the end of the chapter, you will be able to look at any page and explain, property by property, why it feels the way it does — and you will be able to build that feeling yourself.

12Sections
11Objectives
12Key Terms
30Total Chapters

Learning Objectives

By the end of this chapter, you should be able to:

  1. 1Identify and write CSS color values using named colors, hexadecimal values, RGB, and HSL.
  2. 2Distinguish between the color and background-color properties and explain why one is inherited and the other is not.
  3. 3Evaluate a text-and-background color pair for sufficient accessibility contrast.
  4. 4Build a font stack that ends in an appropriate generic family name, including a system font stack.
  5. 5Explain the difference between px, em, and rem font-size units and explain why rem is recommended for scalable typography.
  6. 6Set comfortable, unitless line-height values for body text and for headings.
  7. 7Apply font-weight values to create emphasis and hierarchy.
  8. 8Use text-align and text-decoration to control text presentation appropriately.
  9. 9Distinguish padding from margin and use margin: 0 auto to center a block element.
  10. 10Use the border shorthand and border-radius to build boxes, callouts, pills, and circles.
  11. 11Combine color, typography, spacing, and borders to produce a clear visual hierarchy on a complete page.

Sections at a Glance

Sections 13.1 – 13.3
Color: Formats, Properties, and Accessibility
Readability · Hex · RGB · HSL · color · background-color · Contrast

Visual Design Begins with Readability

Before learning individual CSS properties, it helps to understand what those properties are for. Visual design in web development is not decoration applied after the content is ready. It is the craft of making content readable, navigable, and usable.

Every property in this chapter is a tool for one of four goals. Keep these in mind as you read — they explain why a property exists, not just what it does.

Goal 1
Readability
Text that is easy to read. Size, line height, font choice, and color all affect how effortless reading feels.
Goal 2
Contrast
Important elements stand out from each other and from the background. Headings should look different from body text; links should look different from plain text.
Goal 3
Spacing
Elements have room to breathe. Crowded text is harder to scan; generous spacing creates clarity and organization.
Goal 4
Hierarchy
Visual weight reflects importance. A page title should carry more weight than a subheading, which should carry more weight than a paragraph.
Keep This in Mind

The goal of styling is never to make a page look impressive for its own sake. The goal is to serve the reader. Every property in this chapter should be judged against these four goals.

Color Formats in CSS

CSS provides several ways to specify color. Each format describes the same set of colors but expresses them differently. You will see all of these in real CSS, so you need to be able to read and write all of them.

Named Colors

CSS recognizes a set of named colors written as plain words. CSS has 140 named colors, including common ones like red, blue, and gray, and less common ones like rebeccapurple and papayawhip. Named colors are easy to read and useful for quick experiments, but they are too limited and imprecise for serious design work.

CSS
p { color: black; } h1 { color: navy; } body { background-color: white; }

Hexadecimal Color Values

Hexadecimal values, usually called hex values, are the most common color format in web CSS. A hex value begins with # followed by six characters — three pairs for red, green, and blue. Each character is a digit from 0–9 or a letter from A–F. 00 means none of that channel; ff means the maximum.

CSS
h1 { color: #2c3e50; } p { color: #444444; } body { background-color: #ffffff; }

When both characters in each pair match, you can shorten the value to three characters: #ffffff becomes #fff, and #444444 becomes #444. This shorthand is common in real stylesheets.

RGB Values

RGB values express color as three numbers — red, green, and blue — each ranging from 0 to 255. RGB is mathematically identical to hex; rgb(255, 0, 0) is the same color as #ff0000.

CSS
h1 { color: rgb(44, 62, 80); } .overlay { background-color: rgba(0, 0, 0, 0.5); } .notice { background-color: rgba(255, 200, 0, 0.15); }

The rgba() format adds a fourth value, the alpha channel, ranging from 0 (fully transparent) to 1 (fully opaque). This is frequently used for overlays, shadows, and subtle background tints.

HSL Values

HSL stands for Hue, Saturation, and Lightness — a way to describe color that many designers find more intuitive. Hue is an angle on a color wheel from 0 to 360 (0 and 360 are both red; 120 is green; 240 is blue). Saturation runs from 0% (gray) to 100% (vivid). Lightness runs from 0% (black) to 100% (white), with 50% giving the purest version of the color.

CSS
h1 { color: hsl(210, 29%, 24%); } a { color: hsl(207, 90%, 54%); } .card { background-color: hsl(0, 0%, 97%); }

HSL makes it easy to create color variations: increase lightness for a lighter version, decrease saturation for a more muted one. Like RGB, HSL has an alpha variant: hsla(207, 90%, 54%, 0.8).

FormatExampleBest Use
Namednavy, whiteQuick experiments and prototypes
Hex#2c3e50The most common format; compact and easy to copy from tools
RGB / rgba()rgb(44, 62, 80)When you need transparency or readable numeric channels
HSL / hsla()hsl(210, 29%, 24%)Building systematic color scales and variations
Which Format Should You Use?

For this course, you will mostly use hex values and named colors. Hex values are compact, widely used, and easy to copy from design tools, color pickers, and documentation. As you build CSS variable systems in Chapter 18, HSL may become more useful because its logic makes systematic color scales easier to build.

The color and background-color Properties

The two most fundamental color properties are color and background-color. color sets the foreground color of an element — for text elements, the color of the text itself, including text decorations such as underlines. background-color sets the background color of the element's box. By default, element backgrounds are transparent, so the parent's background shows through.

CSS
body { background-color: #f8f8f8; color: #333333; } h1 { color: #1a1a2e; } .notice { background-color: #fff8e1; color: #5d4037; }

Color Inheritance

The color property is inherited. If you set color on body, all text inside the page inherits that color unless a more specific rule overrides it. This is why setting color and background-color on body is a common, efficient pattern for establishing base page colors.

background-color is not inherited. Child elements default to a transparent background, allowing the parent's background to show through. If you want a child element to have a different background, you must set it explicitly.

Color Contrast for Accessibility

Color contrast is a critical accessibility requirement, not a design preference. When text lacks sufficient contrast against its background, many users cannot read it — including users with low vision, older users, users in bright sunlight, and users on lower-quality screens.

Accessibility Requirement

The Web Content Accessibility Guidelines (WCAG) define minimum contrast ratios: 4.5 to 1 for normal text and 3 to 1 for large text. Free online tools can calculate the contrast ratio between two colors. Any text-and-background combination you use should pass these minimums before you ship it.

CSS
/* Low contrast — difficult to read */ .low-contrast { background-color: #f0f0f0; color: #cccccc; } /* High contrast — easy to read */ .high-contrast { background-color: #ffffff; color: #333333; }

The first rule places light gray text on a light gray background — the contrast ratio is far too low for comfortable reading. The second places dark gray text on white, providing strong contrast. Bright color combinations can fail contrast checks too: bright yellow text on white looks vivid but is extremely difficult to read. Always check your contrast ratio with a tool before finalizing a color scheme.

Sections 13.4 – 13.8
Typography: Fonts, Size, Weight, and Alignment
Font Stacks · px / em / rem · Line Height · Font Weight · Alignment · Decoration

Font Families

The font-family property controls which typeface is used for text. On the web, fonts work differently than in a word processor: the browser renders text using fonts installed on the user's operating system, or fonts downloaded from a web font service.

Font Stacks

Because you cannot guarantee which fonts a user has installed, font-family accepts a comma-separated list of font names called a font stack. The browser uses the first font in the list that it can find.

CSS
body { font-family: Georgia, "Times New Roman", Times, serif; } h1 { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; } code { font-family: "Courier New", Courier, monospace; }

The last value in a font stack should always be a generic family name: serif, sans-serif, monospace, cursive, or fantasy. These are keywords, not specific fonts — they tell the browser which category to fall back to if none of the named fonts are available. Serif fonts have small decorative strokes at the ends of letterforms (Georgia, Times New Roman). Sans-serif fonts have none and tend to look cleaner on screens (Helvetica, Arial). Monospace fonts give every character equal width, which matters for code and terminals.

System Font Stacks

Modern CSS often uses a system font stack that tells the browser to use the operating system's default interface font, avoiding slow font loading and providing a native appearance.

CSS
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif; }

Each entry is a fallback for a specific operating system: macOS uses San Francisco, Windows uses Segoe UI, Android uses Roboto, Linux uses Ubuntu or Cantarell. If none are found, the browser falls back to a generic sans-serif. System font stacks are widely used where fast loading and native appearance matter more than a specific designed typeface.

Web Fonts

Web fonts are font files loaded from a server — your own, or a service such as Google Fonts. Web fonts give you access to any typeface, but add a network request and a small loading cost.

CSS
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap'); body { font-family: "Inter", sans-serif; }
A Habit Worth Building Now

You may use system fonts or web fonts freely in this course. The important habit to build now is always ending your font stack with a generic family name.

Font Size

The font-size property controls how large text appears. Font size can be specified in several units — most commonly pixels (px), em, and rem.

Pixels

Pixels are absolute units. font-size: 16px means the text renders at 16 pixels tall. The typical base size for body text is 16px — the browser default for most elements and a comfortable reading size on most screens. Going below 14px makes reading uncomfortable for many users.

CSS
body { font-size: 16px; } h1 { font-size: 36px; } h2 { font-size: 28px; } small { font-size: 13px; }

em Units

The em unit is relative to the current element's font size. If body font size is 16px, 1em is 16px and 1.5em is 24px. The problem with em is that nested elements compound: a list item inside a container that already has a reduced font size inherits that smaller size, and then em values calculate relative to it again, producing unexpected results.

CSS
body { font-size: 16px; } h1 { font-size: 2.25em; /* 36px when body is 16px */ } small { font-size: 0.875em; /* 14px */ }

rem Units

rem stands for root em. It is relative to the font size of the root <html> element, not the current element. Because rem always refers to the same base size, it does not compound through nesting.

CSS
html { font-size: 16px; } h1 { font-size: 2.25rem; /* always 36px, regardless of nesting */ } p { font-size: 1rem; /* always 16px */ }
UnitBased OnCompounds When Nested?
pxA fixed pixel valueNo — always absolute
emThe current element's font sizeYes — can compound unexpectedly
remThe root html element's font sizeNo — predictable everywhere
Why rem Is Recommended

rem is widely recommended for font sizes in modern CSS because it is predictable and easy to reason about. When you learn CSS custom properties in Chapter 18, you will use rem as the unit for a consistent typographic scale. For this chapter, practice with both px and rem — use px to understand exact pixel sizes directly, and use rem to begin building good habits for scalable typography.

Line Height

Line height controls the vertical space between lines of text within a paragraph, using the line-height property. Adequate line height dramatically improves readability, especially for longer passages.

CSS
/* Too tight — lines feel cramped */ .cramped { font-size: 16px; line-height: 1; } /* Comfortable — standard for body text */ .comfortable { font-size: 16px; line-height: 1.6; } /* Too loose — looks disconnected */ .loose { font-size: 16px; line-height: 2.5; }

Notice that line-height is set without a unit. A unitless value is a multiplier based on the current font size — line-height: 1.6 means the line height is 1.6 times the font size (25.6px on 16px text). Using a unitless multiplier is recommended because it scales proportionally when font size changes; a fixed pixel value will not scale correctly when the font size differs.

Recommended Line Heights

For body text, a line height between 1.4 and 1.7 is generally comfortable — 1.5 and 1.6 are common defaults. For headings, a line height between 1.1 and 1.3 is usually appropriate, since large text already has letters spaced farther apart and needs less extra space between lines.

CSS
body { font-size: 16px; line-height: 1.6; } h1, h2, h3 { line-height: 1.2; }

This is a widely used pattern: generous line height for readability in body text, and tighter line height for headings, where extra space between lines feels excessive at large sizes.

Font Weight

Font weight controls how thick or thin letterforms appear. The font-weight property accepts keyword values (normal, bold) and numeric values from 100 to 900 in increments of 100. normal is equivalent to 400 and bold is equivalent to 700.

CSS
p { font-weight: normal; } strong { font-weight: bold; } h1 { font-weight: 700; } .medium-label { font-weight: 500; }
A Limitation Worth Knowing

A font must actually include the weight you request. If you specify font-weight: 300 and the font has no 300 variant, the browser substitutes the nearest available weight. Many system fonts only include 400 and 700. Google Fonts and other web font services often offer multiple weights.

WeightCommon Use
400 (normal)Standard body text
500 – 600 (medium)Subheadings, labels, UI text
700 (bold)Headings and strong emphasis

Text Alignment and Text Decoration

Text Alignment

The text-align property controls the horizontal alignment of text within its container: left, right, center, and justify.

CSS
p { text-align: left; } h1 { text-align: center; } .price { text-align: right; }

Left alignment is the default in left-to-right languages and is the easiest text to read for most users. Center alignment suits short headings and hero text. Right alignment is useful in tables for numbers and prices. Justify stretches text so both edges align, which can help printed documents but often creates uneven spacing on screens, especially in narrow containers — use it sparingly. For body text in a left-to-right language, text-align: left is almost always the right choice for readability.

Text Decoration

The text-decoration property controls underlines, overlines, and strikethroughs.

CSS
a { text-decoration: underline; } .strikethrough { text-decoration: line-through; } a.button-link { text-decoration: none; }
A Caution About Removing Underlines

Browsers underline anchor elements by default. Removing the underline with text-decoration: none is common for navigation links and buttons styled in other ways. Be careful removing underlines from links inside body text — users expect text links to be underlined, and removing that visual cue can confuse them.

Sections 13.9 – 13.12
Spacing, Borders, and Visual Hierarchy
Padding · Margin · Borders · Border Radius · Hierarchy · A Complete Page

Margins and Padding

Margins and padding are both spacing properties, but they act in different places. Understanding this distinction is fundamental to CSS layout.

The Key Distinction

Padding is the space between an element's content and its border — inside the box.

Margin is the space between an element's border and neighboring elements — outside the box.

Chapter 14 explores both in depth as part of the complete box model. For now, practice with the basic single-value syntax to build familiarity with how each property changes the appearance of elements.

Padding

CSS
p { padding: 16px; } .card { padding: 24px; } .tight-label { padding: 4px 8px; }

padding: 16px applies 16px of internal space to all four sides. padding: 4px 8px applies the first value (4px) to top and bottom and the second value (8px) to left and right.

Margin

CSS
h1 { margin-bottom: 8px; } p { margin-top: 0; margin-bottom: 16px; } .section { margin-top: 48px; margin-bottom: 48px; }

Individual sides can be set with margin-top, margin-right, margin-bottom, and margin-left — the preferred approach when you only want to control spacing on specific sides. A very common pattern is resetting paragraph top margin to zero and controlling spacing entirely through bottom margin, producing predictable, consistent vertical rhythm down the page.

Margin Auto for Centering

One special and important use of margin is centering a block element horizontally. Setting margin-left: auto and margin-right: auto on a block element with a defined width centers it within its container.

CSS
.container { width: 800px; margin-left: auto; margin-right: auto; }
A Shorthand Worth Memorizing

The shorthand margin: 0 auto sets top and bottom margins to zero and left and right margins to auto, centering the element horizontally. This is one of the most commonly used layout patterns in all of CSS.

Borders and Border Radius

Borders draw a visible line around the outside of an element's padding area. The border shorthand property accepts three values: width, style, and color.

CSS
.box { border: 1px solid #cccccc; } .alert { border: 2px solid #e53935; } .notice { border: 1px dashed #888888; }
PartCommon ValuesNotes
Width1px, 2px, 3pxBorders of 4px or more look heavy unless used intentionally
Stylesolid, dashed, dotted, nonesolid and none are by far the most common
ColorAny CSS color valueOften subtle gray, or meaningful (red for errors, green for success)

Individual Border Sides

You can set borders on individual sides using border-top, border-right, border-bottom, and border-left.

CSS
.section-heading { border-bottom: 2px solid #1a1a2e; } .left-highlight { border-left: 4px solid #3498db; padding-left: 16px; }

A bottom border under a heading is a common way to create visual separation. A left border combined with left padding creates an indented callout style widely used for quotations and notices.

Border Radius

The border-radius property rounds the corners of an element's border box.

CSS
.card { border: 1px solid #e0e0e0; border-radius: 8px; } .pill-button { border-radius: 999px; } .avatar { border-radius: 50%; }

border-radius: 8px rounds all four corners by 8 pixels. border-radius: 50% creates a perfect circle on an element with equal width and height. border-radius: 999px creates a fully rounded pill shape on a wide rectangle — a common technique for badges and buttons.

Using Borders Without Visible Lines

CSS
.input { border: 1px solid #cccccc; } .input:focus { border: 1px solid #3498db; }

Here, an input field has a gray border by default; on focus, the border color changes to blue. Because both states define a 1px solid border, the layout does not shift when focus activates — only the color changes.

Visual Hierarchy

Visual hierarchy is the arrangement of elements so their visual weight reflects their importance. Users scan pages before reading them. Visual hierarchy guides the eye, signals what matters most, and makes pages easier to navigate. CSS controls hierarchy through the properties you have learned in this chapter: size, weight, color, contrast, spacing, and borders.

CSS
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; font-size: 16px; line-height: 1.6; color: #333333; background-color: #ffffff; margin: 0; padding: 0; } h1 { font-size: 2rem; font-weight: 700; color: #1a1a2e; line-height: 1.2; margin-top: 0; margin-bottom: 12px; } h2 { font-size: 1.4rem; font-weight: 600; color: #2c3e50; line-height: 1.3; margin-top: 40px; margin-bottom: 8px; } p { margin-top: 0; margin-bottom: 20px; max-width: 680px; } a { color: #2980b9; text-decoration: underline; } a:hover { color: #1a5276; }
Reading the Decisions
Every rule above is a deliberate choice
  • bodySets the visual foundation: a readable font, 16px size, generous line height, dark gray text on white, and no default margins.
  • h1Large, heavy, and dark — the most visually important element on the page.
  • h2Noticeably smaller than h1 but still distinctly larger and heavier than body text. Extra top margin separates it from content above, creating clear section breaks.
  • pNo top margin and a consistent bottom margin, creating even vertical rhythm down the page.
  • a / a:hoverA recognizable link color with an underline. The hover state darkens the color, giving visual feedback when a user points at the link.

Together, these rules create a clear visual hierarchy without any complex layout, shadows, or decorative elements. Hierarchy is a product of disciplined, consistent decisions — not visual flourishes.

Building a Readable Page Style

This section walks through a complete, working example of foundational styles applied to a real HTML page. Study each rule carefully. Notice how typography, color, spacing, and borders work together.

HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Learning Web Development</title> <link rel="stylesheet" href="css/main.css"> </head> <body> <header> <nav> <a href="index.html">Home</a> <a href="about.html">About</a> <a href="lessons.html">Lessons</a> <a href="contact.html">Contact</a> </nav> </header> <main> <article> <h1>Getting Started with CSS</h1> <p class="byline">Posted on October 15 — Web Development Lessons</p> <p>CSS stands for Cascading Style Sheets. It is the language that controls the visual presentation of HTML documents.</p> <h2>Why CSS Matters</h2> <p>Without CSS, every web page would look the same: black text on a white background, in a single font, with no layout.</p> <div class="notice"> <strong>Important:</strong> Always link your CSS file using a <code>&lt;link&gt;</code> element in the <code>&lt;head&gt;</code> section. </div> <h2>What CSS Controls</h2> <ul> <li>Font families, sizes, weights, and line heights</li> <li>Text colors and background colors</li> <li>Spacing: margins and padding</li> <li>Borders and border radius</li> <li>Layout: positioning, flexbox, and grid</li> </ul> </article> </main> <footer> <p>&copy; 2025 Student Learning Site. All rights reserved.</p> </footer> </body> </html>
CSS — css/main.css
/* --- Global reset and base styles --- */ *, *::before, *::after { box-sizing: border-box; } body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Open Sans", sans-serif; font-size: 16px; line-height: 1.65; color: #333333; background-color: #f9f9f9; margin: 0; padding: 0; } /* --- Header and navigation --- */ header { background-color: #1a1a2e; padding: 0 32px; } nav { display: flex; gap: 24px; padding: 16px 0; } nav a { color: #e0e0e0; text-decoration: none; font-size: 0.95rem; font-weight: 500; } nav a:hover { color: #ffffff; text-decoration: underline; } /* --- Main content area --- */ main { max-width: 760px; margin: 48px auto; padding: 0 24px; } /* --- Article typography --- */ article { background-color: #ffffff; border: 1px solid #e0e0e0; border-radius: 6px; padding: 40px 48px; } h1 { font-size: 2rem; font-weight: 700; color: #1a1a2e; line-height: 1.2; margin-top: 0; margin-bottom: 8px; } .byline { font-size: 0.875rem; color: #888888; margin-top: 0; margin-bottom: 32px; border-bottom: 1px solid #e8e8e8; padding-bottom: 20px; } h2 { font-size: 1.35rem; font-weight: 600; color: #2c3e50; line-height: 1.3; margin-top: 40px; margin-bottom: 12px; border-bottom: 2px solid #e8e8e8; padding-bottom: 8px; } p { margin-top: 0; margin-bottom: 20px; } a { color: #2980b9; text-decoration: underline; } a:hover { color: #1a5276; } /* --- Lists --- */ ul { margin-top: 0; margin-bottom: 20px; padding-left: 24px; } li { margin-bottom: 8px; line-height: 1.6; } /* --- Notice box --- */ .notice { background-color: #fff8e1; border: 1px solid #ffe082; border-left: 4px solid #f9a825; border-radius: 4px; padding: 16px 20px; margin-top: 0; margin-bottom: 24px; font-size: 0.95rem; color: #5d4037; } /* --- Inline code --- */ code { font-family: "Courier New", Courier, monospace; font-size: 0.875em; background-color: #f0f0f0; color: #c0392b; padding: 2px 5px; border-radius: 3px; } /* --- Footer --- */ footer { text-align: center; padding: 32px 24px; font-size: 0.875rem; color: #999999; border-top: 1px solid #e0e0e0; margin-top: 64px; }
Reading the Example

Work through the stylesheet rule by rule and answer these questions for each one: What element or class does this rule target? What visual property does it control? Why might this choice have been made? What would the page look like without this rule? This kind of active CSS reading is more valuable than copying styles without thinking. Every rule is a decision — understanding why decisions are made is how you develop design judgment.

Chapter Summary

Key Terms

color contrast
The difference in luminance between a foreground color and its background. Sufficient contrast is required for text to be readable by users with low vision or in difficult viewing conditions.
font stack
A comma-separated list of font family names in a font-family declaration. The browser uses the first available font on the user's system.
generic family name
A keyword at the end of a font stack telling the browser which type category to use as a final fallback: serif, sans-serif, monospace, cursive, or fantasy.
hex value
A color expressed as a six-character hexadecimal code preceded by #. The characters represent red, green, and blue channel values from 00 to ff.
HSL
A color model using Hue, Saturation, and Lightness. Useful for creating predictable color variations.
line height
The vertical space occupied by a line of text. Unitless values like 1.6 are multiples of the current font size.
margin
Space outside an element's border, separating it from neighboring elements. Can be set per side or with a shorthand.
padding
Space inside an element between its content and its border. Padding adds to the visual size of the element's background area.
RGB
A color model using Red, Green, and Blue values from 0 to 255. rgba() adds an alpha transparency value from 0 to 1.
rem
Root em. A relative unit equal to the font size of the root html element. Does not compound through nesting.
visual hierarchy
The arrangement of visual elements so that their weight, size, or prominence corresponds to their importance in the content.
font-weight
A CSS property controlling the thickness of text characters. Numeric values range from 100 to 900; 400 is normal and 700 is bold.
PracticeCode Reading

Read each CSS rule or group of rules carefully. Answer the questions after each one before running the code.

Exercise 13-R1
h1 { font-size: 2.5rem; font-weight: 700; color: #1a1a2e; line-height: 1.15; }
  1. If the root font size is 16px, how tall will the h1 text be in pixels?
  2. Is font-weight: 700 heavier or lighter than the default?
  3. What does line-height: 1.15 mean in terms of actual spacing?
Exercise 13-R2
p { font-size: 1rem; line-height: 1.7; color: #444444; margin-top: 0; margin-bottom: 24px; }
  1. Why might margin-top be set to 0 instead of some positive value?
  2. Is #444444 dark, medium, or light gray?
  3. If the root font size is 16px, how much bottom space does each paragraph have?
Exercise 13-R3
.notice { background-color: #e8f5e9; border: 1px solid #a5d6a7; border-left: 4px solid #2e7d32; border-radius: 4px; padding: 16px 20px; color: #1b5e20; }
  1. How many separate border properties are defined here? How do they interact?
  2. What color scheme do these hex values suggest (warm, cool, neutral)?
  3. What would padding: 16px 20px produce on each side?
Exercise 13-R4
.container { max-width: 900px; margin-left: auto; margin-right: auto; padding: 0 24px; }
  1. What do margin-left: auto and margin-right: auto accomplish for this container?
  2. Why is there padding: 0 24px on a container element?
  3. What happens to this container on a screen narrower than 900px?
PracticeCode Tracing

Trace through the CSS rules below and predict the computed style of each element.

Exercise 13-T1
body { font-size: 16px; color: #333333; } h2 { font-size: 1.5rem; color: #1a1a2e; font-weight: 600; } h2.secondary { color: #888888; font-weight: 400; }
HTML
<h2>Primary Heading</h2> <h2 class="secondary">Secondary Heading</h2>
  1. What color is the first h2?
  2. What color is the second h2?
  3. What font weight is the second h2?
  4. What size in pixels is each h2?
Exercise 13-T2
a { color: #2980b9; text-decoration: underline; } nav a { color: #ffffff; text-decoration: none; }
HTML
<nav> <a href="about.html">About</a> </nav> <p> Read our <a href="guide.html">beginner's guide</a>. </p>
  1. What color is the "About" link?
  2. Is the "About" link underlined?
  3. What color is the "beginner's guide" link?
  4. Is the "beginner's guide" link underlined?
PracticePredict the Result

For each code block, describe what the rendered element will look like.

Exercise 13-P1
.badge { background-color: #e53935; color: #ffffff; font-size: 0.75rem; font-weight: 700; padding: 4px 10px; border-radius: 999px; }

Describe the visual appearance of an element with class badge containing the text "New".

Exercise 13-P2
.card { background-color: #ffffff; border: 1px solid #e0e0e0; border-radius: 8px; padding: 24px; } .card h3 { font-size: 1.1rem; font-weight: 600; color: #1a1a2e; margin-top: 0; margin-bottom: 8px; } .card p { font-size: 0.95rem; color: #666666; margin: 0; line-height: 1.5; }

Describe the visual appearance of a .card containing an h3 and a p.

PracticeDebugging and Error Analysis

Each code block below contains one or more problems. Identify the problem and explain how to fix it.

Exercise 13-D1
h1 { font-size: 36; color: 1a1a2e; font-weight: bold; }

What two errors are in this rule?

Exercise 13-D2
body { font-family: Helvetica Neue, Arial, sans-serif; font-size: 16px; }

What is the problem with the font-family declaration?

Exercise 13-D3
.warning-box { background-color: #ffeb3b; color: #f5f5f5; padding: 16px; border-radius: 4px; }

This .warning-box displays yellow with light text. What is the likely problem, and how would you fix it?

Exercise 13-D4
.article-content { margin: auto; max-width: 720px; }

A developer expects the article content to be centered on the page, and it appears to center horizontally. Another developer points out that the margin: auto shorthand is technically valid here but a common source of confusion. Why?

PracticeShort Build Tasks
Task 13-B1 — Base page styles

Create a CSS file that establishes a clean, readable base for a page. Your stylesheet must include:

  • A system font stack on the body
  • 16px font size and 1.6 line height on the body
  • A dark gray text color and a near-white background color on the body
  • Heading styles for h1, h2, and h3 with distinct sizes, weights, and colors that create clear visual hierarchy
  • Paragraph styles with no top margin and a consistent bottom margin
Task 13-B2 — Color the navigation

Given the following HTML, write CSS that gives the header a dark background color of your choice, makes the navigation links white with no underlines, and adds a hover state that underlines the links when the mouse is over them.

HTML
<header> <nav> <a href="index.html">Home</a> <a href="about.html">About</a> <a href="lessons.html">Lessons</a> </nav> </header>
Task 13-B3 — Notice and callout styles

Write CSS for two classes. .success-notice: a green-tinted notice box for positive feedback, with a light green background, dark green text, a 4px solid green left border, padding, and a slightly rounded border. .error-notice: a red-tinted notice box for error feedback, using the same padding and border radius but a light red or pink background, dark red text, and a red left border. Avoid color values so vivid they strain the eye — the goal is readable, accessible, color-coded notices.

Task 13-B4 — Card with border and spacing

Write HTML and CSS for a simple article card with a white background, a 1px solid light gray border, 8px border radius, 24px of internal padding on all sides, a card heading (h3) with appropriate size, weight, and color, a short paragraph with slightly muted text color compared to the heading, and a link at the bottom styled distinctly as a "read more" link.

Mini-ProjectReadable Article Page

Build a complete readable article page applying everything from this chapter.

Readable Article Page Requirements
Confirm each item before submitting
HTML Structure
  • A header with site title and navigation links.
  • A main element containing an article.
  • An h1, a byline paragraph, and at least two h2 sections inside the article.
  • Multiple paragraphs, an unordered list, and one notice or callout box.
  • A footer.
CSS Requirements
  • A base font, font size, line height, text color, and background color on the body.
  • Distinct heading styles for h1 and h2 using different sizes, weights, and colors — h1 must be clearly the most dominant heading.
  • A comfortable reading width for the article using max-width, centered with auto margins.
  • Styled navigation with a dark background, light link text, no underlines, and a hover state.
  • A .notice or .callout class with a tinted background, a colored left border, and padding.
  • A styled footer with a top border, muted text color, and centered text.
Quality Check
  • Body text is large enough and has adequate line height.
  • Headings create a clear visual hierarchy.
  • All text has sufficient contrast against its background.
  • All navigation links are visible against the header background.
  • The article is comfortable to read at its constrained width.

Accessibility Check

Answer these questions about your mini-project page or any page you have built this chapter.

  1. Open a browser color contrast checker. Test your body text color against your page background. Does it pass the 4.5:1 minimum ratio?
  2. Test your navigation link text against the header background. Does it pass the 3:1 minimum for large text or 4.5:1 for normal text?
  3. Is your font size at least 16px for body text? If you used rem, calculate the actual pixel size.
  4. Is your line height at least 1.4 for body text? Would a user with dyslexia find the line spacing comfortable?
  5. Are all links distinguishable from plain text either by underline, color, or another visual cue?

Mixed Review

These questions draw from Chapters 11, 12, and 13.

  1. What is the difference between a class selector and a type selector? Give one example of each.
  2. A p element has color: #555555 from a rule targeting p. Inside that paragraph, a span has color: #e53935. What color will the span text be, and why?
  3. What does the cascade use to resolve conflicts between two rules that target the same element?
  4. You want the font-family declared on body to apply to all text on the page. Does font-family inherit? How does this affect your stylesheet?
  5. A developer writes margin: 24px auto. What does this shorthand do to each of the four sides?
  6. You have a heading with font-size: 1.5rem and the root font size is 18px. What is the computed size in pixels?
  7. What is the difference between border: none and border: 0?
  8. A user reports they cannot read the text on a page. You inspect and find the text is #aaaaaa on a #ffffff background. What is likely the problem and what would you do to fix it?
Preview — Chapter 14
The Box Model

Chapter 13 introduced margins, padding, and borders as styling tools. Chapter 14 examines them in depth as layout tools. You will learn the CSS box model — the precise structural model that determines how much space every element occupies on the page. You will study the content area, padding, border, and margin as distinct regions of every element's box. You will learn the box-sizing property, the difference between block and inline elements in terms of space, the width and height properties, and how to use developer tools to inspect and debug the box model visually. Understanding the box model is prerequisite knowledge for everything in the layout chapters that follow.

MGU Library Connection
  • Web Development: HTML, CSS, and JavaScript — Appendix D: CSS Reference
  • Web Development: HTML, CSS, and JavaScript — Appendix M: Glossary
  • Companion resource: CSS Selector Chart
  • Companion resource: Color and Contrast Guide
  • Companion resource: Typography Checklist
  • Companion resource: CSS Box Model Chart
  • Next chapter: Chapter 14 — The Box Model (requires this chapter's spacing and border concepts)
Chapter 13 Checklist
Use this to confirm your understanding before moving to Chapter 14
Concepts
  • I can write CSS colors using named colors, hex, RGB, and HSL.
  • I can explain why color is inherited and background-color is not.
  • I can check whether a text-and-background color pair meets accessibility contrast minimums.
  • I can build a font stack that ends in a generic family name.
  • I can explain the difference between px, em, and rem, and why rem is recommended.
  • I can set unitless line-height values appropriately for body text and headings.
  • I can apply font-weight values for emphasis and hierarchy.
  • I can distinguish padding from margin and explain what each affects.
  • I can use margin: 0 auto to center a block element.
  • I can write the border shorthand and use border-radius for rounded corners, pills, and circles.
Applications
  • I can build a complete, readable page style combining color, typography, spacing, and borders.
  • I can identify and explain the visual hierarchy in an existing stylesheet.
  • I can debug common color, font-size, and spacing mistakes in CSS.
Study Habits
  • I can read an unfamiliar CSS rule and explain what visual effect it produces before testing it in a browser.
  • I check color contrast with a tool rather than relying on my own judgment.
  • I default to rem for font sizes and unitless values for line height.