Start Consultation

(+62)81 471 5682


A tag on its own says what kind of content you have: a link, an image, an input. It does not say where the link goes, which image to load or what kind of input is expected. HTML attributes supply that missing information.

This guide covers how attributes are written, which ones work on every element, which ones belong to specific elements and the habits that keep them tidy.

What Are HTML Attributes?

An attribute is extra information written inside an opening tag. It is made of a name and a value, joined by an equals sign.

html

<a href="https://example.com/menu">View the menu</a>

Here href is the name and the web address is the value. An element can have several attributes separated by spaces, and their order does not matter. Attributes never go in the closing tag.

Quoting rules for attribute values

HTML is forgiving about quotes, which is exactly why a firm habit helps.

Global Attributes

Global attributes can be placed on any element.

AttributeWhat it does
idGives one element a unique name
classGives an element one or more reusable labels
titleAdds advisory text, usually shown as a tooltip
langStates the language of the content, such as lang="en"
hiddenHides the element from the page
styleApplies inline CSS to that one element
data-*Stores custom data on the element

Element-Specific Attributes

Other attributes only make sense on certain elements.

Links

href sets the destination of a link. target="_blank" opens that destination in a new tab.

Images

html

<img src="loaf.jpg" alt="Sliced sourdough loaf" width="600" height="400">

Inputs

html

<input type="email" name="email" placeholder="you@example.com" required>

Checking input in more depth is covered in the article on form validation.

Boolean Attributes

A boolean attribute needs no value. If it is present, it is on. If it is absent, it is off.

html

<button disabled>Send</button>
<input type="checkbox" checked>

Common examples are required, disabled, checked and hidden. A frequent mistake is writing disabled="false". The attribute is still present, so the button stays disabled. To switch a boolean attribute off, remove it completely.

id vs class

Beginners often mix these two up.

html

<section id="contact" class="panel panel-dark">
  <h2>Contact us</h2>
</section>

A simple test: if there could ever be two of something on a page, use a class. How CSS targets each one is explained in the article on CSS selectors.

Custom Data Attributes

Sometimes you need to attach your own information to an element. Any attribute name that starts with data- is reserved for that purpose.

html

<button data-product-id="42" data-color="red">Add to cart</button>

Data attributes do not change how the element looks or behaves. They hold values that JavaScript can read later through the element’s dataset property. Keep the names lowercase, and never store private information in them, because anyone can view the page source.

A Short Note on ARIA Attributes

Attributes that begin with aria- give extra information to assistive technology such as screen readers. For example, aria-label="Close menu" gives a name to a button that shows only an icon.

ARIA attributes describe an element but add no behavior. The right native element is usually the better choice, and the accessibility article in this series goes into detail.

Best Practices for HTML Attributes

Frequently Asked Questions

Are HTML attributes case sensitive?

Attribute names are not, so HREF and href work the same. Many values are case sensitive, though, including id and class values.

Can an element have more than one attribute?

Yes. Add as many as you need, separated by spaces, but use each name only once per element.

What is the difference between alt and title on an image?

alt replaces the image for anyone who cannot see it, so it is essential. title is optional extra text that usually appears as a tooltip on hover, which touch screen and keyboard users may never see.

Final Thoughts

Attributes turn general tags into specific, useful elements: a link with a destination, an image with a description, a field with rules. Keep ids unique, reuse classes and quote every value, and your markup will stay clean.

Related reading

Leave a Reply

Your email address will not be published. Required fields are marked *