Before you begin
Basic HTML helps, but each example is small enough to practise in a new HTML file.
Start with CSS selectors and spacing, then style readable pages and create responsive layouts with Flexbox, Grid, media queries and reusable colors. Every module explains the idea in plain language, walks through a code example and gives you a small task to try yourself.
Basic HTML helps, but each example is small enough to practise in a new HTML file.
Build responsive, accessible page layouts and debug style conflicts with a repeatable method.
Work through the modules in order if you’re new to CSS. If you already know the basics, use the contents list to jump to a topic. Try each example by changing one value, running it again and explaining the result in your own words.
A selector tells CSS which HTML elements to style. The declarations inside the rule set things such as color, size and spacing. Sometimes several rules match the same element; the browser follows the cascade to decide which one wins. Simple selectors and clear component names make that decision easier to predict. If a style does not apply, inspect the matching rules before adding more specific selectors.
/* style.css */
body { color: #182538; }
.lesson-card { padding: 1.25rem; }
.lesson-card h2 { margin-block: 0 0.5rem; }CSS lays each element out as a box. The content is in the middle, padding is the space inside the edge, border is the edge itself, and margin is space outside it. With box-sizing: border-box, a card set to 400 pixels wide stays 400 pixels wide even after padding and a border are added. Logical properties such as margin-inline add space at the start and end, which also works for languages written right to left.
*, *::before, *::after { box-sizing: border-box; }
.card { width: min(100%, 32rem); padding: 1.5rem; border: 1px solid #d5dce5; margin-inline: auto; }Use rem for text and spacing that should scale with the root font size, and percentages or flexible units for fluid layouts. Choose a readable line length and contrast. CSS custom properties let a design system share colors and spacing consistently.
:root { --ink: #182538; --accent: #c5f467; }
body { color: var(--ink); font: 1rem/1.65 system-ui, sans-serif; }
.article { max-width: 68ch; margin-inline: auto; }Normal flow lays out block and inline content in document order. display changes how an element participates in layout. Positioning removes or offsets boxes in specific ways; sticky and fixed elements must not hide focused controls or important content.
.toolbar { display: flex; align-items: center; gap: 1rem; }
.notice { position: sticky; inset-block-start: 0; z-index: 2; background: white; }Flexbox is made for arranging items in one direction: across a row or down a column. Imagine placing a heading and buttons on a shelf. justify-content controls their spacing along the shelf, align-items lines them up across it, and gap adds even space between items. When the screen gets too narrow, flex-wrap lets items move onto a new line instead of being squeezed or cut off.
.actions { display: flex; flex-wrap: wrap; align-items: center; gap: 0.75rem; }
.actions > a { padding: 0.65rem 1rem; }CSS Grid arranges items in rows and columns at the same time, so it works well for a gallery or a set of course cards. repeat() describes how many columns to make, while minmax() gives each column a safe minimum and a flexible maximum. auto-fit allows the browser to fit as many columns as the available space can hold. The HTML reading order stays important even if CSS moves items around visually.
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr)); gap: 1rem; }Start with a layout that works in a narrow viewport, then add breakpoints only when the content needs them. Media queries can react to viewport width, user preferences such as reduced motion, and other environmental features. Avoid fixed page widths that cause horizontal scrolling.
.page { padding-inline: 1rem; }
@media (min-width: 48rem) { .page { padding-inline: 2rem; } }
@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto; } }Pseudo-classes select an element’s state or relationship, such as :hover, :focus-visible and :checked. Pseudo-elements style a part of an element, such as ::before. Make focus indicators visible and ensure hover is never the only way to reveal key information.
.button:hover { filter: brightness(0.95); }
.button:focus-visible { outline: 3px solid #246b56; outline-offset: 3px; }
li + li { margin-block-start: 0.5rem; }Custom properties hold reusable values and can vary in the cascade. Cascade layers let you define a predictable order for broad styles, components and overrides. Organize rules around components and remove rules that no longer describe the interface.
@layer reset, base, components, utilities;
@layer base { :root { --space: 1rem; } }
@layer components { .panel { padding: var(--space); border-radius: 0.75rem; } }Transitions animate a property change and transforms move or scale an element without changing surrounding layout. Use animation to communicate state, not as decoration that blocks reading. Respect prefers-reduced-motion and avoid animating expensive properties when a transform or opacity will work.
.card { transition: transform 160ms ease, box-shadow 160ms ease; }
.card:hover { transform: translateY(-2px); }
@media (prefers-reduced-motion: reduce) { .card { transition: none; } }Design a responsive learning dashboard using Grid, Flexbox, design tokens and reduced-motion support. Write down what the project should do, build it in small steps, and include a README that explains how to run it.
Find a guided course →Use primary documentation for current language rules and version changes. The examples in this guide are learning starting points; verify details for the exact runtime and browser support your project needs.
No. CSS standards are developed module by module. The W3C CSS Snapshot collects the stable definition for a given year.
Use Flexbox when arranging items along one axis; use Grid when rows and columns work together. They can also be combined.