<style>

A CSS stylesheet written directly in the HTML document.

Display
none
Closing tag
Required
Permitted content
CSS code
On this page

Example

#
HTML
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Course card</title>
    <style>
      .course {
        max-width: 280px;
        padding: 16px;
        border: 2px solid #a78bfa;
        border-radius: 12px;
      }
      .course h2 {
        margin: 0 0 8px;
        color: #a78bfa;
      }
      .course p {
        margin: 0;
      }
    </style>
  </head>
  <body>
    <article class="course">
      <h2>CSS Course</h2>
      <p>Colors, spacing, and page layouts in 12 lessons.</p>
    </article>
  </body>
</html>
Preview

Definition and usage

#

The <style> element contains CSS rules that the browser applies to the whole page. It belongs in <head>. Browsers will also apply styles written in <body>, but according to the HTML standard that is an error.

Internal styles work well in small projects, prototypes, and single pages, and also when you want to put into the HTML only the most urgent styles needed to show the first screen. For a larger site, a separate file attached with <link rel="stylesheet"> is better: the browser downloads it once and uses it on all subpages, while the contents of <style> arrive anew with every document.

The cascade decides which rule wins. With the same specificity, the rule written later wins, so a <style> placed after <link> can override styles from the file. In turn, the style attribute on a specific element is stronger than the rules from <style>.

Syntax

#
Syntax
<style>
  selector {
    property: value;
  }
</style>

Attributes

#
  • media

    media query

    Applies the styles only when the condition is met, e.g. when printing (print) or on narrow screens. Without this attribute the styles always apply.
  • type

    text/css (unnecessary)

    In older code you’ll come across type="text/css". It’s the only allowed value and also the default, so you can simply leave the attribute out.

The element also supports all global attributes, such as class, id, style and hidden.

Attributes in practice

#

media

media query

Applies the styles only when the condition is met, e.g. when printing (print) or on narrow screens. Without this attribute the styles always apply.

HTML
<head>
  <style>
    body { font-family: system-ui, sans-serif; }
  </style>
  <style media="print">
    nav, .ad { display: none; }
    body { color: black; background: white; }
  </style>
</head>

Default CSS

#

Most browsers display this element with these default styles:

CSS
style {
  display: none;
}

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • When styles grow and apply to many subpages, move them to a separate .css file.
  • Don’t scatter styles across three places at once (a file, <style>, and the style attribute). Finding where a button’s color comes from will then take longer than changing it.
  • To see which rules apply to an element, right-click it and choose “Inspect”. The developer tools will show all matching rules.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.