HTML <style> Tag

The HTML <style> tag is used for declaring style sheets within your HTML document.

The <style> element does not represent actual content for the user. Rather, it is used for styling the content.

Syntax

The <style> tag is written as <style></style> with the style sheet inserted between the start and end tags.

Like this:

You can use the type attribute to specify the style sheet language. In HTML 4 this is a required attribute. From HTML5 it is optional. Like this:

You can use the media attribute to specify which media the styles should apply to.

Like this:

Examples

Placement in the <head> Element

The <style> element is placed in the <head> element of the document, with the style declarations located between the opening and closing tags.

More specifically, each <style> tag must be located between the <head> tags (or a <noscript> element that is a child of a <head> element).

Here's an example:

Within the <body> Element

Despite only being valid in the document's head, the placement of the <style> element within the document's body has been a widespread practice, despite it's non-conformance to the HTML specification.

There is ongoing debate about whether the <style> element should be allowed in the document's body or not (for various reasons).

Despite this, most browsers support the <style> element being placed within the <body> element.

The scoped Attribute

Draft versions of the HTML 5.1 specification had included a scoped attribute that would allow the <style> element to appear within the document's body.

The attribute would allow authors to define styles for only a sub-section of the document (i.e. they wouldn't affect the rest of the document).

However, the scoped attribute was removed from the HTML 5.1 specification in early 2016.

Attributes

Attributes can be added to an HTML element to provide more information about how the element should appear or behave.

There are 3 kinds of attributes that you can add to your HTML tags: Element-specific, global, and event handler content attributes.

The <style> element accepts the following attributes.

Element-Specific Attributes

This table shows the attributes that are specific to the <style> tag/element.

AttributeDescription
typeSpecifies the style sheet language as a content-type (MIME type).
mediaSpecifies the device that the styles apply to. Must be a valid media query.

Possible values:

  • all
  • braille
  • print
  • projection
  • screen
  • speech
scopedSpecifies that the styles only apply to this element's parent element and that element's child elements. If this attribute is not used, styles will be applied to the whole document.

Note that if the <style> tag is being used outside of the document <head>, it must have the scoped attribute.

Draft versions of the HTML 5.1 specification had included a scoped attribute that would allow the style element to appear within the document's body.

The attribute would allow authors to define styles for only a sub-section of the document (i.e. they wouldn't affect the rest of the document).

However, the scoped attribute was removed from the HTML 5.1 specification in early 2016.

Global Attributes

The following attributes are standard across all HTML5 elements. Therefore, you can use these attributes with the <style> tag , as well as with all other HTML tags.

For a full explanation of these attributes, see HTML 5 global attributes.

Event Handler Content Attributes

Event handler content attributes enable you to invoke a script from within your HTML. The script is invoked when a certain "event" occurs. Each event handler content attribute deals with a different event.

Below are the standard HTML5 event handler content attributes.

Again, you can use any of these with the <style> element, as well as any other HTML5 element.

For a full explanation of these attributes, see HTML 5 event handler content attributes.

Differences Between HTML 4 & HTML 5

HTML 4 specifies that the type attribute is a required attribute, and that it has no default value.

HTML5 specifies that the type attribute is an optional attribute, and that the default value is text/css.

Also, HTML 5.1 introduced the scoped attribute.

To see more detail on the two versions see HTML5 <style> Tag and HTML4 <style> Tag. Also check out the links to the official specifications below.

Template

Here's a template for the <style> tag with all available attributes for the tag (based on HTML5). These are grouped into attribute types, each type separated by a space. In many cases, you will probably only need one or two (if any) attributes. Simply remove the attributes you don't need.

For more information on attributes for this tag, see HTML5 <style> Tag and HTML4 <style> Tag.

* The title attribute has special semantics on the <style> element. If used, it defines alternative style sheet sets.

Also, the title attribute on <style> elements differs from the global title attribute of most other elements in that a <style> element without a title does not inherit the title of the parent element: it simply has no title.

Tag Details

For more details about the <style> tag, see HTML5 <style> Tag and HTML4 <style> Tag.

Specifications

Here are the official specifications for the <style> element.

What's the Difference?

W3C creates "snapshot" specifications that don't change once defined. So the HTML5 specification won't change once it becomes an official recommendation. WHATWG on the other hand, develops a "living standard" that is updated on a regular basis. In general, you will probably find that the HTML living standard will be more closely aligned to the current W3C draft than to the HTML5 specification.