Skip to main content

require-attr

Warns if specified attributes or required attribute on specs are not appeared on an element.

This rule refers to the HTML Living Standard. It has settings in @markuplint/html-spec.

Some attributes are required only under certain conditions (for example, <link rel="preload"> must carry an as attribute, but <link rel="modulepreload"> may omit it). Those conditional rules live in the per-element spec files under @markuplint/html-spec/src/spec.<element>.jsonc โ€” search for "required": to see which attributes carry a conditional selector and on which other attribute values they depend.

The <img> element's src attribute is required by the HTML Living Standard.

๐Ÿ‘Ž Example of incorrect code for this rule

<img />

<!-- "require-attr": "alt" -->
<img src="/path/to/image.png" />

๐Ÿ‘ Example of correct code for this rule

<img src="/path/to/image.png" />

<!-- "require-attr": "alt" -->
<img src="/path/to/image.png" alt="alternative text" />
note

This rule doesn't evaluate the element that has the spread attribute ({...props} of JSX, v-bind="obj" and :[key] of Vue, &attributes of Pug, a block of a template language in the place of an attribute such as <?= $attrs ?>). In the below code, it doesn't evaluate whether the img element includes the src attribute. Because Markuplint can't know whether the spread attribute includes the src property.

const Component = (props) => {
return <img {...props}>;
}

Interfaceโ€‹

{
"require-attr": string | (string | Attr)[]
}
type Attr = {
name: string;
value?: string | string[];
};

Optionsโ€‹

{
"require-attr": {
"options": {
"ignoreAttrs"?: string[]
}
}
}
PropertyTypeDefault ValueDescription
ignoreAttrsstring[]undefinedAttribute names to exclude from required-attribute checks. Use this when you want to ignore specific required attributes without disabling the entire rule.

Default Severityโ€‹

error

Configuration Exampleโ€‹

{
"rules": {
"require-attr": "alt"
}
}
{
"rules": {
"require-attr": ["alt", "src"]
}
}
{
"rules": {
"require-attr": [
"alt",
{
"name": "src",
"value": "/^\\/|^https:\\/\\//i"
}
]
}
}

Since we ordinary want to configure required attributes for each element type, require-attr rule should be configured in the nodeRules option.

Example configuration that alt attribute must be required on <img> element:

{
"rules": {
"require-attr": true
},
"nodeRules": [
{
"selector": "img",
"rules": {
"require-attr": "alt"
}
}
]
}

ignoreAttrsโ€‹

You can use the ignoreAttrs option to exclude specific attributes from require-attribute checks. This is useful when you want to keep the rule enabled but ignore certain attributes that are handled by a framework or are intentionally omitted.

Names are compared as the DOM compares them (see Attribute name case): ALT satisfies a required alt in the HTML syntax, and "ignoreAttrs": ["viewbox"] ignores a required viewBox of an <svg> there, but not in JSX, where an SVG element keeps the name as written. In JSX and Vue, a name in the option or in the rule value that has a capital letter is read as a prop of the framework and compared as written, so write the name of the specification in lower case (alt).

{
"nodeRules": [
{
"selector": "img",
"rules": {
"require-attr": {
"options": {
"ignoreAttrs": ["src", "srcset"]
}
}
}
}
]
}