What is CSS?¶
CSS controls how HTML looks. A paragraph is still a paragraph, but CSS decides its colour, size, spacing, and position on the page.
A first rule¶
A rule has a selector and a declaration block:
Read it as: select every <p> element, then apply these declarations.
A rule can have several declarations:
Where CSS lives¶
External stylesheet¶
Put CSS in a separate file and link it from <head>:
This is the normal choice. It keeps HTML readable and lets you reuse the same styles on several pages.
Internal stylesheet¶
A <style> block inside <head> works for small tests:
Use it for quick experiments, not for a whole site.
Inline styles¶
An inline style applies to one element:
Avoid inline styles for normal styling. They are hard to reuse and hard to override.
A starting stylesheet¶
Start a project with a small reset and readable defaults:
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: system-ui, sans-serif;
line-height: 1.6;
color: #222;
}
box-sizing: border-box makes width calculations easier. The body rule sets a readable font and removes the browser's default margin.
Check your work¶
If your styles do not apply:
- Check the stylesheet path in
href. - Check that the CSS file is saved with a
.cssextension. - Check that the
<link>is inside<head>. - Check for a missing
}or;in the CSS file. - Reload the page after saving.
Try it¶
- Create
index.htmlandstyles.cssin the same folder. - Link the stylesheet from the HTML file.
- Add a heading and two paragraphs.
- Change the body font, text colour, and line height in the CSS.
- Add a rule that makes all links a different colour.
If nothing changes, fix the link before adding more CSS.
Next: Box Model