⚡0 XP🔥0

CSS Basics - Selectors, Colours and Fonts

Lesson 4 of 6 ⏱ 5 min read ❓ 5-question quiz ▶ Runs HTML

How CSS styles HTML - the three ways to add CSS, selectors (element, class, id, descendant, pseudo-class), colours, units, fonts, the cascade and specificity.

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#

▶ Try it YourselfHTML
Ctrl + Enter to run

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; }
▶ Try it YourselfHTML
Ctrl + Enter to run
Tip

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#

▶ Try it YourselfHTML
Ctrl + Enter to run

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:

  1. Importance - !important wins (avoid using it).
  2. Specificity - more specific selectors win: id > class / attribute / pseudo-class > element. Inline styles beat all of them.
  3. Order - if specificity is equal, the rule written later wins.
▶ Try it YourselfHTML
Ctrl + Enter to run

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 rem for text, %/vw for widths.
  • Conflicts are solved by importance, then specificity (id > class > element), then order.

Quick Quiz

Q1.

Which way of adding CSS is best for a real website with many pages?

Q2.

Which selector matches <p class="note">?

Q3.

Two rules set the colour of the same paragraph: p { color: blue; } and .intro { color: red; }. The paragraph has class intro. What colour is it?

Q4.

What does 1rem usually equal?

Q5.

What does a:hover { color: orange; } do?