Skip to main content

HTML Tags

HTML <style> Tag

Written by Updated HTML <style> Tag exercises

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

AttributeWhat it does
mediaApplies the styles only when a media query matches, like media="print".
typeWas text/css. It is the default, so leave it out.
nonceA one-time token that lets the styles run under a strict Content Security Policy.
scopedObsolete. 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 &lt;style&gt; 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?

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+
YesYesYesYesYesYes

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

Check your answer to see it here.