Skip to the page
css reference · attaching css

CSS in a <style> tag

type
HTML tag, lives in <head>
applies to
the whole document
inherits
n/a
common values
any CSS rules between the opening and closing tag

Where it goes

Inside <head>, so the browser knows the styles before it draws the page. For a real site with several pages, move the rules to a .css file and attach it with <link rel="stylesheet">. Then every page shares them.

Example

<head>
  <style>
    body { font-family: system-ui, sans-serif; }
    h1 { color: teal; }
  </style>
</head>

Open this example in the editor →

Common mistakes

  • Writing HTML inside it. <style> switches the browser into CSS mode, so <h1> { } is an error. Write the bare selector: h1 { }.
  • Leaving off </style>. The browser reads the rest of the page as CSS, and the page goes blank from that point.
  • Using the style="" attribute instead. A style on one tag beats every rule in the <style> block. A stray style="" is the classic reason a rule "does not work."

Related

Updated 24 October 2026. Practice it in the css route, or read more on MDN.