Codectionary / Developer documentation / HTML

<style>

The <style> element contains embedded CSS rules that apply directly to the current document, as an alternative to linking an external stylesheet with <link>. It normally lives inside <head>, though the scoped nature of modern CSS means it can technically appear in body content too.

Syntax

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

Examples

Basic Embedded Styles

Adding page-specific CSS directly in the document.

<head>
  <style>
    body { margin: 0; font-family: system-ui; }
    h1 { color: #1f2937; }
  </style>
</head>

Media-Specific Styles

Using the media attribute to apply styles conditionally.

<style media="print">
  nav, footer { display: none; }
</style>

Component-Scoped Styles

A style block for a specific reusable component.

<style>
  .card { border-radius: 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); padding: 16px; }
</style>
<div class="card">Card content</div>

Attributes

  • media (string): A media query specifying when the styles apply
  • type (string): Defaults to text/css; rarely needs to be set explicitly

Best practices

  • Prefer external stylesheets via <link> for anything beyond a handful of page-specific rules, for better caching and maintainability
  • Use style blocks for critical above-the-fold CSS you want inlined for faster first paint
  • Avoid inline style attributes on individual elements for anything reusable — a style block or external file keeps things DRY
  • Keep embedded styles scoped and specific to avoid unexpectedly overriding global styles elsewhere on the site

At a glance

Purpose
Structure and meaning for web content
File extension
.html · .htm
Runs in
Web browsers
Usually used with
CSS and JavaScript

Specifications & further reading

Related HTML documentation