HTML <style> Tag

Beginner
5 min read
Updated: Nov 2026
4 examples
Live preview

Definition and Usage

The <style> element holds CSS for the document (or part of it). Place it in <head> for page-wide rules.

For shared or large stylesheets prefer <link rel="stylesheet"> so the file can be cached. For one-off styles on a single element, the style attribute works — but keep most CSS in stylesheets.

Baseline · Widely available.

Core embedding of CSS is stable since about July 2015. Some features (such as blocking) have varying support.

Deprecated: non-text/css type.

Omit type for normal CSS. If you set it, only the empty string or text/css is allowed.

HTML Syntax

syntax.html
<style [media|nonce|title|blocking]>CSS rules</style>

Both start and end tags are required. Content is CSS text (not HTML markup).

Attributes

Besides globals, these attributes matter most on <style>:

AttributeDescriptionTry it
mediaMedia query — apply only when it matches (default all)Try it
nonceCSP nonce so this inline stylesheet is allowed by style-srcAdvanced
titleNames an alternative stylesheet setOptional
blockingMay use render so applying the sheet can block rendering (often with dynamic styles)Advanced
typeDeprecated for other values. Omit, or use empty / text/css onlyAvoid extras

Global Attributes

The <style> tag also supports the Global Attributes in HTML.

Live Preview

Change the embedded rule and watch the sample paragraph update.

Simulates a <style> block applying to the sample below.

Result
<style>
  p { color: #26b72b; }
</style>

This text is styled by an embedded rule.

Green text from an embedded color rule.

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Basic Stylesheet in <head>

Embed a short CSS rule that styles paragraphs on this page.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Basic style</title>
  <style>
    p {
      color: #26b72b;
    }
  </style>
</head>
<body>
  <p>This text will be green.</p>
</body>
</html>
Try It Yourself

How It Works

  1. Open <style> in <head> so rules apply to the page.
  2. Write a CSS rule (here, color for p).
  3. Close </style> — both tags are required.
  4. Omit type for normal CSS.

Example 2 — media Query

Limit when the embedded sheet applies with a media query.

HTML
<head>
  <style>
    p { color: navy; }
  </style>
  <style media="(max-width: 500px)">
    p { color: teal; }
  </style>
</head>
<body>
  <p>Resize the window to see the media rule.</p>
</body>
Try It Yourself

How It Works

  1. Keep a base rule in the first <style> (applies always).
  2. Add media on a second <style> with a query.
  3. When the query matches, that sheet’s rules apply (and can override).
  4. Default is all if you omit media.

Example 3 — Multiple <style> Blocks

Later sheets with equal specificity override earlier ones.

HTML
<head>
  <style>
    p {
      color: white;
      background-color: blue;
      padding: 0.5rem;
    }
  </style>
  <style>
    p {
      color: blue;
      background-color: yellow;
    }
  </style>
</head>
<body>
  <p>Later style wins on conflicting properties.</p>
</body>
Try It Yourself

How It Works

  1. First sheet sets color, background, and padding.
  2. Second sheet overrides color and background (same specificity, later source).
  3. Padding remains from the first sheet — it was not redeclared.
  4. Order with <link> matters too — include sheets in the order you intend.

Edge Cases & Pitfalls

Most style mistakes come from treating it as a dumping ground instead of a clear stylesheet.

order

Cascade order matters

<style> and <link> apply in document order. A later sheet can override an earlier one at the same specificity.

type

Skip needless type

Omit type for CSS. Other MIME types on <style> are obsolete.

body

Body styles are restricted

A <style> in the body must be the first child of its parent and should only style that parent (via @scope). Prefer <head> for normal pages.

csp

CSP may block inline CSS

Strict Content-Security-Policy setups need a matching nonce (or hash) for embedded stylesheets.

vs attr

Not the style attribute

The element embeds a sheet. The attribute styles one element. Prefer stylesheets for most work.

Browser Support

The <style> element is core HTML in every browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

<style>

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera all support embedded CSS. Some attributes (blocking) vary.

98% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer All
Supported
<style> Widely supported

Bottom line: Safe to use everywhere for embedded CSS. Prefer link for shared stylesheets; use media when a sheet should apply only sometimes.

Key Takeaways

  • Role: <style> embeds CSS for the document.
  • Placement: put it in <head> for normal page styles.
  • Compare: link for shared files; style attribute for one element.
  • media: limit when rules apply; omit for all media.

One line: use <style> for small page CSS; use <link> when styles are shared.

Default CSS Settings

Typical user-agent styling for the element itself (the CSS inside is what styles the page):

default.css
style {
  display: none;
}

The element is hidden; its rules still apply to matching content in the document.

Frequently Asked Questions

The style element holds CSS for the current document (or part of it). Put it in the head for page-wide rules.
Use style for small, page-only CSS. Prefer link with rel="stylesheet" for shared or larger stylesheets so browsers can cache the file.
No. Omit type for normal CSS. If present, it must be empty or text/css — other values are obsolete.
media limits when the stylesheet applies (for example a media query). If omitted, the default is all.
Yes, but only as the first child of its parent, with rules scoped to that parent (typically via @scope). Prefer head for normal page styles.
The style element embeds a stylesheet. The style attribute sets CSS on one element. Prefer stylesheets for maintainability.

Next: sub

Learn the subscript element for chemical formulas and footnotes.

sub tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful