๐Ÿ”’ Members-only lesson โ€” thanks for supporting devnotes
← back to course
progress is saved in this browser โ€” no account needed
devnotes / HTML & CSS Fundamentals / Lesson 2
lesson 2 of 6 ยท premium

Styling With CSS: Colors, Fonts & Spacing

22 min ยท HTML & CSS Fundamentals

In Lesson 1, you built a page that worked โ€” but it looked like a plain text file. CSS is what turns structure into design. By the end of this lesson, your "Hello, world!" page will actually look like something you'd want to show someone.

Three ways to add CSS to a page

Before styling anything, you need to know where CSS actually lives. There are three ways to attach it to your HTML, and only one of them is worth using regularly.

// 1. Inline โ€” directly on one element (avoid this, hard to maintain) <p style="color: blue;">Hello</p> // 2. Internal โ€” inside a <style> tag in your <head> <style> p { color: blue; } </style> // 3. External โ€” a separate .css file (the standard approach) <link rel="stylesheet" href="style.css">

For any real project, use an external stylesheet. It keeps your HTML clean and lets you reuse the same styles across multiple pages โ€” exactly how professional sites are built.

Selectors: telling CSS what to style

A selector is how you point at the HTML element you want to style. The most common ones:

SelectorTargetsExample
h1Every <h1> on the pageh1 { color: navy; }
.classnameAny element with class="classname".card { padding: 10px; }
#idnameThe one element with id="idname"#header { background: black; }

Use classes for anything you'll style more than once (like .card for repeated content boxes), and IDs only for something unique on the page, like a single header.

Leveling up: combining selectors and pseudo-classes

Real stylesheets rarely use just one selector at a time. A few patterns you'll use constantly:

h1, h2, h3 { /* comma = apply to all three */ font-family: 'Georgia', serif; } .card p { /* space = "a <p> inside something with class card" */ color: gray; } a:hover { /* :hover = only while the mouse is over it */ color: orange; } li:first-child { /* :first-child = only the first item in a list */ font-weight: bold; }

Pseudo-classes (the colon syntax like :hover) target a state or position rather than a fixed element โ€” they're how buttons change color when you hover over them, or how the first item in a navigation menu gets special styling.

quick tip
If two rules conflict on the same element, CSS uses "specificity" to decide which wins โ€” as a rule of thumb, an ID beats a class, and a class beats a plain tag selector. When something isn't styling the way you expect, this is usually why.

Color: three ways to write it

mint
amber
ink
color: red; /* named color โ€” limited options */ color: #3fa980; /* hex โ€” most common in real projects */ color: rgb(63, 169, 128); /* rgb โ€” same color as the hex above */

Named colors are fine for quick tests, but hex codes are what you'll see in almost every real codebase โ€” they give you access to any color, not just a limited named list.

Fonts: family, size, and weight

body { font-family: 'Georgia', serif; font-size: 16px; font-weight: 400; } h1 { font-weight: 700; /* bold */ }

Always list a fallback after your chosen font (like serif or sans-serif above) โ€” if the browser can't load your font, it falls back to something similar instead of breaking the layout.

quick tip
Want fonts beyond the handful built into every browser? Google Fonts (fonts.google.com) is free โ€” it gives you a <link> tag to paste in your <head>, then you reference the font name in your CSS exactly like any other font.

Spacing: your first look at margin and padding

Two properties control the space around and inside elements. We'll go much deeper into this in Lesson 3, but here's the difference in one sentence: padding is space inside the element's border, margin is space outside it, pushing other elements away.

.card { padding: 20px; /* space between the border and the content inside */ margin: 16px; /* space between this element and its neighbors */ }

Try it yourself

exercise
Open the index.html file you built in Lesson 1. Add a <style> block inside <head>, and give your <h1> a color of your choice, your <body> a font-family, and your <p> some padding. Refresh the page in your browser and watch it actually start to look designed.

Units: px, %, em, and rem

Every size in CSS needs a unit, and picking the right one matters more than beginners expect.

UnitMeansUse it for
pxA fixed pixel size, never changesBorders, small fixed details
%Relative to the parent element's sizeWidths that should flex with the container
emRelative to the current element's font sizeSpacing that should scale with text size
remRelative to the root (page-wide) font sizeFont sizes โ€” the most predictable choice for text

If you're not sure which to use: rem for font sizes, px for borders and small fixed details, and % for widths that need to adapt to different screen sizes.

Styling text: alignment, line height, and decoration

p { text-align: center; /* left, right, center, justify */ line-height: 1.6; /* space between lines of text */ text-decoration: none; /* removes the underline from links */ letter-spacing: 0.5px; /* space between individual letters */ }

line-height is the one beginners skip most often โ€” and it's usually the single biggest reason a paragraph of text feels cramped or hard to read. A value between 1.4 and 1.6 is a safe default for body text.

Backgrounds: color vs. image

.hero { background-color: #151b23; background-image: url('photo.jpg'); background-size: cover; /* fills the whole element, cropping if needed */ background-position: center; }

You can combine both โ€” the color shows immediately while the image loads, and stays visible as a fallback if the image fails to load at all.

Putting it all together

Here's a small, complete example combining everything from this lesson into one realistic component โ€” a simple card:

.card { background-color: #ffffff; border-radius: 8px; padding: 20px; margin: 16px; font-family: 'Georgia', serif; } .card h3 { color: #151b23; font-size: 1.2rem; line-height: 1.3; } .card p { color: #6b6558; font-size: 0.95rem; line-height: 1.6; } .card:hover { background-color: #f4f2ec; }

Notice how every property here is something you already learned above โ€” a selector, a color, a font, some spacing, and a pseudo-class. Real CSS is mostly this: combining a handful of properties you already know, over and over, on different elements.

AD SLOT โ€” 728ร—90 or responsive (mid-lesson)

Level up: style a full card

exercise 2
Add a <div class="card"> to your page with a heading and a paragraph inside it. Style it using the card example above as a starting point, then make it your own โ€” try changing the background on hover, adjusting the padding, or using a Google Font instead of Georgia.
what's next
Lesson 3 covers the box model in depth โ€” exactly how padding, border, and margin interact to determine an element's real size on the page. It's the concept that trips up nearly every beginner at least once, and once it clicks, layout stops feeling like guesswork.
bottom line
CSS selectors point at elements โ€” including by state, with pseudo-classes โ€” and properties like color, font, units, text, and backgrounds describe how they should look. Next lesson, we go deep on the box model, the concept that explains exactly how spacing, borders, and sizing interact.
AD SLOT โ€” 300ร—250 or responsive (in-lesson)
next upLesson 3: The Box Model
Continue โ†’