Imagine writing a college notice, then deciding its lettering, colour and spacing. HTML describes headings, paragraphs and links. CSS, short for Cascading Style Sheets, supplies presentation rules. Changing text colour does not change a paragraph into a heading.
We have used a few styles in earlier examples. Now we will read the connection carefully. This chapter is a bridge to CSS, not a promise that all page design fits into one lesson.
Read one rule slowly
In h1 { color: #174a94; }, h1 is the selector: it identifies the elements to style. Curly braces contain declarations. Color is the property, meaning the setting; #174a94 is its value. The colon separates property from value, and a semicolon ends the declaration.
Example fragment — use inside a complete program
h1 {
color: #174a94;
}
This rule selects h1 elements in this document. The hash here begins a hexadecimal colour value, not an HTML fragment link. Keep the brackets and punctuation in the right places.
Internal, external and inline are different locations
Internal CSS lives in a style element in head and applies to that document. External CSS lives in a separate .css file connected by link rel=stylesheet. Several pages can share that file. Inline CSS is a style attribute on an individual element.
Use internal CSS for the first complete example so its whole setup fits in one file. Use external CSS for the second so you can practise the real connection. Inline styles can be useful for small demonstrations, but repeating them across many elements makes changes harder. Do not claim that all developers always use one method for every task.
Classes choose a reusable set of elements
A selector .practice-note begins with a dot and matches class=practice-note. A tag selector such as p matches paragraphs. An id selector such as #session matches id=session. The dot and hash belong to CSS selector syntax; they are not part of the class or id attribute value.
A class is useful when several elements share a visual treatment. It does not change their meaning. In these examples the note still uses p, because it is a paragraph, rather than a fake heading chosen for its colour.
Understand the properties used here
Color sets text colour; background-color fills behind the content. Font-family offers Arial and then the generic sans-serif fallback if needed. Line-height=1.6 gives lines room based on the text size. Margin adds outside space. Padding adds inside space between content and border. Border describes a visible edge.
Rem is a unit based on the root text size: 1rem is one root-sized unit, not always exactly 16 physical pixels. Keep useful link underlines and a visible focus outline. The :focus-visible selector styles a focus state when the browser decides it should be shown, commonly during keyboard use.
Create and connect the external file
Save the complete CSS below as practice-page.css beside linked-style.html. The CSS file contains rules only, without style tags or an HTML document wrapper. In the HTML link, rel=stylesheet describes the relationship and href gives the path. Link is a void element.
A stylesheet in another folder needs a matching relative path. CSS url() resource paths, when used later in an external stylesheet, resolve relative to the CSS file. Our teaching stylesheet has no external fonts or images to fetch.
Why a style may not appear
Check the CSS filename, folder, rel value and selector spelling first. Confirm the file is saved. If class=practice-note is misspelled, a correct stylesheet still will not select that paragraph. Browser tools can show missing requests and the rules applied to an element.
When several declarations compete, the cascade decides which wins. Importance, origin, layers, selector specificity and source order can matter. Do not learn the false rule that the last line always wins. For these simple rules with the same precedence and selector, a later declaration for the same property wins. Detailed cascade rules belong in the CSS course.
Keep meaning and usability while styling
A coloured word alone should not be the only way to convey an error. Keep explanatory text and suitable HTML. Do not remove focus outlines merely for appearance. A stylesheet can make a page easier to use, but it cannot repair missing labels or unclear content by itself.
The lesson preview embeds the exact supplied teaching stylesheet locally; copied HTML keeps its normal link. When practising in saved files, both HTML and CSS must be present.
Follow the idea
Connect content and appearance
1HTML: headings, text and links
2Selector: choose elements
3Declaration: property and value
4Link: connect the saved CSS file
CSS changes presentation without changing the meaning of the HTML element.
Complete program: internal-style.html
Save this as internal-style.html. Open it in your browser.
internal-style.html
<!doctype html><htmllang="en"><head><metacharset="UTF-8" /><metaname="viewport"content="width=device-width, initial-scale=1" /><title>A Small Internal Stylesheet</title><style>
body {
color: #202c3a;
background-color: #f5f7fb;
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 1rem;
}
h1 {
color: #174a94;
}
.practice-note {
background-color: #e8f0ff;
border: 1px solid #174a94;
padding: 1rem;
}
a {
color: #174a94;
}
a:focus-visible {
outline: 3px solid #174a94;
outline-offset: 3px;
}
</style></head><body><h1>Give a practice note a clear style</h1><main><pclass="practice-note">Save your page before refreshing the browser.</p><p>This ordinary paragraph has no practice-note class.</p></main></body></html>
Browser previewDocument title: A Small Internal Stylesheet
This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.
Expected result: readable dark text on a light background, a blue heading and a bordered note. The second paragraph does not receive the class-specific note background. All CSS is inside this file.
Complete program: linked-style.html
Save this as linked-style.html. Open it in your browser.
linked-style.html
<!doctype html><htmllang="en"><head><metacharset="UTF-8" /><metaname="viewport"content="width=device-width, initial-scale=1" /><title>Link an External Stylesheet</title><linkrel="stylesheet"href="practice-page.css" /></head><body><h1>Keep presentation in a separate file</h1><main><pclass="practice-note">This note uses the same rules from practice-page.css.</p><p><ahref="internal-style.html">Compare the internal-style version</a></p></main></body></html>
Browser previewDocument title: Link an External Stylesheet
This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.
Expected result: the same visual rules are loaded from practice-page.css. Save both files together. Rename the CSS file temporarily to see the plain fallback, then restore the correct name and refresh. The content remains readable without the styling.
Problem and solution
Problem: the page is plain although the CSS contains correct rules.
Solution: check href and the saved file path. A correct rule cannot apply if the stylesheet was never loaded. Restore the filename, refresh, then check selector spelling.
Common mistakes and useful tips
Do not put style tags inside a .css file.
Do not type .practice-note in the HTML class value.
Do not confuse padding with margin.
Do not use colour as the only instruction.
Do not remove focus indicators.
Do not assume the last rule always wins regardless of precedence.
Practice questions
What does a selector do?
Which part of color: #174a94 is the property?
How does .practice-note connect to HTML?
Which file contains no style tags?
What differs between margin and padding?
Save both stylesheet examples. Rename practice-page.css and refresh linked-style.html, then restore it. What changed?
Check your practice answers
It chooses the elements a rule applies to.
Color.
It matches an element with class=practice-note.
The external practice-page.css file.
Margin is outside the border; padding is inside it.
With the wrong path, the external design disappears while content remains. The corrected path restores the intended rules.
Interview questions
How do HTML and CSS work together?
HTML describes content and meaning. CSS rules select elements and define their presentation.
Why share an external stylesheet?
Several pages can use one set of rules, so an intentional design change can be maintained in one file.
Does a class name create appearance automatically?
No. A matching stylesheet rule must supply the presentation, and a class does not add semantic meaning.
Why can the last declaration fail to win?
The cascade has other ordering rules before source order, including importance and selector specificity. Source order breaks otherwise equal cases.