CSS Introduction
CSS stands for Cascading Style Sheets. HTML says what each part of a page is; CSS says how it looks: colours, sizes, spacing and layout.
A CSS rule has a selector, which picks the elements to style, and a set of declarations inside curly braces. Each declaration is a property, a colon, a value and a semicolon.
Without CSS, every website would look like a plain document: black text on white, in one column. CSS is what gives Terixto, Instagram and every other site its colours, fonts, spacing, cards, buttons and layout. The same HTML can look completely different with different CSS.
Cascading means several rules can apply to the same element, and CSS has clear rules for which one wins - generally, the more specific rule wins, and if two are equally specific, the later one wins. Understanding this saves a lot of confusion when a style "doesn't work".
h1 {
color: purple;
font-size: 32px;
}Where CSS goes
- In a separate
.cssfile linked with<link rel="stylesheet" href="style.css">- best for real sites. - Inside a
<style>block in the page head - handy for small examples, like the ones here. - In a
styleattribute on one element - use sparingly.
Keeping CSS in a separate file is best for real websites: one stylesheet can style hundreds of pages, so you change a colour once and the whole site updates. The browser also saves (caches) the file, so pages load faster after the first visit.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; background: #fdf6ec; }
h1 { color: #7c3aed; }
p { color: #444; font-size: 18px; }
</style>
</head>
<body>
<h1>Styled with CSS</h1>
<p>Change the colours above and press Run.</p>
</body>
</html>▶ Try it yourself- CSS controls how HTML looks: colour, size, spacing, layout.
- A rule = selector + { property: value; }.
- Put CSS in a separate file for real sites.
- When rules clash, the more specific (or later) one wins.
