Sub topic 1
How HTML and CSS Work Together
HTML and CSS are separate languages that the browser combines into one rendered page. This chapter explains what each one does, how the browser merges them, and why the later chapters can offer three different ways to attach CSS to the same HTML. Those three ways are the style attribute, the style element, and a linked stylesheet.
Prerequisites
This chapter assumes you can already do the following:
- Read a basic HTML document and recognize its structure:
html,head,body, and common elements such ash1,p,a, andul. - Create and save a plain text file, then open it in a browser, either by double-clicking it or by dragging it into a browser window.
- Understand what a file path is. For example,
css/site.cssmeans a file namedsite.cssinside a folder namedcss, relative to the current file.
You do not need to know any CSS yet. The syntax is introduced as needed here and covered properly in the next chapter.
Two languages, two jobs
HTML (HyperText Markup Language) describes what the content is. A heading is a heading, a list is a list, and a link points somewhere. HTML says nothing about colors, fonts, or spacing. Those choices belong to CSS (Cascading Style Sheets), which describes how that content should look.
Here is a small HTML document with no CSS at all:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Plain page</title>
</head>
<body>
<h1>Field notes</h1>
<p>Today I saw a heron by the river.</p>
<p>It stood still for almost ten minutes.</p>
</body>
</html>Open it and you will see a large bold heading and two paragraphs in a serif font on a white background. You wrote no CSS, yet the page has a look. That look comes from the browser's built-in stylesheet, usually called the user agent stylesheet. Every browser ships one, and it gives headings a larger size, paragraphs some vertical space, and links an underline and a blue color. Your own CSS overrides these defaults. It never starts from nothing.
The structure of a page, and therefore its meaning, can be understood without CSS. Screen readers, search engines, and text-only browsers rely on that structure. CSS adds presentation on top, and removing it should leave a readable page.
What the browser does with both
The browser does not run your HTML and CSS as one program. It reads them separately and then combines the results.
- It downloads the HTML and parses it into the DOM (Document Object Model), a tree of objects that represents the elements.
- While parsing, it finds CSS from
styleelements,styleattributes, andlinkelements, and downloads any linked files. - It parses all the CSS into a second structure, the CSSOM (CSS Object Model), a collection of rules.
- It matches rules to elements and works out one final value for each property of each element. This step is called computing styles, and the cascade drives it.
- It calculates sizes and positions (layout) and then draws the result to the screen (paint).
The diagram shows why CSS from several places can coexist. Whichever method delivers the CSS, the text ends up in the same pool of rules and goes through the same matching step. The method only changes where the browser finds the rules, when it finds them, and how much weight they carry when they conflict.
Rules connect the two languages
CSS reaches HTML elements through selectors. A selector is a pattern that says which elements a rule applies to. The simplest selector is an element name:
p {
color: darkblue;
}This rule says: every p element gets the text color darkblue. The part inside the braces is a declaration, a property (color) paired with a value (darkblue). The next chapter dissects this syntax. For now, note the division of labor. HTML supplies the elements, CSS supplies selectors that find them, and the browser applies the declarations to every match.
HTML also provides hooks for CSS to grab. The two most common are the class attribute, which can be shared by many elements, and the id attribute, which should be unique on a page:
<p class="note">Remember to bring water.</p>
<p id="intro">Welcome to the trail guide.</p>CSS can then target .note or #intro without touching the element name. Good HTML naming makes CSS easier to write, and a well-organized stylesheet means you rarely need to edit the HTML to change the design.
Same HTML, different look
Because the two languages are independent, one HTML document can take on very different appearances. Here is the field notes page again, with an internal stylesheet added:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Styled page</title>
<style>
body {
font-family: system-ui, sans-serif;
max-width: 36rem;
margin: 2rem auto;
background-color: #fdf6e3;
color: #333333;
}
h1 {
color: #8b3a1a;
border-bottom: 2px solid #8b3a1a;
}
</style>
</head>
<body>
<h1>Field notes</h1>
<p>Today I saw a heron by the river.</p>
<p>It stood still for almost ten minutes.</p>
</body>
</html>The HTML inside body is identical to the first version. Only the CSS changed. Swap the colors and the font and the same content looks completely different. Delete the style element and the page returns to the browser defaults.
Notice that the p elements never mention a color, yet they appear dark gray. The color property is inherited. When you set it on body, the descendant elements use the same value unless a rule says otherwise. Not every property inherits. border, for example, does not, so the h1 border does not spread to other elements. Inheritance is part of why you can style a whole page with a few rules.
Three ways to deliver the same CSS
CSS can arrive in three places. All three feed the same pool of rules described above.
| Method | Where the CSS lives | Reach |
|---|---|---|
| `style` attribute (inline) | On one element, in the HTML | That one element |
| `style` element (internal) | In the `head` of one HTML file | Any matching element on that one page |
| `link` element (external) | In a separate `.css` file | Any page that links to the file |
The next three chapters explain each method in turn. The rest of the tutorial then covers what happens when they overlap, how to inspect the result in developer tools, and how to choose among them.
Here is the smallest possible example of each, applying the same effect:
<!-- Inline -->
<p style="color: crimson;">Heron</p>
<!-- Internal, inside <head> -->
<style>
p { color: crimson; }
</style>
<!-- External, inside <head> -->
<link rel="stylesheet" href="css/site.css">The inline version affects one paragraph. The other two affect every p that the page loads rules for. In the external case, css/site.css would contain p { color: crimson; }.
Why the separation matters
Keeping content and presentation apart is a design principle with practical effects.
- Reuse. One stylesheet can serve hundreds of pages. Change one rule and every page updates.
- Maintenance. Designers can edit CSS while writers edit HTML, with less risk of breaking each other's work.
- Accessibility. Users can override your styles with their own, for larger text or higher contrast, because the browser treats the content as separate from the look.
- Adaptability. CSS can target conditions such as screen width or printing, so one HTML file can serve phones, desktops, and paper.
- Weight. Repeating style information in every element makes pages larger and harder to read.
Inline styles weaken most of these benefits, which is why they are usually reserved for special cases. The final chapters of this tutorial cover those trade-offs in detail.
Common errors at this stage
Most early problems come from the browser silently ignoring something. Neither HTML nor CSS stops with an error message when it meets a mistake. They skip what they do not understand and continue.
- A misspelled property.
colour: red;is not valid, so the declaration is ignored. Nothing turns red and no message appears. - A missing semicolon. In
color: red font-weight: bold;the browser cannot parse the first declaration properly and drops it, and you may lose the second one too. - CSS written directly in the body. Writing
p { color: red; }as plain text outside astyleelement makes it appear on the page as visible text. CSS rules need astyleelement, a stylesheet file, or an attribute to carry them. - A wrong path. If
href="css/site.css"points to a file that does not exist relative to the HTML file, the link fails quietly and the page renders with defaults. Check the folder name and the capitalization. Many servers treatSite.cssandsite.cssas different files. - HTML tags inside a
.cssfile. An external stylesheet holds only CSS. A<style>tag in it breaks the first rule that follows. - Editing and not refreshing. The browser may show a cached copy of a stylesheet. Reload the page, or force a refresh, before concluding that a change did not work.
What comes next
You now have the model. HTML builds the DOM and CSS builds a set of rules. The browser matches them and computes one final value for each property. In the next chapter you will take the rule apart into selector, property, and value, which gives you the vocabulary to use each of the three methods correctly.
Exercise
A page has no CSS written by you, yet its headings are large and bold and its links are blue and underlined. Where do those styles come from, and what happens when you write your own rule for h1?
Show answer
They come from the browser's built-in user agent stylesheet. When you write your own rule for h1, your declarations take precedence over the browser defaults for any property you set, and properties you do not set keep the default values.
Exercise
You add the line p { color: green; } as plain text inside the body of an HTML file, and the paragraphs stay black. What is the problem, and what are two correct ways to fix it?
Show answer
The CSS is not inside any element or file that the browser treats as CSS, so it is displayed as ordinary text. Fix it by placing the rule inside a style element in the head, or by saving it in a .css file and linking it with <link rel="stylesheet" href="...">.
Exercise
Name the two data structures the browser builds from HTML and CSS, and say what step combines them.
Show answer
The HTML becomes the DOM tree and the CSS becomes the CSSOM, a set of rules. The browser then matches rules to elements and computes the final style of each element, which feeds layout and paint.