<style> Tag in HTML

Quick facts

CategoryMetadata content
Content modelCSS text
Tag omissionNeither the start nor the end tag can be omitted
Permitted parentsA <head> element (browsers also accept it in the <body>, but the HTML standard expects it in the head)
DOM interfaceHTMLStyleElement

Syntax

<style> body { font-family: sans-serif; } h1 { color: purple; } </style>

Live example

Edit the code below and watch the preview update instantly.

index.html
Preview

Attributes

AttributeDescription
mediaApplies the CSS only for a media query, e.g. print or (max-width: 600px)
titleNames an alternative stylesheet set
blockingrender keeps the page hidden until this CSS is ready
typeObsolete: text/css is the default, so leave it out

Common examples

Print styles

A <style media="print"> block applies only when the page is printed, so you can hide menus and buttons on paper.

<style media="print"> nav, footer, .no-print { display: none; } body { color: #000; background: #fff; } </style>

Dark mode

Use the prefers-color-scheme media query to match the visitor’s light or dark setting.

<style> body { background: #fff; color: #222; } @media (prefers-color-scheme: dark) { body { background: #111; color: #eee; } } </style>

Reusable colors with CSS variables

Define colors once in :root and reuse them everywhere — change one line to restyle the page.

<style> :root { --brand: #7b61ff; } a { color: var(--brand); } .button { background: var(--brand); color: #fff; } </style>

Critical CSS in <style> vs a full stylesheet

A common speed technique uses both. The small amount of CSS needed for what appears first on screen — the header, the main heading, the layout — is placed in a <style> tag so the page can show immediately, with no extra download. The rest of the CSS loads from an external file via <link>, which the browser caches for every other page.

For small sites, a single external stylesheet is simpler and perfectly fast. Inline “critical CSS” is worth it on large, busy pages where every fraction of a second of loading time counts.

Critical CSS in <style>Full stylesheet
DownloadsNone (in the page)One file, then cached
SizeSmall, above-the-fold onlyEverything
BenefitFaster first paintCaching across pages

Common mistakes to avoid

  • Copying the same big <style> block into every page. It can’t be cached and is hard to update; move shared CSS into one external file.
  • Fighting with !important. Using it to win every conflict makes styles impossible to override later; fix the selector order instead.
  • Writing // comments. CSS only understands /* … */ comments; // breaks the rule that follows.
  • Forgetting the closing </style>. Everything after it is treated as CSS and disappears from the page.

Accessibility & SEO tips

  • Check text color contrast — a ratio of at least 4.5:1 for normal text keeps your page readable for everyone.
  • Respect visitors who prefer less motion with @media (prefers-reduced-motion: reduce) and tone down animations there.
  • Never remove focus outlines without replacing them; keyboard users rely on them to see where they are.

Browser support

Supported in every browser, including Chrome, Edge, Firefox, Safari and their mobile versions — it has been part of HTML since the very first versions. Modern CSS features used above, such as variables and prefers-color-scheme, work in all current browsers.

Related tags

Frequently asked questions

What is the difference between <style>, a stylesheet and the style attribute?
A <style> tag holds CSS for one page. An external stylesheet, loaded with <link rel="stylesheet">, can be shared by many pages and cached by the browser. The style attribute styles one single element. Most sites use external stylesheets and keep <style> for small, page-specific rules.
Can I put a <style> tag in the body?
Browsers will apply it, but the HTML standard expects <style> in the <head>, and CSS that arrives late can make the page flash or shift as it loads. Keep it in the head when you can.
Do I need type="text/css" on <style>?
No. CSS is the default and only type, so the type attribute is unnecessary in modern HTML.
What are the three ways to add CSS to HTML?
An external stylesheet loaded with <link rel="stylesheet">, a <style> tag inside the page, and the style attribute on a single element. External stylesheets are best for most sites.
Can I use media queries inside a <style> tag?
Yes. Any CSS works inside a <style> tag, including @media queries, @font-face and CSS variables.
How many <style> tags can a page have?
As many as you like. They all apply, and when two set the same property, the one that comes later in the page wins.