Skip to content

<style> - Embedded Style Information

Metadata HTML 3.2

The <style> element contains CSS style information for a document or document fragment. It allows you to embed CSS directly into HTML, defining how elements should be displayed in the browser. While typically placed in the <head> section, <style> elements can appear anywhere in your document.

Result
<style>
/* CSS rules go here */
selector {
property: value;
}
</style>

The content inside <style> tags should be valid CSS. The styles apply to the document or shadow DOM tree containing the element.

AttributeDescriptionValues
mediaSpecifies which media/device the styles are optimized forMedia query (e.g., screen, print, (max-width: 600px))
typeMIME type of the style sheet (deprecated)text/css (default, can be omitted)
nonceCryptographic nonce for Content Security PolicyBase64-encoded string
blockingIndicates rendering should block on fetching critical subresourcesrender

This element supports all global attributes, including id, class, and title.

Embed CSS directly in your HTML for small projects or single-page applications:

Result

Use the media attribute to apply styles only for specific devices or conditions:

Result

Inline critical above-the-fold styles to improve initial render performance:

Result

Use <style> within document fragments or web components for scoped styling:

Result

Implement dark mode using the prefers-color-scheme media query:

Result

Responsive Styles with Multiple Media Queries

Section titled “Responsive Styles with Multiple Media Queries”

Create responsive layouts with media query-based styles:

Result

Understanding when to use <style> versus <link> is crucial for performance and maintainability:

Use inline <style> when: Critical above-the-fold CSS (improves First Contentful Paint), small amounts of CSS (under 10KB), page-specific styles not reused elsewhere, implementing critical rendering path optimization, working with component-based architectures where scoping is important, or creating single-page applications or standalone HTML files.

Result

Styles in <style> tags are render-blocking, meaning the browser must parse and apply them before rendering content. This is both a strength and a consideration:

Advantages of inline styles: No network request required (faster than external files for small CSS), guaranteed to be available immediately, perfect for critical rendering path optimization, and eliminates FOUC (Flash of Unstyled Content) for initial content. Inline styles reduce Total Blocking Time and improve First Contentful Paint metrics.

The optimal approach for most production websites:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Optimized Page</title>
<!-- Step 1: Inline critical CSS (above-the-fold) -->
<style>
/* Critical styles for immediate render */
body { margin: 0; font-family: system-ui; }
.header { background: #333; color: white; padding: 1rem; }
.hero { min-height: 500px; background: #f0f0f0; }
</style>
<!-- Step 2: Preload non-critical CSS -->
<link rel="preload" href="/styles/main.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="/styles/main.css"></noscript>
<!-- Step 3: Preconnect to external resources -->
<link rel="preconnect" href="https://fonts.googleapis.com">
</head>
<body>
<!-- Content renders immediately with critical styles -->
</body>
</html>

Keep inline <style> blocks appropriately sized:

  • Under 5KB: Ideal for critical CSS
  • 5-10KB: Acceptable for component-specific styles
  • 10-14KB: Maximum recommended (fits in one TCP packet)
  • Over 14KB: Consider splitting or using external stylesheet

Modern web applications use Content Security Policy to prevent XSS attacks. The nonce attribute allows inline styles while maintaining security:

Result

When implementing Content Security Policy with inline styles:

  1. Generate unique nonces: Use a cryptographically secure random generator on the server for each request
  2. Match header and attribute: Ensure the nonce in your CSP header matches the nonce attribute exactly
  3. Avoid ‘unsafe-inline’: Never use 'unsafe-inline' in production; use nonces or hashes instead
  4. Use hashes for static styles: For truly static inline styles, you can use hash-based CSP instead of nonces
<!-- Example with hash-based CSP -->
<!-- CSP header: style-src 'sha256-xyz123abc456...' -->
<style>
body { margin: 0; }
</style>

Inline styles can be vectors for CSS injection attacks if user content is unsafely inserted:

<!-- DANGEROUS: Never do this -->
<style>
.user-color {
/* User input: <%= user_input %> */
color: <%= user_input %>;
}
</style>

Risk: An attacker could inject malicious CSS to steal data or manipulate the page appearance for phishing attacks.

Respect user preferences for high contrast mode:

Result

Respect user preferences for reduced motion to avoid triggering vestibular disorders:

Result

Ensure your styles support browser zoom and user font size preferences:

/* Good: Use relative units */
body {
font-size: 1rem; /* Respects user preferences */
line-height: 1.5;
}
h1 {
font-size: 2em; /* Scales with body font */
}
/* Avoid: Fixed sizes that don't scale */
body {
font-size: 16px; /* Ignores user preferences */
}

Ensure sufficient color contrast for readability:

/* Good: WCAG AA compliant (4.5:1 contrast ratio) */
.text {
color: #333333;
background-color: #ffffff;
}
/* Poor: Insufficient contrast */
.text {
color: #cccccc;
background-color: #ffffff;
}
BrowserVersionNotes
Chrome1+Full support including media queries
Firefox1+Full support including media queries
Safari1+Full support including media queries
Edge12+Full support including media queries
IE3+Basic support; IE9+ for media queries
FeatureChromeFirefoxSafariEdge
Basic <style>1+1+1+12+
media attribute1+1+3+12+
Media queries4+3.5+4+12+
nonce attribute45+31+10+79+
blocking attribute105+105+17+105+

Container for document metadata where style elements typically reside. Learn more →

Links to external stylesheets, an alternative to inline styles. Learn more →