Home HTML Tags <style> HTML <style> Tag Definition
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.
Syntax
HTML Syntax <style [media|nonce|title|blocking]>CSS rules </style>
Both start and end tags are required. Content is CSS text (not HTML markup).
Reference
Attributes Besides globals, these attributes matter most on <style>:
Attribute Description Try it mediaMedia query — apply only when it matches (default all) Try itnonceCSP nonce so this inline stylesheet is allowed by style-src Advanced titleNames an alternative stylesheet set Optional 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
Try it
Live Preview Change the embedded rule and watch the sample paragraph update.
Code
HTML Examples Short, copy-ready snippets. Use Try It Yourself for a live editor.
1. Basic style 2. media query 3. Multiple styles 4. style vs link Example 1 — Basic Stylesheet in <head> Embed a short CSS rule that styles paragraphs on this page.
<!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> How It Works Open <style> in <head> so rules apply to the page.Write a CSS rule (here, color for p).Close </style> — both tags are required.Omit type for normal CSS.Example 2 — media Query Limit when the embedded sheet applies with a media query.
<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> How It Works Keep a base rule in the first <style> (applies always).Add media on a second <style> with a query.When the query matches , that sheet’s rules apply (and can override).Default is all if you omit media.Example 3 — Multiple <style> Blocks Later sheets with equal specificity override earlier ones.
<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> Later style wins on conflicting properties.
How It Works First sheet sets color, background, and padding.Second sheet overrides color and background (same specificity, later source).Padding remains from the first sheet — it was not redeclared.Order with <link> matters too — include sheets in the order you intend.Example 4 — Prefer <link> for Shared CSS Embedded <style> is fine for tiny page-only CSS. Shared styles belong in a file.
<!-- Page-only (OK for small demos) -->
<style>
.note { font-weight: bold; }
</style>
<!-- Shared across pages (preferred) -->
<link rel="stylesheet" href="styles.css"> Embedded style can bold this note.
A real styles.css would load via <link> in production.
How It Works Use <style> when the CSS is small and only for this page.Use <link rel="stylesheet"> when many pages share the same file.Browsers can cache linked CSS — faster on later visits.Keep one clear place for shared rules so updates stay easy. 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.
Compatibility
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.
Remember
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.
UA style
Default CSS Settings Typical user-agent styling for the element itself (the CSS inside is what styles the page):
The element is hidden; its rules still apply to matching content in the document.
Frequently Asked Questions What is the HTML style tag used for? The style element holds CSS for the current document (or part of it). Put it in the head for page-wide rules.
Should I use style or link? Use style for small, page-only CSS. Prefer link with rel="stylesheet" for shared or larger stylesheets so browsers can cache the file.
Is the type attribute required? No. Omit type for normal CSS. If present, it must be empty or text/css — other values are obsolete.
What does the media attribute do? media limits when the stylesheet applies (for example a media query). If omitted, the default is all.
Can I put style in the body? 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.
How is style different from the style attribute? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion