#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
HTML Fundamentals

HTML Attributes

HTML attributes are extra pieces of information written inside an element's opening tag. They change how the element behaves or give the browser details it needs: where a link goes, which image to load, what kind of input to show, or which language the page is in. They are written as name="value" pairs, and some, called boolean attributes, work just by being present.

Writing attributes correctly

<a href="/contact" title="Send us a message">Contact</a>

<img src="team.jpg" alt="Five people around a whiteboard" width="640" height="360">

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

<button type="submit" disabled>Sending...</button>

Put attributes only in the opening tag, separate them with spaces, and wrap values in double quotes. Attribute names are not case-sensitive, but lowercase is the convention. Each attribute should appear only once per element.

Global attributes work on any element

AttributePurpose
idA unique identifier for one element on the page, used by CSS, JavaScript, labels and in-page links
classOne or more space-separated names for styling and scripting groups of elements
styleInline CSS; handy for quick tests, but hard to maintain
titleExtra advisory text, often shown as a tooltip
langThe language of the content, such as en or hi
hiddenHides the element from display and assistive technology
data-*Custom data for your scripts, read in JavaScript through element.dataset
tabindexControls whether and how an element can receive keyboard focus

Element-specific attributes

Many attributes belong to particular elements. href and target belong to links. src, alt, width and height belong to images. Form controls use type, name, value, placeholder, required, min, max and pattern. The for attribute on a <label> must match the id of its input, which makes the label clickable and readable by screen readers.

Boolean attributes

Attributes like required, disabled, checked, readonly and autofocus are on when present and off when absent. Writing disabled="false" does not enable the element; the attribute is still there, so it is still disabled. To turn it off you must remove the attribute entirely.

Custom data attributes in practice

<button class="add-to-cart" data-product-id="42" data-price="499">Add</button>

<script>
  document.querySelector('.add-to-cart').addEventListener('click', (e) => {
    const { productId, price } = e.currentTarget.dataset;
    console.log(productId, Number(price));
  });
</script>

Hyphenated names become camelCase in dataset, so data-product-id is read as dataset.productId. Values always come back as strings.

Mistakes worth avoiding

Missing or useless alt text is the most frequent one. Every content image needs an alt that describes it; purely decorative images should have an empty alt="" so screen readers skip them. Leaving the attribute out makes some screen readers read the file name aloud.

Reusing the same id on several elements is another. Browsers will not complain, but getElementById returns only the first, labels attach to the wrong input and in-page links jump to the wrong spot. Use class for anything that repeats.

Also be careful with target="_blank". Modern browsers now treat it as if rel="noopener" were set, but adding rel="noopener" yourself keeps older browsers safe too.

Key points

  • Attributes go in the opening tag as quoted name-value pairs.
  • id must be unique; class can repeat.
  • Boolean attributes work by presence, not by value.
  • Give every meaningful image a descriptive alt.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course