What Are Attributes?
Attributes provide extra information about an element, written inside the opening tag as name="value" pairs.
<a href="https://example.com" target="_blank">Visit Example</a>
Here, href and target are attributes of the <a> (anchor) element. href tells the browser where the link goes, and target="_blank" tells it to open in a new tab.
A few rules to remember:
- Attribute values are almost always wrapped in double quotes (single quotes also work, but be consistent).
- An element can have multiple attributes, separated by spaces:
<img src="cat.jpg" alt="Cat" width="300">. - Some attributes are boolean — their mere presence means "true," with no value needed:
<input type="checkbox" checked>
<button disabled>Can't click me</button>
Here, checked and disabled don't need ="true" — just writing the attribute name turns the behavior on.
Attributes are how the same tag can behave very differently. <input type="text">, <input type="checkbox">, and <input type="date"> all use the same tag but render completely different form controls, purely because of the type attribute's value.