Skip to main content

How to Add CSS to an HTML Page with the style Element

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

The <style> element holds CSS inside the HTML page itself. Put it in head, write your CSS rules between its tags, and they apply to that page.

Syntax​

Syntax
<style>
<rules>
</style>
PartWhat it does
<style>Starts the CSS for this page.
<rules>CSS rules, such as h1 { color: #5b5fc7; }.
</style>Ends the CSS.

MDN's style page says the element is typically placed in the document's head.

The Example​

The example is a short notice about a team lunch. Its style element gives the page a sans-serif font and the heading a purple color.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page whose head holds a style element with two rules: body gets the font family sans-serif, and h1 gets the color #5b5fc7. The body has the heading Team lunch and the paragraph Friday at 12:30, in the kitchen.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Team lunch</title>
<style>
body {
font-family: sans-serif;
}

h1 {
color: #5b5fc7;
}
</style>
</head>
<body>
<h1>Team lunch</h1>
<p>Friday at 12:30, in the kitchen.</p>
</body>
</html>

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: the heading Team lunch in purple, and the line Friday at 12:30, in the kitchen., both in a sans-serif font.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The browser applied both rules. The heading's color came out as rgb(91, 95, 199), which is #5b5fc7, and the page's font as sans-serif.
  • The CSS itself doesn't show on the page, because everything inside style is read as CSS, not as text to display.

Common Mistakes​

MistakeWhat happensDo this instead
Writing CSS in the page without a style elementIt shows on the page as text: h1 { color: #5b5fc7; } appeared above the heading, and the heading stayed blackPut the rules between <style> and </style>
Forgetting </style>The rest of the page is read as CSS: with the closing tag missing, the page showed nothing at allAlways close it with </style>

Troubleshooting​

SymptomLikely causeFix
CSS rules show as text on the pageThey aren't inside a style elementWrap them in <style> and </style>
A rule has no effectIts selector doesn't match, or the rule has a typoCheck the selector and the spelling of each property

FAQ​

Can the style element go in body?​

Chromium applied a style element placed in body too, and colored the heading. MDN's style page says the element is typically placed in head, which keeps all the page's CSS in one place, before the content.

When should I use a separate CSS file instead?​

When several pages share the same look. A CSS file linked from each page styles them all, while a style element styles only its own page.

Can one page have several style elements?​

Yes. The browser applies the rules of each one: with one style element coloring the heading purple and another coloring the paragraph teal, both colors showed.

Conclusion​

A style element in head is the quickest way to give one page its own CSS. Keep the CSS between its tags, and move it to a separate file once several pages need the same rules.

Additional Resources​