The HTML <style> tag
The HTML <style> element embeds CSS directly in the document, normally inside the <head>. Use the media attribute to scope styles to a media query. For anything beyond critical or page-specific CSS, prefer an external <link> stylesheet.
Overview
The <style> element embeds CSS directly in the document. It usually lives in the <head>, and its media attribute can limit the styles to matching conditions, for example media="print" for print-only rules.
Inlining CSS this way is best reserved for a small amount of critical or page-specific CSS — the styles needed to render the first screen quickly. For shared styles used across the site, an external <link rel="stylesheet"> is better, because the browser can cache one file across every page.
One security note: a strict Content-Security-Policy may block inline <style> blocks unless they carry a matching nonce. The old type="text/css" attribute is no longer needed in HTML.
Syntax
<style>
body { font-family: system-ui; }
.card { padding: 1rem; }
</style>
Attributes
The <style> element supports the following attributes, in addition to the global attributes available to every HTML element.
Example
<style>
p { color: #1c7ce9; }
</style>
<p>Styled by an embedded stylesheet.</p>
Best practices
- Reserve inline
<style>for small amounts of critical or page-specific CSS. - Use an external <link rel="stylesheet"> for shared, cacheable styles.
- Scope a block with the
mediaattribute (e.g.media="print") when needed. - Add a
nonceif a strict Content-Security-Policy blocks inline styles.
Frequently asked questions
What is the style element for?
Should I use a style element or an external stylesheet?
<style> only for small, critical CSS; use an external <link rel="stylesheet"> for shared styles so they can be cached.How do I apply a style block only when printing?
media="print" to the <style> element so its rules apply only to printed output.Does the style element need a type attribute?
type="text/css" is the default and is no longer needed in HTML.