HTML CSS
HTML describes what is on a page: headings, paragraphs, images, links. CSS (Cascading Style Sheets) describes how it should look: colors, fonts, spacing, sizes and layout. Keeping the two separate means you can restyle a whole site without touching its content.
Three ways to add CSS
| Method | Where it lives | Best for |
|---|---|---|
| Inline | A style attribute on one element |
Quick one-off tweaks and demos |
| Internal | A <style> element in the page's <head> |
Styles used by a single page |
| External | A separate .css file linked with <link> |
Real websites with many pages |
Inline CSS
Inline styles apply to exactly one element. They are handy for testing, but they mix content with presentation and are hard to maintain.
<h1 style="color: darkcyan; font-family: Georgia, serif;">Inline styled heading</h1>
<p style="font-size: 20px; letter-spacing: 2px;">This paragraph has inline styles.</p>
<p>This paragraph has none.</p>Internal CSS
An internal stylesheet goes inside a <style> element, normally in the <head>. Each rule has a selector (which elements to style) and a block of declarations (property: value;).
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: system-ui, sans-serif;
background-color: #f4f7fb;
}
h1 {
color: #2a4d8f;
}
p {
color: #333;
line-height: 1.6;
}
</style>
</head>
<body>
<h1>Internal stylesheet</h1>
<p>Every paragraph on this page gets the same color and line height.</p>
<p>Change a rule once and all matching elements update.</p>
</body>
</html>External CSS
On a real site you put rules in a file such as styles.css and link it from every page:
<head>
<link rel="stylesheet" href="styles.css">
</head>The browser downloads the file once and caches it, so every page loads faster and shares one consistent look.
Note: The playgrounds on this site run a single document, so they use internal
<style>blocks instead of external files. The rules work exactly the same way.
Anatomy of a CSS rule
p {
color: navy;
font-size: 18px;
}pis the selector — it matches every<p>element.colorandfont-sizeare properties.navyand18pxare values.- Each declaration ends with a semicolon, and the block is wrapped in curly braces.
Common properties to start with
| Property | Example | Effect |
|---|---|---|
color |
color: crimson; |
Text color |
background-color |
background-color: #eee; |
Background color |
font-family |
font-family: Arial, sans-serif; |
Typeface (with fallbacks) |
font-size |
font-size: 1.25rem; |
Text size |
border |
border: 2px solid black; |
Width, style and color of the border |
padding |
padding: 12px; |
Space inside the border |
margin |
margin: 20px; |
Space outside the border |
text-align |
text-align: center; |
Horizontal alignment of text |
Padding, border and margin together form the box model. Every element is a rectangle: content, then padding, then border, then margin.
<style>
.box {
background-color: lightyellow;
border: 4px dashed orange;
padding: 20px;
margin: 30px;
font-family: sans-serif;
}
</style>
<div class="box">
The yellow area is content plus padding. The dashed line is the border.
The empty space around it is the margin.
</div>The "cascading" part
When several rules target the same element, CSS decides which one wins using a few principles:
- Specificity — more specific selectors win. An
idbeats aclass, which beats a tag name. - Order — if specificity is equal, the rule written later wins.
- Inline styles beat rules in a stylesheet.
<style>
p { color: gray; }
.note { color: green; }
#special { color: purple; }
</style>
<p>Matched only by the tag selector: gray.</p>
<p class="note">The class rule is more specific: green.</p>
<p class="note" id="special">The id rule wins over both: purple.</p>
<p class="note" style="color: red;">An inline style overrides everything above: red.</p>Tip: Prefer classes for styling. They are reusable and keep specificity low, which makes your CSS easier to override later.
Test yourself
Which element links an external stylesheet to an HTML page?
<style src="styles.css"><link rel="stylesheet" href="styles.css"><css href="styles.css"><script src="styles.css">
Which property adds space between an element's content and its border?
marginspacingpaddinggap
A paragraph matches both p { color: blue } and .alert { color: red }. What color is it?
- Blue
- Red
- Purple
- It depends on the browser