The <style>
contains style information for a document, or part of a document. It contains CSS, which is applied to the contents of the document containing the <style>
element.
The <style>
element can be included inside the <head>
or <body>
of the document, and the styles will still be applied, however it is recommended that you include your styles in the <head>
for organizational purposes — it is a lot better to separate your content from your presentation as much as possible. Even better, put your styles in external stylesheets and apply them using <link>
elements.
If you include multiple <style>
and <link>
elements in your document, they will be applied to the DOM in the order they are included in the document — make sure you include them in the correct order, to avoid unexpected cascade issues.
In the same manner as <link>
elements, <style>
elements can include media
attributes that contain media queries, allowing you to selectively apply internal stylesheets to your document depending on media features such as viewport width.
This element includes the global attributes.
type
text/css
if it is not specified — there is very little reason to include this in modern web documents.media
all
if the attribute is missing.nonce
title
scoped
The <style>
element itself has no visual representation, therefore it has no styling considerations.
In the following example, we apply a very simple stylesheet to a document:
<!doctype html> <html> <head> <style> p { color: red; } </style> </head> <body> <p>This is my paragraph.</p> </body> </html>
In this example we've included two <style>
elements — notice how the conflicting declarations in the later <style>
element override those in the earlier one, if they have equal specificity.
<!doctype html> <html> <head> <style> p { color: white; background-color: blue; padding: 5px; border: 1px solid black; } </style> <style> p { color: blue; background-color: yellow; } </style> </head> <body> <p>This is my paragraph.</p> </body> </html>
In this example we build on the previous one, including a media
attribute on the second <style>
element so it is only applied when the viewport is less than 500px in width.
<!doctype html> <html> <head> <style> p { color: white; background-color: blue; padding: 5px; border: 1px solid black; } </style> <style media="all and (max-width: 500px)"> p { color: blue; background-color: yellow; } </style> </head> <body> <p>This is my paragraph.</p> </body> </html>
Content categories |
Metadata content, and if the scoped attribute is present: flow content. |
---|---|
Permitted content | Text content matching the type attribute, that is text/css . |
Tag omission | Neither tag is omissible. |
Permitted parents | Any element that accepts metadata content. |
Permitted ARIA roles | None |
DOM interface | HTMLStyleElement |
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'style' in that specification. | Living Standard | |
HTML5 The definition of 'style' in that specification. | Recommendation | No change from HTML 4.01 Specification. |
HTML 4.01 Specification The definition of 'style' in that specification. | Recommendation |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 1 | Yes | 1 | 3 | 3.5 | 1 |
type |
1 | Yes | 1 | 3 | 3.5 | 1 |
media |
1 | Yes | 1 | 3 | 3.5 | 1 |
title |
1 | Yes | 1 | 3 | 3.5 | 1 |
scoped
|
19 — 35
|
No | 55 — 61
|
No | No | No |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 1 | 18 | Yes | 4 | 6 | 1 | Yes |
type |
1 | 18 | Yes | 4 | 6 | 1 | Yes |
media |
1 | 18 | Yes | 4 | 6 | 1 | Yes |
title |
1 | 18 | Yes | 4 | 6 | 1 | Yes |
scoped
|
No | No | No | 55 — 61
|
No | No | No |
<link>
element, which allows us to apply external stylesheets to a document.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/style