How To Add CSS
There are three ways to attach CSS to an HTML document. Each one has its place, and you will meet all three in real projects.
| Method | Where the CSS lives | Best for |
|---|---|---|
| External stylesheet | A separate .css file linked with <link> |
Real websites — shared across many pages |
| Internal stylesheet | A <style> element in the page's <head> |
Single pages, demos, quick experiments |
| Inline style | A style="..." attribute on one element |
One-off tweaks, styles set by JavaScript |
External CSS
With an external stylesheet you write your CSS in its own file, for example styles.css:
body {
background: #fafafa;
font-family: system-ui, sans-serif;
}
h1 {
color: #264de4;
}Then every HTML page links to it from the <head>:
<head>
<link rel="stylesheet" href="styles.css">
</head>This is the recommended approach for real sites:
- One file, many pages. Change a color once and every page updates.
- Caching. The browser downloads the file once and reuses it on every page.
- Clean HTML. Content and presentation stay in separate files.
Note: The playgrounds in this course use internal
<style>blocks because each example must fit in a single document. In your own projects, move the CSS to an external file.
Internal CSS
Put a <style> element inside <head>. The rules apply only to that page.
<!DOCTYPE html>
<html>
<head>
<style>
body {
background: linear-gradient(#fff6e5, #ffe0b3);
font-family: sans-serif;
text-align: center;
padding-top: 40px;
}
h1 { color: #d35400; }
p { font-size: 18px; }
</style>
</head>
<body>
<h1>Internal stylesheet</h1>
<p>All styles live in the <style> element of this page.</p>
</body>
</html>Inline CSS
Add a style attribute directly to an element. There is no selector and no braces — just declarations.
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif;">
<h2 style="color: white; background: seagreen; padding: 10px;">Inline heading</h2>
<p style="font-size: 22px; color: purple;">This paragraph is styled inline.</p>
<p>This one has no inline style.</p>
</body>
</html>Inline styles are quick but come with downsides: they cannot be reused, they mix presentation into content, they cannot express hover states or media queries, and they are hard to override from a stylesheet because they have very high priority.
Multiple sources and their order
A page can use all three methods at once. When they set the same property on the same element, the general rule is:
- Inline styles win over stylesheet rules (for equal importance).
- Between stylesheets, whichever rule comes later in the document wins if specificity is equal.
So if you link an external sheet and then add a <style> block, the internal rules override the external ones. If you put the <style> block before the <link>, the external file wins.
<!DOCTYPE html>
<html>
<head>
<style>
p { color: blue; font: 20px sans-serif; }
</style>
<style>
p { color: green; } /* later sheet: overrides blue */
</style>
</head>
<body>
<p>I am green (second style block wins).</p>
<p style="color: crimson;">I am crimson (inline wins).</p>
</body>
</html>Browser default styles
Even with no CSS at all, a page has some styling: headings are big and bold, links are blue and underlined, body has an 8px margin. These come from the browser's built-in user-agent stylesheet, which sits at the very bottom of the cascade. Your styles override them.
Tip: You can link more than one stylesheet, for example
reset.cssfollowed bysite.css. Put the general files first and the specific ones last.
Test yourself
Which element links an external stylesheet?
<style src="a.css"><link rel="stylesheet" href="a.css"><css href="a.css"><script href="a.css">
Where should an internal <style> element normally go?
- Inside
<head> - At the end of
<body> - Inside
<title> - In a separate file
Why are external stylesheets preferred for real websites?
- They are the only way to use classes
- One file can style every page and is cached by the browser
- Inline styles do not work in modern browsers
- They load faster than any HTML