Chapter 2 of 17 · CSS for Beginners — Complete Free Tutorial
Adding CSS to HTML
Before you can style anything, the browser must receive your CSS. There are three ways to include styles in a page — and one clear winner for real projects. This chapter shows each method, the trade-offs, and the production-ready setup.
Method 1: inline styles
An inline style lives in the element's style attribute:
<p style="color: teal; font-weight: 600;">Styled inline.</p>
Expected output: that single paragraph turns teal and semi-bold.
Inline styles have the highest specificity, so they beat almost everything — which sounds handy but is actually a trap: they mix presentation into markup, cannot style pseudo-classes like :hover, and repeat endlessly. Reserve them only for values that truly come from the server (for example a dynamic width from your database).
Method 2: internal stylesheets
A <style> block in the page <head>:
<head>
<meta charset="utf-8">
<title>Internal CSS</title>
<style>
h1 { color: #4f46e5; }
p { line-height: 1.7; }
</style>
</head>
This is perfect for a one-page document or critical email-style HTML, but every page that needs the styles must repeat the block.
Method 3: external stylesheets (the winner)
A separate .css file linked from the head:
<head>
<meta charset="utf-8">
<title>External CSS</title>
<link rel="stylesheet" href="/css/styles.css">
</head>
/* /css/styles.css */
body {
font-family: system-ui, sans-serif;
color: #0f172a;
background: #ffffff;
}
a {
color: #4f46e5;
text-underline-offset: 3px;
}
Expected output: every page that links this file gets the same typography and link colour — change it once, and the whole site updates.
Why external CSS wins
| Concern | Inline | Internal | External |
|---|---|---|---|
| Reuse across pages | No | No | Yes |
| Browser caching | No | No | Yes — loads once |
Style :hover and media queries | No | Yes | Yes |
| Clean, readable HTML | No | Yes | Yes |
| Separation of concerns | No | Partial | Complete |
Best-practice production setup
- Keep all styles in external files, for example
css/base.css,css/components.css. - Put the
<link>in<head>so styles apply before the body paints — this avoids the "flash of unstyled content". - Load the first stylesheet with
rel="preload"if you want it even earlier:<link rel="preload" href="/css/styles.css" as="style" onload="this.rel='stylesheet'"> <noscript><link rel="stylesheet" href="/css/styles.css"></noscript> - Combine and minify files before deploying (your build tool does this automatically).
What order do stylesheets apply in?
When several rules are equal, the last one wins. So this:
<link rel="stylesheet" href="/css/reset.css">
<link rel="stylesheet" href="/css/theme.css">
means theme.css overrides reset.css for equal-specificity rules. Load order is a tool: resets first, then components, then overrides.
Common mistakes
- Relative paths that break —
href="styles.css"resolves against the current page URL. On/blog/postit looks for/blog/styles.css. Prefer root-absolute paths like/css/styles.css. - Blocking fonts/styles in the wrong place — stylesheets in the
<body>block rendering; keep them in the head. - Inline-style every element — it feels fast on page one and becomes unmanageable by page fifty.
Practice checklist
- Move the example from chapter 1 into an external
styles.cssand link it. - Add a second stylesheet that only overrides the heading colour — confirm order decides the winner.
- Right-click → Inspect → check the Network tab: the stylesheet loads once and gets cached on reload.
With CSS attached, you are ready to target elements precisely — CSS Selectors Basics is next.
Frequently Asked Questions
CSS (Cascading Style Sheets) is the language that controls how HTML content looks — colours, fonts, spacing, layout, responsiveness and animation. HTML provides structure and meaning; CSS provides presentation. One stylesheet can restyle an entire website at once.
The fundamentals (selectors, box model, Flexbox, media queries) take about 2–3 weeks of daily practice. Grid, animations and architecture come with a little more practice. This 17-chapter series is designed for 3–4 weeks at a relaxed pace — faster if you type out every example.
Yes. HTML first, then CSS, then JavaScript. CSS and HTML together let you build complete, good-looking static pages, and almost every JavaScript framework assumes you already know CSS layout and selectors.
No — CSS is a styling language with selectors and property-value declarations, not variables, loops or conditionals. You can get far without any programming logic, and JavaScript adds the behaviour later.
Flexbox distributes space along one axis (a row or a column) and is ideal for components — toolbars, navbars, card rows. Grid controls rows and columns together and is ideal for page structure and card grids. They are complementary: many layouts use a grid page shell with flex components inside.
Usually one of three reasons: the selector does not match (check class names), a more specific rule wins (use the browser inspector to see computed styles and specificity), or the file is not linked/loads in the wrong order. The specificity chapter explains how the cascade decides.
Every element is a box of four layers: content, padding (inner space), border, and margin (outer space). With box-sizing: border-box, the width you set includes padding and border — the universal reset every project uses.
Use relative units, flexible grids like repeat(auto-fit, minmax(260px, 1fr)), images with max-width: 100%, and mobile-first media queries. The goal is zero horizontal scrolling from 320px up — the responsive chapters show the full workflow.
Not to learn. Frameworks are productivity tools built on the same CSS you are learning here; understanding the fundamentals makes any framework easier to use (and to debug when it fights you). Learn vanilla CSS first, then evaluate frameworks with an informed eye.
JavaScript for interactivity, then a build tool and version control (Git). With HTML, CSS and JavaScript you can build complete websites; after that, frameworks like React or server-side tools like Laravel Blade build on these fundamentals.