S/Shortutorial
Home / Programming / CSS basics
Programming · Web

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.

Beginner~15 min read6 lessonsLive CSS playgroundBox model visualizer

New to HTML? CSS styles HTML, so it helps to know a few tags first. HTML in 10 Minutes covers everything you need.

01

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;
}

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:

  1. The more specific selector wins (an ID beats a class, a class beats a tag name).
  2. If they're equally specific, the rule that comes later wins.
  3. Some properties, like color and font-family, are inherited: set them on body and every element inside gets them too.

Comments. Write notes to yourself between /* and */. The browser ignores them.

02

Selectors: picking what to style

Selectors are how you point at things. These cover almost everything a beginner needs:

SelectorPicksExample
pEvery element of that typeAll paragraphs
.tipElements with class="tip"Any tagged element, reusable
#logoThe one element with id="logo"IDs must be unique on a page
nav aLinks anywhere inside a navDescendant (space)
h1, h2Both headingsGroup with a comma
a:hoverA link while the mouse is over itPseudo-class
li:first-childThe first item in each listPseudo-class
Example · classes do most of the work
/* 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.

03

Colors and units

Four ways to write a color

FormatExampleNotes
NametomatoAbout 140 named colors. Easy to read.
Hex#ff6347Red, green, blue as 00 to ff. The most common format.
RGBrgb(255 99 71)Same channels as 0 to 255. Add / 50% for see-through.
HSLhsl(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?

UnitMeansGood for
pxCSS pixels, a fixed sizeBorders, small details
remTimes the root font size (16px by default)Font sizes and spacing. Respects the reader's text-size setting.
emTimes this element's font sizePadding that grows with the text
%Percent of the parentWidths
vw / vh1% of the window width / heightFull-screen sections
Example · rem math
  1. The browser's default root font size is 16px.
  2. font-size: 1.5rem → 1.5 × 16
  3. 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.

04

The box model

Every element is a rectangle made of four layers, from the inside out:

  1. Content: the text or image. Its size is set by width and height.
  2. Padding: space inside the border. It shows the element's background color.
  3. Border: a line around the padding, like border: 2px solid black.
  4. 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;
}
Try it · box model visualizer
marginborderpaddingcontent

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.

05

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-directionrow (default), column
justify-content
main axis
flex-start, center, flex-end, space-between, space-around
align-items
cross axis
stretch (default), center, flex-start, flex-end
flex-wrapnowrap (default), wrap to let items flow onto new lines
gapSpace 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.

Example · center anything
.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.

06

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; }
}

Habits that make most pages responsive for free

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.

GO

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.

Try it · live CSS playground
Show the HTML for this challenge
Preview

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.

MORESources

LINKSKeep learning elsewhere

NEXTKeep learning