Skip to main content

attr-order

info

๐Ÿ”ง This rule supports auto-fix with the --fix option.

Enforces a consistent order of attributes on elements. By default, attributes are sorted alphabetically. You can configure priority lists, predefined groups (global, event, aria, data, spread), and custom patterns to define the desired order.

โŒ Examples of incorrect code for this rule

<div style="color: red" class="foo" id="bar"></div>

โœ… Examples of correct code for this rule

<div class="foo" id="bar" style="color: red"></div>

Configurationโ€‹

Priority listโ€‹

{
"attr-order": ["id", "class", "style"]
}

Attributes matching the list are placed first in the specified order. Unmatched attributes follow alphabetically.

Predefined groupsโ€‹

{
"attr-order": [{ "group": "global" }, { "group": "aria" }, { "group": "event" }, { "group": "data" }]
}
GroupMatches
globalHTML global attributes (id, class, style, etc.)
eventEvent handler attributes (onclick, onchange, etc.)
ariaARIA attributes (aria-label, aria-hidden, etc.)
dataCustom data attributes (data-*)
spreadSpread attributes (JSX {...props})

Pattern matchingโ€‹

{
"attr-order": [{ "pattern": "^data-" }]
}

Group-internal sort orderโ€‹

{
"attr-order": [
{ "group": "global", "order": "alphabetical" },
{ "group": "aria", "order": ["aria-label", "aria-describedby", "aria-hidden"] },
{ "group": "event", "order": "source-order" }
]
}
  • "alphabetical" (default) โ€” Sort alphabetically within the group.
  • "source-order" โ€” Preserve the original order within the group.
  • string[] โ€” Fixed order. Unlisted attributes are appended alphabetically.

Optionsโ€‹

alphabeticalโ€‹

Type: boolean Default: true

Whether to sort unmatched attributes alphabetically. When false, unmatched attributes preserve their source order.

{
"attr-order": {
"value": ["id", "class"],
"options": { "alphabetical": false }
}
}

Interfaceโ€‹

{
"attr-order": undefined[]
}

An ordered list of attribute order entries. Each entry can be a string (attribute name), or an object specifying a group or pattern.

Optionsโ€‹

{
"attr-order": {
"options": {
"alphabetical"?: boolean
}
}
}
PropertyTypeDefault ValueDescription
alphabeticalbooleantrueWhether to sort unmatched attributes alphabetically.

Default Severityโ€‹

warning