The foundational visual properties of CSS — color, type, spacing, and borders — and how they combine to build readable, accessible, well-organized pages
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.
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.
By the end of this chapter, you should be able to:
color and background-color properties and explain why one is inherited and the other is not.px, em, and rem font-size units and explain why rem is recommended for scalable typography.line-height values for body text and for headings.font-weight values to create emphasis and hierarchy.text-align and text-decoration to control text presentation appropriately.margin: 0 auto to center a block element.border shorthand and border-radius to build boxes, callouts, pills, and circles.color and background-color PropertiesForeground versus background color; inheritance; color contrast and accessibility minimumspx, em, and rem units; why rem avoids compoundingtext-align values; underlines, strikethroughs, and link conventionsmargin: 0 autoBefore 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.
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.
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.
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.
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.
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 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.
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 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.
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).
| Format | Example | Best Use |
|---|---|---|
| Named | navy, white | Quick experiments and prototypes |
| Hex | #2c3e50 | The 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 |
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.
color and background-color PropertiesThe 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.
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 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.
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.
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.
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.
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.
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.
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.
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 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.
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.
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 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.
em UnitsThe 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.
rem Unitsrem 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.
| Unit | Based On | Compounds When Nested? |
|---|---|---|
| px | A fixed pixel value | No — always absolute |
| em | The current element's font size | Yes — can compound unexpectedly |
| rem | The root html element's font size | No — predictable everywhere |
rem Is Recommendedrem 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 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.
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.
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.
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 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.
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.
| Weight | Common Use |
|---|---|
| 400 (normal) | Standard body text |
| 500 – 600 (medium) | Subheadings, labels, UI text |
| 700 (bold) | Headings and strong emphasis |
The text-align property controls the horizontal alignment of text within its container: left, right, center, and justify.
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.
The text-decoration property controls underlines, overlines, and strikethroughs.
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.
Margins and padding are both spacing properties, but they act in different places. Understanding this distinction is fundamental to CSS layout.
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: 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.
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.
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.
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 draw a visible line around the outside of an element's padding area. The border shorthand property accepts three values: width, style, and color.
| Part | Common Values | Notes |
|---|---|---|
| Width | 1px, 2px, 3px | Borders of 4px or more look heavy unless used intentionally |
| Style | solid, dashed, dotted, none | solid and none are by far the most common |
| Color | Any CSS color value | Often subtle gray, or meaningful (red for errors, green for success) |
You can set borders on individual sides using border-top, border-right, border-bottom, and border-left.
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.
The border-radius property rounds the corners of an element's border box.
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.
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 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.
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.
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.
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.
rgba() and hsla() add transparency. HSL is convenient for systematic color variation.color sets foreground text color and is inherited. background-color sets background color and is not inherited. Text and background combinations must meet WCAG contrast minimums.px is absolute; em is relative to the current element and can compound when nested; rem is relative to the root element and does not compound. rem is recommended for scalable typography.line-height values scale with font size. 1.4–1.7 suits body text; 1.1–1.3 suits large headings.font-weight ranges from 100 to 900; 400 is normal and 700 is bold. A font must include a weight for it to render distinctly.text-align controls horizontal alignment; left alignment is best for body text readability. text-decoration controls underlines and strikethroughs; removing link underlines requires care.margin: 0 auto centers a block element with a defined width.border shorthand sets width, style, and color. border-radius rounds corners; 50% creates a circle and large values create pills.font-family declaration. The browser uses the first available font on the user's system.#. The characters represent red, green, and blue channel values from 00 to ff.rgba() adds an alpha transparency value from 0 to 1.html element. Does not compound through nesting.Read each CSS rule or group of rules carefully. Answer the questions after each one before running the code.
h1 text be in pixels?font-weight: 700 heavier or lighter than the default?line-height: 1.15 mean in terms of actual spacing?0 instead of some positive value?#444444 dark, medium, or light gray?padding: 16px 20px produce on each side?margin-left: auto and margin-right: auto accomplish for this container?padding: 0 24px on a container element?Trace through the CSS rules below and predict the computed style of each element.
h2?h2?h2?h2?For each code block, describe what the rendered element will look like.
Describe the visual appearance of an element with class badge containing the text "New".
Describe the visual appearance of a .card containing an h3 and a p.
Each code block below contains one or more problems. Identify the problem and explain how to fix it.
What two errors are in this rule?
What is the problem with the font-family declaration?
This .warning-box displays yellow with light text. What is the likely problem, and how would you fix it?
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?
Create a CSS file that establishes a clean, readable base for a page. Your stylesheet must include:
bodybodybodyh1, h2, and h3 with distinct sizes, weights, and colors that create clear visual hierarchyGiven 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.
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.
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.
Build a complete readable article page applying everything from this chapter.
header with site title and navigation links.main element containing an article.h1, a byline paragraph, and at least two h2 sections inside the article.footer.body.h1 and h2 using different sizes, weights, and colors — h1 must be clearly the most dominant heading.max-width, centered with auto margins..notice or .callout class with a tinted background, a colored left border, and padding.Answer these questions about your mini-project page or any page you have built this chapter.
rem, calculate the actual pixel size.These questions draw from Chapters 11, 12, and 13.
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?font-family declared on body to apply to all text on the page. Does font-family inherit? How does this affect your stylesheet?margin: 24px auto. What does this shorthand do to each of the four sides?font-size: 1.5rem and the root font size is 18px. What is the computed size in pixels?border: none and border: 0?#aaaaaa on a #ffffff background. What is likely the problem and what would you do to fix it?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.
color is inherited and background-color is not.px, em, and rem, and why rem is recommended.line-height values appropriately for body text and headings.font-weight values for emphasis and hierarchy.margin: 0 auto to center a block element.border shorthand and use border-radius for rounded corners, pills, and circles.rem for font sizes and unitless values for line height.