HTML <style> Tag
<style> tag is used for declaring style sheets within your HTML document.
<style> element does not represent actual content for the user. Rather, it is used for styling the content.
<style> tag is written as
</style> with the style sheet inserted between the start and end tags.
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.
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:
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
headof the document. The use of style in the
bodyof the document may cause restyling, trigger layout and/or cause repainting, and hence, should be used with care.
<style> element has quite a bit of history with regards to its placement within the
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
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).
scoped attribute was removed from the HTML 5.1 specification in early 2016.
Attributes can be added to an HTML element to provide more information about how the element should appear or behave.
<style> element accepts the following attributes.
|Specifies the device that the styles apply to. Must be a valid media query.
|Represents 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.|
|Specifies the style sheet language as a content-type (MIME type).|
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 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.