Skip to main content

The <style> Element

As defined in the HTML Living Standard, the <style> element allows authors to embed style information (CSS) in their documents. The <style> element is one of the two primary ways to document CSS rules for a document, the other being the <link> element.

<style>
  body {
    color: var(--color-text);
  }
</style>

The <style> tag is typically placed in the <head> of the document, but it can also be used in the <body> under specific conditions in modern HTML5.

View HTML Living Standard: The style element

Separation of Concerns

The primary role of the <style> element is to separate Presentation from Structure.

WCAG Requirement: Info and Relationships

Success Criterion 1.3.1 (Level A): Information, structure, and relationships conveyed through presentation can be programmatically determined.

By moving visual choices (like font-size, color, and positioning) into a <style> block instead of using inline style="" attributes or presentational tags (like <font>), you ensure that the HTML structure remains clean. This allows assistive technology to focus on the semantic meaning of the content without being cluttered by presentational noise.

Understand SC 1.3.1: Info and Relationships

Accessibility via Media Queries

The <style> element is the container for powerful media queries that allow you to adapt your site to specific user needs.

@media (prefers-reduced-motion: reduce) { * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; } }

Best Practices

A11y Tip: User Overrides

Users with low vision often use "User Stylesheets" to increase font sizes or change background colors. Keeping your styles centralized in a <style> block makes it easier for these specialized browsers to calculate specificity and apply the user's preferred settings.