Skip to content

HTML <style> Tag

The <style> tag is used to style an HTML document with CSS. It defines how elements should be displayed in browsers.

TIP

To link to an external style sheet, use the <link> tag.

The content of the <style> tag is meant for browsers, which is why it is placed inside the <head> element. Scripts on the page are typically placed after CSS code.

It is possible to use more than one <style> element on one page.

DANGER

In earlier versions of HTML and in XHTML, the type="text/css" attribute was required with the <style> tag.

Syntax

The <style> tag comes in pairs. The content is written between the opening (<style>) and closing (</style>) tags.

Example of the HTML <style> tag:

HTML <style> tag

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      h1 {
        color: #1c87c9;
      }
      p {
        color: #8ebf42;
      }
    </style>
  </head>
  <body>
    <h1>Text heading.</h1>
    <p>First paragraph.</p>
  </body>
</html>

Result

style example

Attributes

AttributeValueDescription
mediamedia_querySpecifies the media type for which the styles apply. By default, styles apply to all devices.
scopedscopedIndicates that the style applies only to the element containing the <style> tag and its descendants. When used, the <style> tag must be placed inside the element it styles, rather than in the <head>. Note: This attribute is deprecated in HTML5.2 and should be avoided in modern development.
typetext/cssSpecifies the MIME type of the style sheet. It is optional in HTML5, as text/css is the default.

Practice

What are the characteristics of HTML style tag as shown on the w3docs site?

Dual-run preview — compare with live Symfony routes.