Lesson 5 ยท about 15 minutes
๐จ CSS: Time to Paint the House
Your product page has strong walls now. But it's still gray and boring, like an unpainted house. Today you grab the paint bucket โ and your page suddenly looks DESIGNED.
๐ฌ Watch first (90 seconds)
Meet the paint
Remember the house? HTML = walls, CSS = paint, JavaScript = electricity. Until today you only built walls. CSS (Cascading Style Sheets) is the language that tells the browser how things should LOOK: colors, sizes, fonts, everything pretty.
And here's the great news: a CSS rule is a tiny sentence with only three parts.
h1 {
color: purple;
}
- The selector says WHO gets painted โ
h1means "all the h1 headlines!" - The property says WHAT about them โ
colormeans "their text color". - The value is your choice โ
purple. Because purple is awesome.
; at the end of each line and the { } curly braces around the rules.
Where do these rules live? Inside a <style> element, which goes in the <head> of your page โ the invisible backpack part from lesson 2. Here are the five paint colors on your brush today:
colorโ the color of the textbackground-colorโ the color BEHIND the textfont-familyโ the shape of the letters (trysans-serifโ clean and modern)font-sizeโ how big the letters are (like20px)text-alignโ where the text sits:left,center, orright
For colors, you can just use their names: purple, gold, tomato, hotpink, midnightblue... yes, tomato is a real CSS color. ๐
๐ ๏ธ Build it (the fun part)
- Open
practice/myshop.htmlin VS Code. - Find the
<head>section (up near the top, where the<title>lives). - Right after the
<title>line, add a<style>block. Type it โ don't copy-paste! Pick YOUR OWN colors:<style> body { background-color: lightyellow; font-family: sans-serif; } h1 { color: tomato; text-align: center; } </style> - Save (โS), then refresh the browser (โR).
- WHOA. ๐คฏ Background color, new letters, centered headline โ your page just went from "homework" to "designed". Try swapping the colors a few times. Nobody paints a house right on the first try.
๐น๏ธ Try it right here โ the lesson checks your code!
Type in the dark box and watch the preview. The preview understands CSS too โ the moment you type a color, it PAINTS. Then press Check my code โ.
๐จ Now break it!
Time to break your paint job in practice/myshop.html and see what happens. Predict what will happen BEFORE you refresh!
- Misspell a property: change
color: tomato;tocolr: tomato;. Save, refresh. Nothing breaks... the color just quietly disappears! CSS never shows an error โ the browser silently ignores lines it doesn't understand. Sneaky! With HTML the page went weird; with CSS it just shrugs. Fix the spelling and the color comes back. - Delete one closing
}from your first rule. Save, refresh. Watch every rule AFTER it die too. One missing brace can knock out half your paint job. - Put the
}back. Now add a new rule:p { font-size: 50px; }. Giant paragraphs! Try8pxtoo. Which size is nicest to read?
<style> and </style>? Is every property spelled exactly right (background-color, with the dash)? And did you save before refreshing? CSS won't complain โ it just quietly ignores mistakes.
๐ฎ Quiz time
No peeking at the notes above โ trying to remember is what makes it stick!