HTML <style> Element

Publish in HTML Tutorial el 25/05/2025 16:40

The <style> HTML element contains style information for a document, or part of a document. It contains CSS rules that specify how elements should appear on the page. The <style> element is typically placed inside the <head> section of an HTML document, though it can appear anywhere in the document.

Key characteristics of the <style> element:

  • Contains CSS rules that apply to the current document
  • Can be used in the <head> or <body> (though <head> is recommended)
  • Supports media queries with the media attribute
  • Can be scoped to specific parts of the document (though scoped attribute is deprecated)
  • Alternative stylesheets can be defined using the title attribute


Basic Syntax

Here’s how a basic <style> element looks in HTML:


Examples of <style> Element Usage

Example 1: Basic Document Styling

This example shows how to use the <style> element to define basic styles for HTML elements.

Play Code


Example 2: Media Queries

The <style> element can contain media queries to apply different styles based on device characteristics.

Play Code


Example 3: Keyframe Animations

The <style> element can define CSS animations using @keyframes rules.

Play Code


Example 4: Dynamic Style Changes with JavaScript

This example shows how JavaScript can interact with the <style> element to modify styles dynamically.

Play Code


Example 5: Creating and Inserting Style Elements with JavaScript

This example demonstrates how to create and insert new <style> elements dynamically using JavaScript.

Play Code


Tips and Tricks

  • Performance: Place <style> elements in the <head> for better performance as the browser can start rendering styles sooner.
  • Specificity: Styles in <style> elements have higher specificity than external stylesheets but lower than inline styles.
  • Scoping: While the scoped attribute is deprecated, you can achieve scoping by using specific selectors or CSS methodologies like BEM.
  • Dynamic Styles: For complex dynamic styling, consider using CSS variables that can be modified via JavaScript instead of manipulating <style> elements directly.
  • Organization: For large projects, consider using multiple <style> elements to organize your CSS (e.g., one for layout, one for typography, etc.).


Browser Support

The <style> element is supported by all browsers. However, some attributes like scoped have limited or no support in modern browsers as they’ve been deprecated.

También te puede interesar

HTML <strong> Element
HTML <strong> Element

The <strong> element is used to indicate that its content has strong importance, seriousnes...

HTML <span> Element
HTML <span> Element

The <span> element is an inline container used to mark up a part of a text or document. Unl...

HTML <source> Element
HTML <source> Element

The <source> element is used to specify multiple media resources for media elements like &l...

HTML <small> Element
HTML <small> Element

The <small> element is used to represent side-comments and small print, typically for discl...

HTML <section> Element
HTML <section> Element

HTML <section> Element: The Complete Guide The <section> element is a semantic HTML ...

HTML <script> Element
HTML <script> Element

The <script> HTML element is used to embed or reference executable code, typically JavaScri...