What is CSS?#
CSS (Cascading Style Sheets) controls how HTML looks - colours, fonts, spacing, layout and even animation. One CSS file can style a whole website, so changing the brand colour once changes every page.
A CSS rule has a selector and declarations:
h1 { /* selector: which elements */
color: #0d6b52; /* property: value; */
font-size: 32px;
}
Three ways to add CSS#
Selectors#
| Selector | Matches | Example |
|---|---|---|
p |
every <p> |
p { line-height: 1.6; } |
.card |
elements with class="card" |
.card { border: 1px solid #ddd; } |
#top |
the element with id="top" (ids are unique) |
#top { height: 60px; } |
h1, h2 |
every <h1> and <h2> |
grouping |
nav a |
<a> anywhere inside <nav> (descendant) |
|
ul > li |
<li> that are direct children of <ul> |
|
a:hover |
a link while hovered | pseudo-class |
li:first-child, li:nth-child(2) |
by position | |
input[type="email"] |
by attribute | |
* |
everything | * { box-sizing: border-box; } |
Use classes for styling - they are reusable. Keep ids for one unique element (and for #links and JavaScript). An element can have many classes: class="card card-featured".
Colours#
| Format | Example | Notes |
|---|---|---|
| name | red, teal, white |
140+ named colours |
| hex | #0d6b52, #fff |
most common in design files |
| rgb / rgba | rgb(13 107 82), rgb(0 0 0 / 50%) |
last value = transparency |
| hsl | hsl(160 78% 24%) |
hue, saturation, lightness - easy to make shades |
Units#
| Unit | Relative to | Use for |
|---|---|---|
px |
fixed pixels | borders, small details |
rem |
root font size (16px default) | font sizes, spacing - respects the user's settings |
em |
the element's own font size | padding that grows with text |
% |
the parent | widths |
vw, vh |
1% of the viewport width / height | full-screen sections |
Text and fonts#
font-family takes a list - the browser uses the first one installed. Always end with a generic family (sans-serif, serif, monospace).
The cascade and specificity#
When several rules set the same property on an element, CSS decides with these rules:
- Importance -
!importantwins (avoid using it). - Specificity - more specific selectors win: id > class / attribute / pseudo-class > element. Inline styles beat all of them.
- Order - if specificity is equal, the rule written later wins.
Some properties inherit from the parent - color, font-family, font-size, line-height. Others don't - border, margin, padding, background. That's why setting font-family on body changes the whole page.
Cascade का मतलब है - जब एक ही element पर कई rules लगें, तो कौन जीतेगा। सबसे पहले specificity देखी जाती है: id सबसे ताकतवर, फिर class, फिर tag का नाम। अगर दोनों बराबर हों तो जो rule बाद में लिखा है वह जीतता है।
Summary#
- CSS rules = selector +
property: value;declarations; use an external stylesheet. - Select by element,
.class,#id, descendant, child, attribute and:pseudo-class. - Colours: names, hex, rgb, hsl. Units: prefer
remfor text,%/vwfor widths. - Conflicts are solved by importance, then specificity (id > class > element), then order.