Definition and Usage
The <style> tag represents an area for writing stylesheets within an HTML document.
This area can contain only style rules and comments written in CSS.
The <style> tag is one of several ways to define styles in HTML.
It does not represent content for the user.
Basic Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>The style Tag</title>
<style> /* A tag that represents an area for writing CSS styles */
h1 {
color: white;
background-color: black;
}
p {
color: gray;
}
</style>
</head>
<body>
<h1>The style Tag</h1>
<p>
The style tag represents an area for writing CSS styles
within an HTML document.
</p>
</body>
</html>
To apply styles to HTML by linking a CSS stylesheet, use the <link> tag.
Placement of the <style> Tag
The <style> tag is used to create a CSS area for styling desired elements within an HTML document.
Except for a few cases, there are no particular restrictions on the placement of the <style> tag. However, because HTML documents are rendered from top to bottom, it must be placed before the elements to be styled. Otherwise, those elements will be rendered before the CSS is applied, causing users to see their styles change suddenly—almost as if the screen is flashing...
However, structuring HTML documents is very important.
Elements displayed on the screen are written within the <body> tag. This is the basic structure of HTML.
When the <style> tag is placed within the <head> tag, the structure of the HTML document does not become complex, making it easier to grasp the structure. Furthermore, since the location of the <style> tag can be easily recognized, it is much more convenient in many ways when styling elements and working with HTML documents.
However, despite these recommended practices, the <style> tag is often placed within the <body> tag instead of the <head> tag. In most cases, this occurs when the HTML document is dynamically implemented, particularly when prioritizing development convenience while building with a programming language that handles HTML dynamically, such as PHP.
Multiple <style> Tags
Multiple <style> tags can be written within an HTML document.
When styling, you can also divide the styles across multiple <style> tags, taking into account convenience, readability, and the targets or characteristics of the styling.
<style> tags to improve readability
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Multiple Style Tags</title>
<style>
/* Desktop */
h1 {
color: white;
background-color: black;
}
p {
color: gray;
}
</style>
<style>
/* Mobile */
@media (max-width: 500px) {
h1 {
font-size: 1.8em;
}
p {
font-size: 0.9em;
}
}
</style>
</head>
<body>
<h1>Multiple Style Tags</h1>
<p>
Multiple style tags can be written within an HTML document.
</p>
</body>
</html>
Attributes Used
The <style> tag supports global attributes, and its related attributes include the type and media attributes.
The type Attribute
This attribute defines the content type (MIME type) of the style. In CSS, the content type is text/css.
For the <style> tag, the default value of the type attribute is text/css. This means that even if the type attribute is not specified, the text/css value is applied. In addition, only the text/css value must be specified for the type attribute within the <style> tag. If any value other than text/css is specified, the styles will not be applied.
For this reason, there is no need to explicitly specify the type attribute in the <style> tag, since the default value of text/css is applied even if it is not explicitly specified.
<!-- There is no need to explicitly specify the type attribute. -->
<style type="text/css">...</style>
type attribute is omitted.
<!-- The type attribute has been omitted. -->
<style>...</style>
The media Attribute
This attribute specifies the media type to which the stylesheet is applied. For example, media="screen" specifies screens, and media="print" specifies printing.
The default value of the media attribute is all.
If the media attribute is omitted, the default value of all is applied, meaning that it applies to all media types.
media attribute is omitted,
the styles are applied to all media types.
<style>...</style>
<style media="print">...</style>
Technical Summary
| Permitted parents |
|
|---|---|
| Permitted content | Text that gives a conformant style sheet. |
Implicit ARIA role |
No corresponding role |
Permitted ARIA roles |
No role permitted |
Specifications
| Specification | |
|---|---|
<style>
|
HTML Standard #the-style-element |
Browser Compatibility
| Tag |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<style>
|
1 | 12 | 1 | 1 |