CS Logo

Web Coding Camp

Day 2 — CSS: Color, Type & the Box Model

Selectors · Classes & ids · Colors · Fonts · Box model · Hover, gradients & more

⬇ Today's slides (PDF)

Schedule

TimeActivity
0:00 – 0:45Lecture: separating content from style, selectors
0:45 – 1:30Project: style yesterday's page
1:30 – 1:40Break
1:40 – 3:00Project: make it yours — colors, fonts, spacing

What CSS Is

HTML is your content. CSS is how it looks — colors, fonts, spacing, layout. Keeping them separate means you can restyle a whole page without touching the content.

You write CSS as a list of rules. Each rule picks an element (the selector) and sets some properties:

selector {
  property: value;
  property: value;
}

Yesterday you used a <style> block inside the page. Today, move your styles into their own file — cleaner, and one stylesheet can style every page. Connect it by adding this line inside the <head>:

<link rel="stylesheet" href="style.css">

Then make a file called style.css next to your HTML. Everything below goes in there.

Quick Reference

Selectors — Choosing What to Style

h1 { }              /* every <h1> tag */
p  { }              /* every paragraph */

.card { }           /* every element with class="card"  (dot = class) */
#header { }         /* the one element with id="header"  (# = id) */

nav a { }           /* every <a> inside a <nav> */

Add a class to any HTML element so you can target it:

<p class="intro">Style me differently.</p>

Colors and Text

body {
  color: #222;                 /* text color */
  background: #f4f4f8;          /* background color */
  font-family: system-ui, sans-serif;
  font-size: 18px;
  line-height: 1.6;            /* spacing between lines */
}
h1 {
  color: #6a1b9a;
  text-align: center;
}
Colors: write a name like red, or a hex code like #6a1b9a. Grab a hex from a color picker at htmlcolorcodes.com/color-picker, or a whole matching palette at coolors.co.

The Box Model — Spacing

Every element is a box with three layers of spacing. This is the single most important idea in CSS.

.card {
  padding: 1rem;               /* space INSIDE the box, around the content */
  border: 2px solid #ccc;      /* the line around the box */
  margin: 1rem;                /* space OUTSIDE the box, pushing others away */
  border-radius: 12px;         /* rounded corners */
}

Sizing and Centering

body {
  max-width: 700px;            /* don't stretch wider than this */
  margin: 0 auto;              /* auto left/right margins = centered */
}
img {
  width: 200px;
  border-radius: 12px;
}

The <div> Tag — a Box to Group Things

A <div> is a plain container. On its own it does nothing and shows nothing — but it lets you group related elements so you can style or position them as one block. It's the workhorse tag of every layout.

<div class="card">
  <h2>Dog Walking Service</h2>
  <p>Reliable walks in your neighborhood.</p>
</div>

Wrap related elements in a <div>, give it a class, then style the whole box at once (like the card look below). You'll lean on <div> constantly — for cards, sections, and the layouts we build on Day 3.

Class vs. id — and the CSS for Each

You need a way to style some elements but not others. That's what classes and ids are for. You add them in the HTML, then target them in the CSS.

A class — reuse it on many elements

Add class="..." in the HTML, then style it with a dot in CSS:

<p class="note">Watch out for this!</p>
<p class="note">And this too.</p>
.note {
  color: darkred;
  font-weight: bold;
}

An id — for one special element

Add id="..." in the HTML, then style it with a hash in CSS:

<div id="hero">Welcome!</div>
#hero {
  background: #1a1a2e;
  color: white;
  padding: 2rem;
}
The difference: a class can repeat on as many elements as you like; an id should appear once per page. When in doubt, use a class.

A Bit More CSS

A few extra tricks that make a page feel polished.

Hover effects

Add :hover to a selector to change something when the mouse is over it. transition makes the change fade smoothly instead of snapping.

a {
  color: #6a1b9a;
  transition: color 0.2s;      /* animate the change */
}
a:hover {
  color: tomato;
}

Gradients and shadows

/* a gradient background */
body { background: linear-gradient(#a18cd1, #fbc2eb); }

/* a soft shadow under a card */
.card { box-shadow: 0 4px 12px rgba(0,0,0,0.2); }

Color variables

Name a color once, reuse it everywhere. Change your whole theme from one spot:

:root {
  --brand: #6a1b9a;            /* define it once */
}
h1     { color: var(--brand); }
a:hover { color: var(--brand); }

Project — Style Your Page

Add a <link> to a stylesheet, then style yesterday's page. This CSS works no matter which theme you picked — start here, then change every value to your taste.

Starter Code — style.css

body {
  font-family: system-ui, sans-serif;
  background: #f4f4f8;
  color: #222;
  max-width: 700px;
  margin: 0 auto;              /* centers the page */
  padding: 2rem;
  line-height: 1.6;
}

h1 {
  color: #6a1b9a;
  text-align: center;
}

img {
  width: 200px;
  border-radius: 12px;
}

a {
  color: #6a1b9a;
}
Don't forget: add <link rel="stylesheet" href="style.css"> inside your HTML's <head>, or none of this will show up.

Stretch Goals

  1. Add a hover effect on your links: a:hover { color: red; }
  2. Use a Google Font — grab the <link> from fonts.google.com
  3. Give the page a gradient background: background: linear-gradient(#a18cd1, #fbc2eb);
  4. Add a shadow to an image or box: box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  5. Style a <button> even if it doesn't do anything yet (we'll wire it up on Day 4)

A Fancier Font in Two Lines

The built-in fonts are fine, but a Google Font makes a page feel designed. Pick one at fonts.google.com, then do two things.

1. Paste the <link> Google gives you into your <head>:

<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">

2. Use that font's name in your CSS:

body { font-family: "Poppins", sans-serif; }
Two different spots: the <link> goes in your HTML's <head>; the font-family goes in your CSS. Google Fonts hands you both — just copy each into place.

Color & Units Cheat Sheet

Ways to write a color

FormExampleWhen
Namedtomato, rebeccapurpleQuick and readable — ~140 names exist
Hex#ff6347Exact color, e.g. from coolors.co
RGB w/ opacityrgba(0,0,0,0.2)See-through colors (great for shadows)

Common size units

UnitMeansGood for
pxexact pixelsborders, fixed image widths
remmultiples of the base text sizefont sizes, spacing (scales nicely)
%percent of the parentwidths that flex with the screen
Rule of thumb: reach for rem for fonts and spacing, px for thin borders, and % for widths that should stretch.

Common Mistakes