Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

HTML term or value: style

The `style` attribute contains a CSS declaration list applied directly to one element and exposed to scripts through `CSSStyleDeclaration`.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<p style="color: red">…</p>

The value is parsed like a declaration block: valid properties remain, invalid ones are dropped independently, and a later declaration of the same property can replace an earlier one. Declarations participate in the author cascade; !important and other cascade levels can still change the winner, while CSP style-src-attr can disallow attribute styles.

Prefer a class and a stylesheet for states or presentation shared by multiple elements. Use style for genuinely element-specific runtime values; never concatenate untrusted text into CSS declarations, and remember that serialization can normalize spelling and whitespace.

WHATWG requires content to remain comprehensible and usable when styles are removed. Using style to hide or show content, or to convey meaning that is absent without styling, is nonconforming; presentation cannot replace semantics or native state.

The example parses color: red alongside the unknown property not-a-property; CSSStyleDeclaration retains only 1 valid declaration. Reading x.style.color returns the serialized red inline declaration, not computed style; the unknown name exposes no value.

Examples

<!doctype html><meta charset="utf-8"><p id="x" style="color: red; not-a-property: 1">Text</p><output id="result"></output><script>result.textContent=`declarations=${x.style.length}; color=${x.style.color}; invalid=${x.style.getPropertyValue("not-a-property")}`;</script>

Result

declarations=1; color=red; invalid=

The executed example verifies that one invalid declaration is discarded without invalidating the correct portion of the attribute.

Sources