8. Introduction to CSS
Introduction to CSS
HTML gives your page structure. CSS (Cascading Style Sheets) gives it beauty. Without CSS, every website would look like a plain text document from 1995.
🎨 Your First CSS Rule
A CSS rule has two parts: a selector (which element to target) and a declaration block (the styles to apply). Use <style> tags in the <head> to write CSS alongside your HTML:
🎯 Classes — Targeting Specific Elements
Tag selectors like p target ALL paragraphs. Most of the time you want to style only certain elements. Give them a class attribute and target it with a . prefix:
🏷️ IDs — One Unique Element
IDs are like classes but must be unique on the page. Target them with #. Only use IDs when you're sure there's only one of that element:
🌊 The Cascade — Which Rule Wins?
When multiple CSS rules target the same element, specificity decides which one wins. More specific selectors beat less specific ones:
#idbeats.classbeatstag- When specificity is equal, the last rule in the file wins (the cascade)
!importantoverrides everything (use sparingly!)
Knowledge Check
Ready to test your understanding of 8. Introduction to CSS?