CSS chapters

Chapter 2 of 17

Connect a stylesheet to HTML

Try this in our CSS Compiler →

The idea

A CSS file needs a document to style. Locally, save styles.css next to index.html and reference it with a stylesheet link in the document head. The CSS preview here supplies a sample document; for a custom page use the HTML editor with your own markup and stylesheet.

Worked example

/* Save as styles.css and link from your HTML. */
body { margin: 0; padding: 24px; }
button { padding: 8px 16px; }

What to check

The browser parses declarations independently. A missing stylesheet path and a selector mismatch are different problems; inspect both before changing values.

Practice task

In a local two-file project, change the body's padding. Check that the correct stylesheet loaded rather than relying on a cached file.

Check your understanding

Explain the result in your own words before changing the example. Change one thing at a time, compare the result with your prediction, and keep the version that demonstrates the concept clearly.

Try it yourself

Output
Ctrl/Cmd+Enter runs · Ctrl/Cmd+S downloads the project · Local auto-save is always on