DevAcademy
LearnHTMLHTML Attributes
BeginnerHTML

HTML Attributes

Learn how attributes provide additional information about elements, and explore the most common global attributes.

Reading Time

12 min

Lesson

Lesson 5 of 27

What Are Attributes?

Attributes provide extra information about an element. They are always specified in the opening tag as name/value pairs, in the form name="value".

Attribute Syntax

<a href="https://example.com" target="_blank">Visit Example</a>
<img src="logo.png" alt="Company logo" width="120" />
Output

Common Global Attributes

AttributePurpose
idUniquely identifies a single element on the page
classAssigns one or more class names for styling/scripting
styleApplies inline CSS directly to the element
titleShows a tooltip when hovering the element
langDeclares the language of the element’s content
hiddenHides the element from the page

Attributes Are Element-Specific

While global attributes work on almost any element, many attributes only make sense on specific elements — for example, href only applies to <a>, and src only applies to elements like <img>, <script>, and <video>.

Element-Specific Attributes

<input type="email" placeholder="you@example.com" required />
<video src="movie.mp4" controls></video>
Output

Boolean Attributes

Some attributes do not take a value — their mere presence turns a feature on. Examples include disabled, required, checked, and hidden.

Boolean Attributes Example

<input type="text" disabled />
<input type="checkbox" checked />
Output

Quoting Attribute Values

Attribute values should always be wrapped in double or single quotes. Unquoted values can work for simple cases but break as soon as the value contains a space.

id Must Be Unique

Unlike class, an id value must be unique within the entire page. Using the same id on multiple elements is invalid HTML and can break CSS selectors and JavaScript lookups.

Best Practice

Prefer class over id for styling, since classes are reusable across many elements. Reserve id for unique anchors, JavaScript hooks, or accessibility references like aria-labelledby.

Interview Questions

Quick Quiz

1. Where are attributes written in an HTML element?

2. Which attribute must be unique across an entire page?

3. Which of these is a boolean attribute?