- Home
- /
- Tutorials
- /
- HTML Tutorial
- /
- HTML <style> Tag
HTML Tags
HTML <style> Tag
The style tag holds CSS rules inside the HTML page itself, usually in the head. It is handy for a single page or a quick test. For a real website with many pages, a separate stylesheet linked with link is the better home for your CSS.
Syntax
html
<style>
selector {
property: value;
}
</style>Everything between <style> and </style> is CSS, not HTML. That is why HTML comments and tags do not work in there; use CSS comments, /* like this */.
Attributes
| Attribute | What it does |
|---|---|
| media | Applies the styles only when a media query matches, like media="print". |
| type | Was text/css. It is the default, so leave it out. |
| nonce | A one-time token that lets the styles run under a strict Content Security Policy. |
| scoped | Obsolete. Never worked in most browsers; use @scope in CSS instead. |
Example
html
<!DOCTYPE html>
<html>
<head>
<title>Style Tag Example</title>
<style>
h1 {
color: green;
text-align: center;
}
p {
font-size: 18px;
color: blue;
}
</style>
</head>
<body>
<h1>Welcome to My Webpage</h1>
<p>This paragraph is styled using the <style> tag.</p>
</body>
</html>The heading is green and centered, and the paragraph is blue at 18px. The rules in the <style> block apply to every matching element on the page.
style tag, style attribute or stylesheet?
<style>element: rules for this one page. Good for demos, emails and small pages.styleattribute: styles for one element, like<p style="color: red">. Fine for quick tests, hard to maintain.- External file with
<link rel="stylesheet" href="styles.css">: one file for the whole site, cached by the browser. What most sites use. See the link tag.
When the same element gets different values from these, the usual cascade rules decide. All three are compared in HTML Styles.
Browser Support
Chrome | Edge | Firefox | Safari | Opera | IE9+ |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
Good to know
- Put
<style>in the<head>, so the page is styled before it appears. A style block lower down can make the page flash unstyled for a moment. - You can have several
<style>blocks. They act like one long stylesheet, in order. - Large frameworks often add a small
<style>block with the most important CSS, called critical CSS, to make the first view appear faster.
HTML <style> Tag exercises
Add internal styles
Add a paragraph that says Styled text, and a style block that makes every paragraph teal.
Result
