CSS Basics: Style Your First Web Page
HTML says what things are. CSS says how they look. In six short lessons you'll learn rules, selectors, colors, the box model, flexbox and responsive design, and practice each one in a live playground that checks your work.
New to HTML? CSS styles HTML, so it helps to know a few tags first. HTML in 10 Minutes covers everything you need.
What CSS is, and what a rule looks like
CSS stands for Cascading Style Sheets. It's a list of rules. Each rule picks some elements on the page and sets how they look: colors, sizes, spacing, layout.
h1 {
color: navy;
font-size: 32px;
}- Selector (
h1): which elements the rule applies to. - Declaration block (the curly braces): the settings.
- Property (
color) and value (navy), joined by a colon and ended with a semicolon.
Three ways to add CSS to a page
<!-- 1. A separate file (best for real sites) -->
<link rel="stylesheet" href="style.css">
<!-- 2. A <style> block in the <head> -->
<style> p { color: gray; } </style>
<!-- 3. Inline, on one element (use sparingly) -->
<p style="color: red">Warning!</p>A separate file is best because every page on your site can share it. Change one line and the whole site updates.
The "cascading" part
When two rules set the same property on the same element, the browser has to pick one. In short:
- The more specific selector wins (an ID beats a class, a class beats a tag name).
- If they're equally specific, the rule that comes later wins.
- Some properties, like
colorandfont-family, are inherited: set them onbodyand every element inside gets them too.
Comments. Write notes to yourself between /* and */. The browser ignores them.
Selectors: picking what to style
Selectors are how you point at things. These cover almost everything a beginner needs:
| Selector | Picks | Example |
|---|---|---|
p | Every element of that type | All paragraphs |
.tip | Elements with class="tip" | Any tagged element, reusable |
#logo | The one element with id="logo" | IDs must be unique on a page |
nav a | Links anywhere inside a nav | Descendant (space) |
h1, h2 | Both headings | Group with a comma |
a:hover | A link while the mouse is over it | Pseudo-class |
li:first-child | The first item in each list | Pseudo-class |
/* HTML: <p class="tip">Save often.</p> */
.tip {
background: lightyellow;
border-left: 4px solid orange;
}An element can have several classes, separated by spaces: class="tip big". Both rules apply.
Prefer classes over IDs for styling. IDs are so specific that they're hard to override later. Save them for links like #section-2 and for JavaScript.
Colors and units
Four ways to write a color
| Format | Example | Notes |
|---|---|---|
| Name | tomato | About 140 named colors. Easy to read. |
| Hex | #ff6347 | Red, green, blue as 00 to ff. The most common format. |
| RGB | rgb(255 99 71) | Same channels as 0 to 255. Add / 50% for see-through. |
| HSL | hsl(9 100% 64%) | Hue (0 to 360 on the color wheel), saturation, lightness. Easy to tweak by hand. |
Use color for text and background-color (or just background) for the fill behind it. Make sure there's strong contrast between the two so everyone can read it.
Units: how big is big?
| Unit | Means | Good for |
|---|---|---|
px | CSS pixels, a fixed size | Borders, small details |
rem | Times the root font size (16px by default) | Font sizes and spacing. Respects the reader's text-size setting. |
em | Times this element's font size | Padding that grows with the text |
% | Percent of the parent | Widths |
vw / vh | 1% of the window width / height | Full-screen sections |
- The browser's default root font size is 16px.
font-size: 1.5rem→ 1.5 × 16- 24px, and it grows if the reader sets bigger text
A value of zero needs no unit. margin: 0 is fine. Any other number needs one: margin: 10 is ignored.
The box model
Every element is a rectangle made of four layers, from the inside out:
- Content: the text or image. Its size is set by
widthandheight. - Padding: space inside the border. It shows the element's background color.
- Border: a line around the padding, like
border: 2px solid black. - Margin: space outside the border that pushes other elements away. Always transparent.
Shorthand lets you set all four sides at once, going clockwise from the top: padding: 10px 20px 10px 20px is top, right, bottom, left. Two values mean top-and-bottom, then left-and-right: padding: 10px 20px.
The surprise: width doesn't include padding
By default (box-sizing: content-box), width sets only the content. Padding and border get added on top, so a width: 200px box with 20px padding and a 2px border is really 244px wide. Most developers switch to border-box, where width includes padding and border:
*, *::before, *::after {
box-sizing: border-box;
}See it in your own browser. Right-click any element on any website and choose Inspect. The developer tools show its box model with the exact padding, border and margin.
Flexbox: lining things up
Normally, block elements like div and p stack top to bottom. Put display: flex on a parent, and its direct children (the flex items) line up in a row instead.
.toolbar {
display: flex;
justify-content: space-between; /* along the row */
align-items: center; /* across the row */
gap: 12px; /* space between items */
}| Property (on the parent) | Common values |
|---|---|
flex-direction | row (default), column |
justify-contentmain axis | flex-start, center, flex-end, space-between, space-around |
align-itemscross axis | stretch (default), center, flex-start, flex-end |
flex-wrap | nowrap (default), wrap to let items flow onto new lines |
gap | Space between items, like 12px or 1rem |
On a child, flex: 1 means "take a share of the leftover space." Give every item flex: 1 and they split the row evenly.
The two axes. justify-content works along the direction items flow. align-items works across it. Switch to flex-direction: column and they swap: justify becomes vertical, align becomes horizontal.
.stage {
display: flex;
justify-content: center;
align-items: center;
min-height: 200px;
}The classic "center a box both ways" problem, solved in three lines. You'll do it yourself in the playground.
Responsive design with media queries
Lots of people will read your page on a phone, so it needs to work on narrow screens too. Two pieces make that happen.
1. The viewport tag
Put this in every page's <head>. Without it, phones pretend to be a wide desktop screen and shrink your page to fit.
<meta name="viewport" content="width=device-width, initial-scale=1">2. Media queries
A media query wraps rules that apply only when a condition is true, usually the screen width.
/* Phones first: cards stack in a column */
.cards { display: flex; flex-direction: column; gap: 12px; }
/* 700px and wider: side by side */
@media (min-width: 700px) {
.cards { flex-direction: row; }
}min-widthmeans "this wide or wider." Writing phone styles first and addingmin-widthqueries is called mobile-first.max-widthmeans "this wide or narrower." Handy for fixing one thing on small screens.- Pick breakpoints where your layout starts to look cramped, not for specific phone models.
Habits that make most pages responsive for free
img { max-width: 100%; height: auto; }so images never overflow.- Use
max-widthinstead of a fixedwidthon containers. - Let flex rows wrap with
flex-wrap: wrap. - Use
remfor text so it respects the reader's settings.
Test it. In Chrome, Edge or Firefox, open developer tools and click the phone/tablet icon (device toolbar or responsive design mode) to preview any width.
Practice in the CSS playground
Pick a challenge. The HTML is fixed; write CSS in the box and the preview updates as you type. When your CSS does the job, you'll see a green check. Stuck? Press Show answer.
Show the HTML for this challenge
Tip: press Tab in the CSS box to indent. Press Esc first if you want Tab to move to the next button instead.
FAQCommon questions
Is CSS a programming language?
Not in the usual sense. CSS describes how things look rather than giving step-by-step instructions, so it's called a style sheet language. You still need to learn it to build websites, and it pairs with HTML (structure) and JavaScript (behavior).
Why isn't my CSS working?
Check, in order: a typo in the selector or property, a missing semicolon or curly brace, a missing unit (10 instead of 10px), the stylesheet link path, and whether a more specific rule is overriding yours. Right-click the element, choose Inspect, and the Styles panel shows which rules apply and which are crossed out.
Should I use flexbox or grid?
Flexbox is great for one direction at a time: a row of buttons, a nav bar, centering. CSS Grid is built for two-dimensional layouts with rows and columns, like a photo gallery or a full page layout. Many sites use both. Learn flexbox first.
What's the difference between margin and padding?
Padding is inside the border and takes the element's background color. Margin is outside the border, always transparent, and pushes neighbors away.
Do I need a framework like Tailwind or Bootstrap?
No. Frameworks are built on plain CSS, and they're much easier to use once you understand selectors, the box model and flexbox. Start with plain CSS.
px, rem or em for font sizes?
rem is the usual choice. It scales with the reader's browser text-size setting, which helps people who need bigger text. Use px for things like border widths.