Learn • Create • Explore

How to Add CSS to HTML

Apply inline, internal and external CSS and diagnose which declaration the browser actually uses.

If you can open an HTML file but it looks like a plain document, you have reached a useful point. The page already has structure and can be read before its styling arrives. CSS adds the presentation: readable spacing, a colour palette, layouts that adapt to width, and states such as focus or hover. This guide shows three ways to deliver CSS, then walks through a reliable way to find why a rule does not appear to work. You will start with a notice page that reads well without its stylesheet and can be styled one rule at a time.

Inline, internal, and external CSS provide three ways to deliver styles to a document

Start with the HTML you want to style

Build the main content before adding decorations. The example has a heading, a short notice, and a link. The notice gets a class because another notice could share its appearance. The heading uses an actual heading element even though you could make ordinary text look similar with a large font. CSS controls how the content looks, while the markup continues to hold the reading order and structure. That separation lets you apply a different theme without rewriting the page’s meaning.

Paste the following example into the HTML editor. This first version uses a style element in the head because the example can run without a separate file. If you are using a local editor, save it as a plain text file with an HTML extension and open it directly in a browser. Make one change at a time so you can observe what each property does.

HTML example
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Community room notice</title>
  <style>
    * { box-sizing: border-box; }
    body { margin: 2rem auto; padding-inline: 1rem; max-width: 42rem; font: 1rem/1.6 system-ui; color: #10244a; }
    .notice { border: 1px solid #97c2df; border-inline-start: 6px solid #0053ad; padding: 1rem; background: #eaf6ff; }
    .notice h2 { margin-block-start: 0; }
    .notice a { color: #00488c; }
    a:focus-visible { outline: 3px solid #883f00; outline-offset: 3px; }
  </style>
</head>
<body>
  <main>
    <h1>Community room</h1>
    <section class="notice" aria-labelledby="notice-title">
      <h2 id="notice-title">Change of opening time</h2>
      <p>The room opens at noon on Saturday.</p>
      <a href="#hours">See this weekend’s hours</a>
    </section>
    <section id="hours">
      <h2>Weekend hours</h2>
      <p>Saturday noon–4 p.m. Sunday 10 a.m.–2 p.m.</p>
    </section>
  </main>
</body>
</html>
Try it in the HTML editor

The notice rule starts with a dot followed by the class name. The declarations inside braces set the border, padding, and background. A space between the notice class and the heading selector means the heading is a descendant of the notice. The weekend-hours heading lies outside that section, so it should keep its original margin. If both headings change when you edit a rule, check the selector before adjusting the value.

Choose a delivery method that fits the project

For a one-off test, a style attribute can apply a declaration to a single element. For example, putting an inline background colour on the notice can help you confirm that you are looking at the right box. That method becomes difficult to maintain when many elements share a design. It also does not provide a convenient place to define keyboard states or rules that respond to viewport width. Move a repeated treatment into a reusable rule.

An internal stylesheet keeps selectors and declarations together in the same document. It is convenient for learning examples, single-file demos, and cases where you deliberately maintain a self-contained page. The style element contains stylesheet text. Do not put style tags inside a separate stylesheet file, because that file is parsed directly as CSS.

For a multi-page project, an external stylesheet lets several pages load the same rules. Create a CSS file beside the HTML file, copy the declarations into it, and replace the style element with a link in the document head. Its relationship identifies the resource as a stylesheet and its destination identifies the file. The browser resolves that destination from the page URL. Save both files, refresh the page, and check that the original appearance remains.

A stylesheet link in the document head points to an external CSS file

Understand which declaration wins

A browser can find several declarations for the same property on one element. The cascade decides which declaration to apply. In this simple page, a class selector generally wins against an element selector at the same level when both match the same element. If two declarations have equal precedence and specificity, the later one usually wins. Inline styles can also override an ordinary rule from your stylesheet. Real projects can add layers, importance, and styles from other sources, so read the browser inspector’s explanation rather than relying on a single shortcut in every case.

Inheritance is another source of confusion. Text colour and many font properties can flow from an ancestor to a child, while borders and most spacing properties normally do not. If a section’s text changes when you style the body, inheritance may be working as expected. If you see a border around every card, inheritance is not the likely cause. Inspect the specific rule matching each card. Change one declaration at a time and see which rendered elements respond.

The browser can show both the winning declaration and the matching declarations it overrides

Diagnose the fastest visible signal first

If no styles appear at all, confirm you opened the intended page and saved the files. When you use a linked stylesheet, inspect the browser’s network panel and look for its request. If the request did not happen, check the link markup. If it failed, inspect the full address and the response. A stylesheet URL can return an HTML error page with a successful-looking address, so make sure the response is actually your CSS. Test from the deployed page, since relative paths can resolve differently after a move.

If only one rule fails, select the affected element in the browser’s inspector. A rule that is missing entirely may have a selector that does not match. A declaration marked as invalid may contain a misspelt property or an unsupported value. One that is crossed out can be overridden by another declaration. Correct the underlying issue instead of adding several stronger selectors. Use the CSS editor if you want a smaller place to isolate the problem with a single matching element.

To practise, add a second notice with a different heading. Change the shared class once and confirm both notices update, then add a separate class for one variation. When you are done, widen and narrow the page and try the link with a keyboard. The notice should remain understandable even if the styling is unavailable. For another walkthrough, see MDN’s getting started with CSS guide.

Move the rules and verify shared styling

After you have made the external version, create a second HTML document in the same folder. Give it its own title and one notice section, then link to the same stylesheet. Edit the notice’s padding in the shared file and reload both pages. The spacing should change in both places. This simple experiment demonstrates why a shared stylesheet matters: it gives you one place to correct a recurring visual problem. If you had copied a style element into each page instead, you would have to find and update every copy.

Now move the second document into a subfolder and reload it. Its existing address for the stylesheet is relative to the document, so it may look for a file inside the new subfolder. Update it to point one folder upwards and confirm the request again. If you later use a build system or a framework, it may generate asset addresses for you. Knowing the underlying browser rule still helps when the generated address points at the wrong deployment directory.

Add interaction states without hiding important information

Change the link’s focus outline and test the page with Tab. Then test it with a pointer. Keep both experiences usable. Hover is useful for some feedback but is not a reliable way to reveal essential instructions on a touchscreen. If the notice needs a warning or a status, put it in visible words so readers can understand it without interpreting a colour or passing a pointer over the page.

Try changing the background to a much darker colour. Check that the heading, body text, and link remain legible. Use a contrast checker on the actual foreground and background pair, and check the focus outline in context. A rule that looks readable on a demo screen can become difficult in a different lighting environment or at a smaller size.

Related tools

Continue learning