How to Add CSS to an HTML Page with the style Element
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
<style>
<rules>
</style>
| Part | What 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:

<!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 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 assans-serif. - The CSS itself doesn't show on the page, because everything inside
styleis read as CSS, not as text to display.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing CSS in the page without a style element | It shows on the page as text: h1 { color: #5b5fc7; } appeared above the heading, and the heading stayed black | Put 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 all | Always close it with </style> |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| CSS rules show as text on the page | They aren't inside a style element | Wrap them in <style> and </style> |
| A rule has no effect | Its selector doesn't match, or the rule has a typo | Check 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
- style: The Style Information element, on MDN.
