Sub topic 1
How HTML and CSS Work Together
Prerequisites
This chapter assumes you can do three things:
- Read a basic HTML document:
html,head,body, and common elements such ash1andp. - Save a text file and open it in a browser.
- Understand a relative file path such as
css/site.css.
You do not need to know any CSS yet. The next chapters cover the syntax in detail.
Two languages, two jobs
HTML and CSS solve different problems.
- HTML describes what the content is: a heading, a paragraph, a list, a link.
- CSS describes how that content looks: color, spacing, font, and layout.
Keeping the jobs apart means you can change the look of a whole site without touching its content. You can also rewrite the content without breaking the design.
Here is a page with no CSS at all:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>No styles</title>
</head>
<body>
<h1>Field notes</h1>
<p>Browsers apply built-in default styles to this page.</p>
</body>
</html>The page is not unstyled. Every browser ships a user agent stylesheet, a built-in set of CSS rules. It makes h1 large and bold and puts space between paragraphs. When you add your own CSS, you override those defaults.
What the browser does with both
A browser handles HTML and CSS in separate steps, then combines the results.
- It parses the HTML into the DOM (Document Object Model), a tree of elements.
- It finds the CSS that applies to the page and parses it into a second structure, the CSSOM (CSS Object Model).
- It matches rules to elements and computes the final style of each element.
- It lays out the elements and paints them on screen.
The word "all" in "CSS from all sources" matters. A page can receive CSS from several places at once, and the browser merges them before it computes any element's style.
The sources of CSS
A single page can get styles from these places:
| Source | Where it lives | Scope |
|---|---|---|
| User agent stylesheet | Built into the browser | Every page |
| `style` attribute | On one element | That element only |
| `style` element | In the page's `head` | That one page |
| Linked stylesheet | A separate `.css` file | Every page that links it |
The last three are the ways you attach CSS yourself. Later chapters take them one at a time. For now, here is the smallest possible example, using a style element to change the earlier page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>With a style</title>
<style>
h1 {
color: darkblue;
}
</style>
</head>
<body>
<h1>Field notes</h1>
<p>Only the heading changed color.</p>
</body>
</html>The HTML is the same as before. The only change is that the head now contains CSS, and the browser colors the heading dark blue. The paragraph keeps its default look.
The link between the two
CSS has no effect on its own. It works because it selects HTML elements. A rule such as h1 { color: darkblue; } says "find every h1 in the DOM and set its color." If the HTML has no h1, the rule matches nothing.
This gives you two things to check when a style does not appear:
- Does the CSS reach the page? Look for a wrong file path, a missing
linkelement, or a typo in the file name. - Does the selector match the element you meant? Look for a misspelled element name.
Common mistakes
- Using HTML for appearance. Choosing
h1because it looks big is a mistake. Pick elements for meaning and use CSS for size. - Expecting a file to load without a link. Saving
site.cssnext to the page does nothing until the page references it. - Editing the CSS file but seeing no change. The browser may be showing a cached copy. Reload the page without the cache, or check that you edited the file the page actually links to.
Exercise
A page has an h1 and a stylesheet rule h2 { color: red; }. The stylesheet loads correctly, but the heading stays black. What is the most likely cause, and which two steps in the browser's process does it involve?
Show answer
The selector does not match the element. The rule targets h2, but the page has only an h1. The CSS parsed correctly into the CSSOM, but in the step that matches rules to elements, no element in the DOM matches h2, so no style is applied. Changing the selector to h1 fixes it.