CSS (Cascading Style Sheets) controls the visual appearance of HTML elements. While HTML defines what content is on a page, CSS defines how that content looks — its colour, typography, spacing, layout and animations. The "cascading" in the name refers to the priority rules that determine which styles apply when multiple rules target the same element.
CSS Selectors
- Element —
p { color: red; }targets all paragraphs. - Class —
.btn { padding: 8px; }targets elements withclass="btn". - ID —
#header { }targets the element withid="header". - Pseudo-class —
a:hover { }targets links on mouse hover.
The Box Model
Every HTML element is a rectangular box. From inside out: content → padding → border → margin.
Modern Layout Systems
- Flexbox — One-dimensional layout for rows or columns.
- CSS Grid — Two-dimensional layout for complex page structures.
- CSS Custom Properties (variables) —
--primary: #6366f1;for reusable values.
CSS Specificity
When multiple rules conflict, the more specific rule wins. Specificity order: inline styles > IDs > classes > elements.