Inspired by CS50 WebTrack
Thanks to w3schools
CSS (Cascading Style Sheets) defines the style, appearance, and formatting of websites. It gives a consistent look to every page on a site.
CSS can be written as inline styles directly in your HTML, or stored in an
external file (e.g. styles.css) and linked to it. Both approaches have trade-offs.
| External CSS file | Inline styles | |
|---|---|---|
| Consistency | Changes affect the whole site at once, ensuring a consistent look. | Hard to keep a consistent look — changes must be made on every page. |
| Maintenance | Content and formatting are separate, making code easier to read and update. | Formatting is mixed in with content, which can get messy on large pages. |
| Performance | The stylesheet is cached by the browser, so subsequent pages load faster. | Formatting is reloaded with every page, which can slow the site down. |
| Flexibility | Stylesheets can be swapped for different themes or devices (e.g. mobile). | Useful for quick, one-off changes to a single element without touching a stylesheet. |
Selectors are how you identify which elements to apply CSS to — for example, all <p> tags,
elements with a certain class attribute, or a single element identified by its id.
| topic | description | code | output |
|---|---|---|---|
| Inline styles |
Apply CSS directly to an element using the style attribute.
Highest specificity but hardest to maintain — avoid for large projects.
|
|
Hello! |
| Classes |
Define a reusable style in CSS using .className, then apply it with the class attribute.
The same class can be assigned to many elements.
|
|
Important text Also important |
| Identifiers (id) |
Apply a style to one unique element using #idName in CSS and id="idName" in HTML.
Only one element can have a given id.
|
|
My Title |
background-color |
Sets the background colour of an element. Note the American spelling — background-color not background-colour. Accepts colour names, hex codes, or rgb() values.
|
|
Green background Hex orange background |
border-color |
Sets the colour of an element's border. Requires border-style to also be set, otherwise the border won't show.
|
|
Red border |
border-style |
Sets the style of the border. Common values: solid, dashed, dotted, double, none.
|
|
Solid Dashed Dotted |
border-width |
Sets how thick the border is. Can use pixels (px), or keywords like thin, medium, thick.
|
|
1px 5px thick |
color |
Sets the foreground (text) colour. Note the American spelling — color not colour — as with all CSS properties.
Accepts colour names (e.g. red) or hex codes (e.g. #ff0000).
|
|
Blue text (colour name) Red text (hex #ff0000) |
font-family |
Sets the typeface. List fallbacks separated by commas — the browser uses the first available font.
Generic families like serif, sans-serif, monospace should go last.
|
|
Georgia (serif) Arial (sans-serif) Courier New (monospace) |
font-size |
Sets how large the text is. The most common unit is px (pixels). 16px is the typical browser default.
|
|
12px — small text 20px — larger text 32px — large text |
height |
Sets the height of an element. Works well on block elements or elements with a defined size like images.
Can use px, %, or auto.
|
|
height: 60px
height: 120px
|
width |
Sets the width of an element. Can use px for a fixed width or % to be
relative to the parent container.
|
|
150px wide
75% wide
|