An HTML attribute is a named value on an element’s markup. Attributes give the browser information about an element and can also be read by CSS and JavaScript. They are not the same as JavaScript properties: attributes are strings on the DOM element, while properties live on its JavaScript object and may have other types. section 3.4.3 section 6.2.8
Choose attributes according to what the value controls:
- Use native semantic attributes when they fit. For example, `lang` identifies content language, `alt` supplies image text, and the presence of `disabled` makes an interactive element non-interactive. Prefer these to recreating the behavior with CSS or JavaScript. section 3.4.3
- Use direct attributes such as `variant` or `size` for a custom element’s core configuration. Reserve `data-*` for application-specific metadata; `dataset` exposes those values as strings in JavaScript. section 7.9.1 section 6.2.8
- Use properties for complex values such as objects or arrays, and CSS custom properties for styling choices. Attributes suit simple values that can be expressed as strings. section 7.9
const input = document.createElement("input");
input.setAttribute("value", "A");
document.body.append(input);
input.value = "B";
console.log(input.getAttribute("value")); // "A"
console.log(input.value); // "B"
The input’s `value` attribute remains `"A"` after its JavaScript `value` property becomes `"B"`. For form controls, an attribute may retain the initial value while the property tracks changing state. section 6.2.8
Read or write an attribute with `getAttribute()` and `setAttribute()`. Do not assume a same-named property exists or stays synchronized: some attributes are reflected, while others are not. Boolean attributes such as `disabled` express their state by presence or absence, rather than by a JavaScript boolean stored in markup. section 6.2.8
A small custom-element contract
`<star-rating value="3">` supplies a simple value in HTML. The element can read it with `getAttribute('value')`; a JavaScript property can offer a separate typed interface if needed. If consumers change a property, its setter must also trigger any component update that change requires. Document public attributes alongside properties and events. section 7.9 section 7.9.4 section 9.9.5































