Sub topic 4
Internal styles: the style element
This chapter covers the second way to attach CSS to a page: a style element inside the document. It assumes you know basic HTML structure (html, head, body), can edit and save text files and open them in a browser, and have seen the anatomy of a CSS rule (selector, property, value).
What an internal stylesheet is
An internal stylesheet is a style element that holds ordinary CSS rules. It normally sits in the head. Unlike the style attribute, its rules use selectors, so one rule can style many elements on the page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Internal styles</title>
<style>
p {
color: darkblue;
line-height: 1.5;
}
</style>
</head>
<body>
<p>Every paragraph on this page is styled by the style element.</p>
<p>So is this one.</p>
</body>
</html>The selector p matches both paragraphs. Change darkblue once and both change. Compare this with inline styles, where you would repeat the declaration on every paragraph.
How the browser reads it
The browser parses the HTML from top to bottom. When it reaches the style element, it parses the content as CSS, not as HTML. The rules then apply to every matching element in the document, including elements that appear later in the file.
flowchart TDThe diagram below shows this flow.
Where to place the style element
Put the style element in the head. This way the rules are known before the browser draws the body, so the page does not flash unstyled content.
A style element inside the body is common in older code and some generators, but it is not valid in current HTML for ordinary pages. Browsers usually still apply it, though it can cause a visible restyle after the content has been painted. Keep it in the head.
Scope: one page only
An internal stylesheet applies only to the page that contains it. If your site has ten pages, each page needs its own copy of the rules. When you edit one copy, the other nine do not change.
This is the main limit of internal styles. They suit a single page, such as a standalone demo, an email template, or a landing page. They do not suit a multi-page site, where an external stylesheet is the better tool.
Combining several rules
A style element can hold as many rules as you need. Rules are read in order, and when two rules of equal specificity set the same property on the same element, the later one wins.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Several rules</title>
<style>
body {
font-family: system-ui, sans-serif;
margin: 2rem;
}
h1 {
color: #333;
}
p {
color: darkblue;
}
p {
color: darkgreen;
}
</style>
</head>
<body>
<h1>Heading</h1>
<p>This paragraph is dark green, because the second p rule comes later.</p>
</body>
</html>The next chapters cover this in more detail. For now, note that source order inside a single style element matters.
Common errors
- Putting HTML inside the element. The content is CSS only. A tag such as
<b>insidestyleis read as invalid CSS, and the browser skips the broken rule. - Forgetting a semicolon or closing brace. A missing
;can make the browser drop the declaration after it. A missing}can swallow the next rule. - Writing CSS outside the element. CSS text placed directly in the
headorbodyis displayed as plain text or ignored. - Expecting changes on other pages. Each page needs its own
styleelement. - Using a
styleelement in thebodyfor site-wide rules. Move it to thehead.
When internal styles fit
| Situation | Internal styles a good fit? |
|---|---|
| One-page demo or prototype | Yes |
| Rules shared across many pages | No, use an external file |
| Styling a single element once | Possibly, but a class rule is cleaner |
| Single-file deliverable, such as an HTML email or saved page | Yes |
Exercise
A site has five pages. Each page has the same style element with a p rule. You change the paragraph color on one page. What happens on the other four, and what change would fix the underlying problem?
Show answer
The other four pages keep the old color, because each page holds its own copy of the rules. Move the shared rules into one external .css file and link it from every page with a link element, so one edit updates all five.
Exercise
In a single style element, you write p { color: red; } and then later p { color: blue; }. What color is a paragraph, and why?
Show answer
Blue. Both selectors have equal specificity, so the later rule in source order wins.