Skip to content
Tutorials Center
My learning

Topics

HTML Reference ยท Document and Metadata

The <style> element

Reference for the HTML <style> element: embedding CSS in a document, the media and blocking attributes, when to prefer an external file, examples and details.

The <style> element contains CSS that applies to the current document. It is the middle ground between the style attribute (styles for one element) and an external style sheet loaded with <link> (styles shared by many pages).

HTML
<style>
  body { font-family: system-ui, sans-serif; line-height: 1.5; }
  .price { color: #15803d; font-weight: 600; }
</style>

Try it

Embedded styles with a media query

Attributes

Attribute Values Description
media A media query The styles apply only while the query matches. Defaults to all.
blocking render Explicitly marks the style sheet as render-blocking.
title Text Names an alternative style sheet set. Most pages should leave it out, because a titled sheet may be treated as one of several alternatives.

It also accepts the global attributes; nonce is commonly used to allow the block under a Content Security Policy. The type attribute is obsolete: CSS is the only style language, so leave it out.

Usage notes

  • Put it in the head. <style> is conforming only where metadata content is allowed, which in practice means the <head>. Browsers will still apply a <style> found in the body, but it is invalid and can cause a flash of restyled content.
  • External files for shared styles. An external .css file is cached once and reused on every page. Use <style> for page-specific rules or a small block of critical CSS. Adding CSS to HTML compares the three ways to add CSS.
  • Only CSS goes inside. The content is raw text: HTML tags inside are not parsed as elements. The only thing to avoid is the text </style, which ends the element.
  • Order matters. Later rules win over earlier ones of equal specificity, so a <style> placed after a <link rel="stylesheet"> can override it.
  • Scripts can read it. Each <style> element exposes its style sheet as element.sheet, and the whole list is in document.styleSheets.
  • The style attribute is a different feature with the same name. See The style Attribute.

Technical summary

Content categories Metadata content
Permitted content Text that is valid CSS
Tag omission None; both the start and end tags are required
Permitted parents Any element that accepts metadata content; a <noscript> inside the head
Implicit ARIA role No corresponding role
DOM interface HTMLStyleElement
Default display none

Browser support

Supported in every browser. The blocking attribute is newer and not yet supported everywhere.