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

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

Here's an example:

It can also be placed inside a <noscript> element that is a child of a <head> element.

Within the <body> Element

Starting with HTML 5.2, the <style> element is now allowed in the document's body. However, the HTML specification advises the following:

A style element should preferably be used in the head of the document. The use of style in the body of the document may cause restyling, trigger layout and/or cause repainting, and hence, should be used with care.

History

The <style> element has quite a bit of history with regards to its placement within the body element.

Prior to HTML 5.2, the <style> element was only allowed to be placed in the document's head (although placing it in the body had become widespread practice among CSS developers).

There had been a lot of debate about whether the <style> element should be allowed in the document's body or not (for various reasons).

Despite this, most browsers supported 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.

The <style> element accepts the following attributes.

AttributeDescription
mediaSpecifies the device that the styles apply to. Must be a valid media query.

Possible values:

  • all
  • braille
  • print
  • projection
  • screen
  • speech
nonceRepresents a cryptographic nonce ("number used once") which can be used by Content Security Policy to determine whether or not the style specified by an element will be applied to the document. The value is text.
typeSpecifies the style sheet language as a content-type (MIME type).

Global Attributes

The following attributes are standard across all HTML 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 Handlers

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.

Most event handler content attributes can be used on all HTML elements, but some event handlers have specific rules around when they can be used and which elements they are applicable to.

For more detail, see HTML event handler content attributes.