OSHTML.003: Attributes — Names, Values, Global Attributes, Boolean Attributes, and Element-Specific Configuration

Wordless comic-style editorial artwork of two developers studying a webpage interface with visual markers representing HTML attributes.

Key Takeaways

  • Attributes add configuration or metadata to an HTML element.
  • Some attributes are global and can appear on nearly any HTML element; others only make sense on specific elements.
  • Boolean attributes are controlled by presence or absence, not by writing true or false.

In OSHTML.002: Elements, we learned that HTML pages are trees made from elements. Most elements have a start tag, content, and an end tag, while void elements such as img and input have no end tag. This lesson adds the next layer: the information written inside a start tag that changes an element’s identity, destination, behavior, language, accessibility, data, or configuration.

The Common Coder introduces HTML elements, attributes, global attributes, and how to use the MDN reference.

Learning Objectives

  • Identify an HTML attribute name and value.
  • Place attributes correctly inside a start tag.
  • Use quoted attribute values consistently.
  • Distinguish global attributes from element-specific attributes.
  • Explain how boolean attributes work.
  • Use id, class, lang, title, hidden, and data-* appropriately.
  • Use common element-specific attributes such as href, src, alt, type, name, value, required, and disabled.
  • Recognize common attribute mistakes that lead to invalid, inaccessible, or confusing markup.

What Is an HTML Attribute?

An attribute is additional information attached to an HTML element. Attributes normally appear inside the element’s start tag after the element name. The MDN HTML attribute reference describes attributes as values that configure elements or adjust their behavior.

<a href="https://example.com">Visit Example</a>

In this example, a is the element name, href is the attribute name, and https://example.com is the attribute value. The attribute does not replace the element. It configures the element by telling the browser where the link should go.

Attribute Names and Values

Most attributes follow a name-and-value pattern. The name identifies what is being configured. The value supplies the configuration. Although HTML permits some unquoted values under limited syntax rules, beginners should normally quote attribute values because quoted values are easier to read and less likely to break when spaces or special characters appear.

<img src="rack.jpg" alt="Server rack with fiber cabling" width="800" height="533">

This single img element contains four attributes. src identifies the image resource, alt provides a text alternative, and width and height provide dimensions. The element remains one node even though several attributes configure it.

STP Computer Education demonstrates how HTML attributes provide additional information and behavior to elements.

Global Attributes

Global attributes are attributes defined for use across HTML elements. The MDN global-attributes reference and the WHATWG HTML Standard list attributes that can be specified broadly across HTML elements, although a particular attribute may have little or no useful effect on some elements.

id

The id attribute gives an element a document-wide identifier. An ID should be unique within the document. IDs are useful for fragment links, CSS selectors, JavaScript, accessibility relationships, and other references.

<section id="networking-lab">
  <h2>Networking Lab</h2>
</section>

class

The class attribute provides one or more class names separated by spaces. Unlike an ID, the same class can be shared by many elements. Classes are commonly used by CSS and JavaScript to select groups of elements.

<p class="warning high-priority">Check the PDU before energizing the rack.</p>

lang and dir

The lang attribute identifies the language of an element’s content, while dir can define text direction. The root html element commonly receives the document’s primary language, as introduced in OSHTML.001: Document Structure.

<html lang="en">
...
<p lang="es">Hola, mundo.</p>

title, hidden, and tabindex

The title attribute can provide advisory information, but important instructions should not depend on a tooltip that some users may never receive. The hidden attribute marks content that is not currently relevant for presentation. The tabindex attribute affects keyboard focus order and should be used carefully; ordinary semantic controls already participate in keyboard navigation without needing large positive tabindex values.

Boolean Attributes

A boolean attribute represents an on/off state by whether the attribute is present. According to MDN’s boolean-attribute reference, presence means true and absence means false. Writing a value such as disabled="false" does not switch the attribute off; because the attribute is present, the state is still enabled.

<input type="text" disabled>
<input type="checkbox" checked>
<input type="email" required>

To make a boolean attribute false, remove it. For example, an enabled text field normally omits disabled entirely.

<input type="text">

Element-Specific Attributes

Many attributes belong only to certain elements or have meaning only in certain contexts. An anchor uses href; an image uses src and alt; form controls use attributes such as type, name, value, required, and disabled. Always check the reference for the specific element instead of assuming an attribute works everywhere.

Links: href, target, and rel

<a href="https://developer.mozilla.org/" target="_blank" rel="noopener">
  MDN Web Docs
</a>

The href attribute supplies the destination. target="_blank" requests a new browsing context. When opening an external page in a new tab or window, the rel attribute can describe the relationship and add security-related behavior such as noopener.

Images: src and alt

For images, src identifies the image resource and alt provides a text alternative. The alternative text should communicate the image’s purpose in context rather than mechanically repeat a filename. Decorative images can use an empty alt value when appropriate.

Forms: type, name, value, required, disabled, and checked

<label for="email">Email</label>
<input id="email" name="email" type="email" required>

<label>
  <input type="checkbox" name="alerts" checked>
  Enable alerts
</label>

Form attributes can change validation, submission data, default state, and user interaction. name is especially important because successful form controls are generally submitted using name-value pairs.

Custom data-* Attributes

Custom data-* attributes let authors attach application-specific data to an element when no standard attribute already expresses the information. In JavaScript, these values can be accessed through the element’s dataset interface.

<article data-rack="A17" data-zone="west">
  Rack status
</article>
Adam Khoury demonstrates custom data attributes and how HTML data can be exposed to JavaScript and CSS.

Custom data attributes are useful, but they should not replace semantic HTML. If a standard attribute or element already communicates the meaning, use the standard mechanism first.

Attribute Order and Duplicate Attributes

The order of ordinary attributes inside a start tag generally does not change the element’s meaning, so teams often choose a consistent formatting convention for readability. An element should not contain two attributes with the same name. Duplicate attributes produce invalid markup and can lead to confusing parser behavior.

<!-- Clear and valid -->
<input id="asset-id" class="field" name="asset" type="text" required>

<!-- Do not duplicate the same attribute -->
<input id="first" id="second" type="text">

Accessibility Is Part of Attribute Design

Attributes are often essential to accessibility, but attributes cannot rescue fundamentally poor structure. Use native semantic elements first. Then add attributes such as alt, lang, form-label relationships, and ARIA states or properties when they are actually needed. Avoid replacing a real button or link with a generic element plus a pile of scripting and accessibility attributes.

A Small Attribute Lab

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Attribute Lab</title>
</head>
<body>
  <main id="lab" class="training-page" data-level="3">
    <h1>Attribute Lab</h1>

    <a href="https://developer.mozilla.org/">Open MDN</a>

    <img src="rack.jpg" alt="Data-center rack with patch panels" width="640" height="426">

    <label for="asset">Asset tag</label>
    <input id="asset" name="asset" type="text" required>

    <button type="button" disabled>Run Test</button>
  </main>
</body>
</html>

This file combines global attributes, element-specific attributes, boolean attributes, accessibility-related relationships, and custom data. Open it in a browser, inspect the DOM with developer tools, then add and remove attributes to observe what changes.

Common Beginner Mistakes

  • Writing boolean attributes as disabled=”false”: presence still means true.
  • Reusing the same id on multiple elements: IDs should be unique within the document.
  • Forgetting alt on meaningful images: the image may become inaccessible to users who cannot see it.
  • Using an attribute on an element that does not support it: check the element reference instead of guessing.
  • Leaving complex values unquoted: consistent quotes reduce syntax mistakes.
  • Using title as the only place for critical information: tooltips are not a reliable replacement for visible text or accessible labeling.
  • Using data-* when a standard attribute already exists: prefer standard HTML semantics first.
  • Adding duplicate attributes: each attribute name should appear only once on an element.

Practice

  1. Create a new HTML file based on the document structure from OSHTML.001.
  2. Add an id and two classes to the main element.
  3. Create a link with href.
  4. Add an image with src, meaningful alt, width, and height.
  5. Create a required email input.
  6. Create a disabled button, load the page, then remove the disabled attribute and reload.
  7. Add one data-* attribute and inspect it in developer tools.
  8. Intentionally duplicate an id attribute on one element, run the markup through an HTML validator, then correct it.

Knowledge Check + Answers

  1. Where do attributes normally appear? Inside an element’s start tag after the element name.
  2. What are the two common parts of an ordinary attribute? An attribute name and an attribute value.
  3. What is a global attribute? An attribute defined for broad use across HTML elements.
  4. Should two elements normally share the same id? No. An ID should be unique within the document.
  5. Can many elements share the same class? Yes.
  6. How does a boolean attribute become false? Remove the attribute.
  7. Does disabled=”false” enable an input? No. The presence of disabled still makes the boolean state true.
  8. What does href do on an anchor? It supplies the link destination.
  9. What does alt do on an image? It provides a text alternative that communicates the image’s purpose or content in context.
  10. What are data-* attributes for? Application-specific custom data when no standard HTML attribute already represents the information.

Previous Related Lessons

Primary References

Elementary Review

Elements define the structure; attributes refine the element. Learn to recognize attribute names and values, use global attributes only when they serve a purpose, understand presence-based boolean attributes, and check element-specific documentation instead of guessing. Clean attribute use makes HTML easier to read, style, script, validate, and use with assistive technology.

Next HTML Lesson

The next HTML lesson will move from element configuration into the elements used to structure and communicate text content, including heading hierarchy, paragraphs, emphasis, and related document semantics.

BitcoinVersus.Tech Editor’s Note:
We volunteer daily to ensure the credibility of the information on this platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiatives, please donate using the QR code provided

Bitcoin donation QR code for 5,000 sats.

For best QR-code scanning results, please scan this donation code from a desktop or larger screen. Mobile screens may prevent camera recognition.

Leave a Reply