Sub topic 3
Inline Styles: The style Attribute
This chapter assumes you can write basic HTML (html, head, body, common elements), edit and save text files, and open a page in a browser. You also need to know what a CSS rule looks like: a selector, then properties and values in braces.
What an inline style is
An inline style is a set of CSS declarations written in the style attribute of a single HTML element. A declaration is a property and a value joined by a colon, such as color: crimson. The browser applies the declarations to that element only.
<p style="color: crimson; font-weight: bold;">This paragraph is styled inline.</p>Compare this with a normal rule. A rule has a selector that picks elements, plus braces around the declarations. In a style attribute the element is already chosen, because the attribute sits on it. So you write no selector and no braces.
| Part | In a CSS rule | In a style attribute |
|---|---|---|
| Selector | `p` | Not written. The host element is the target. |
| Braces | `{ ... }` | Not written. |
| Declarations | Separated by semicolons | Separated by semicolons |
Syntax rules
Follow these rules when you write the attribute value.
- Write each declaration as
property: value. - Separate declarations with a semicolon. The last semicolon is optional, but keeping it makes later edits safer.
- Wrap the whole value in quotes. Double quotes are the usual choice.
- Use single quotes for anything inside the value, such as a font name or a URL.
<h1 style="font-family: 'Helvetica Neue', sans-serif; letter-spacing: 0.05em;">
Welcome
</h1>CSS comments (/* ... */) are allowed inside the value. HTML comments are not.
Where it fits in the cascade
You can use style on almost any element in body. A declaration there usually beats rules from a style element or a linked stylesheet, because it has higher priority than any selector. A later chapter covers the reasons and the exception, !important. For now, remember this: an inline declaration is hard to override from a stylesheet.
<head>
<style>
p { color: blue; }
</style>
</head>
<body>
<p>Blue, from the style element.</p>
<p style="color: red;">Red, because the inline declaration wins.</p>
</body>What inline styles cannot do
The style attribute holds declarations only. This limits what you can express.
- No selectors. You cannot style a child, a class, or a state from the parent's attribute.
- No pseudo-classes or pseudo-elements.
:hover,:focus, and::beforecannot go in an inline style. - No media queries. You cannot change a style at a given screen width.
- No reuse. Ten paragraphs that share a look need ten copies of the same text.
<!-- This does nothing: selectors and braces are not valid here. -->
<a href="/home" style="a:hover { color: red; }">Home</a>Common errors
- Missing semicolon between declarations. In
style="color: red font-size: 2rem"the browser cannot read the declaration, so it ignores it. - Using
=instead of:. Writecolor: red, notcolor=red. - Mismatched quotes. An opening double quote closed by a single quote breaks the attribute, and the rest of the tag may be read as text.
- Wrong property names. HTML attributes such as
bgcolorare not CSS. Usebackground-color. - Trying to fix a stylesheet conflict with inline styles. This works at first, then makes the page harder to maintain, because every override is now buried in the markup.
When inline styles are reasonable
Use them when the value is unique to one element or is computed at runtime. Typical cases are a quick test while you experiment, a progress bar width set by a server, or an HTML email, where many mail clients ignore style elements. For anything that repeats, prefer a rule in a stylesheet. Later chapters compare the methods in detail, including how a Content Security Policy can block inline styles.
Exercise
A page has <style> p { color: blue; } </style> in the head. Write one paragraph that shows bold green text despite that rule, using only an inline style. Then name one thing you cannot do in that attribute that a stylesheet rule can.
Show answer
<p style="color: green; font-weight: bold;">Text</p>. The inline declaration for color overrides the blue from the style element. You cannot use a selector, a pseudo-class such as :hover, or a media query in the attribute. A stylesheet rule can use all three.
Exercise
This tag has a bug: <div style="background-color=yellow; padding: 1rem">. What is wrong, and what is the corrected tag?
Show answer
= is used where CSS needs :. The corrected tag is <div style="background-color: yellow; padding: 1rem;">. The trailing semicolon is optional but recommended.