HTML Attributes
Today we look at HTML attributes, the name="value" details that live in the opening tag and tell the browser more about an element, such as where a link should go.
- 1
What an attribute is
In the last lesson we built complete pieces called elements. An opening tag starts a piece, but sometimes the tag alone is not enough. A link needs a destination. A button may need a name you can point to later. Those extra facts are attributes.
An attribute is a
name="value"pair written inside the opening tag. In the example below,href="https://codexcider.com"is one attribute. The name ishref. The value is the URL. - 2
Why Plain Tags Aren't Enough: Giving Elements Context
Imagine you write
<a>Visit CodeX Cider</a>. The browser knows this is a link, but it does not know where the click should go. Without a destination, the link is almost useless.So HTML lets you attach small labelled facts to the opening tag. Put the address in
href, give the element a unique name withid, put it in a group withclass, or add hover text withtitle. The tag stays the same. The attributes change the details. - 3
How Browsers Parse Key-Value Pairs inside Opening Tags
The browser reads attributes when it reads the opening tag. It does not look for them in the closing tag. Write
href,id,class, andtitleon<a ...>, then write the link text, then close with</a>.Two names matter for later work with CSS and JavaScript.
idpoints to one element on the page.classcan be shared by many elements. That is why you never repeat the sameidtwice on one page. - 4
Hands-On: Inspecting a Hyperlink with Multiple Attributes
The example below is one link with four attributes. Read the code once, then look at the browser pane. Hover the link to see the
titletooltip. - 5
The Golden Rule: Attributes Always Live in Opening Tags
In HTML the group attribute is written
class, notclassName.classNamebelongs to React. If you put attributes on the closing tag, or invent a name the browser does not know, that detail is ignored or the page behaves oddly.
Must be 100% unique across the entire HTML page. Never repeat the same ID twice. Crucial for JavaScript targeting and in-page anchor navigation.
Can be shared across any number of elements on the same page. Allows you to style repeated UI patterns (cards, buttons, alerts) consistently.
What you'll learn
- 1Attributes are name="value" pairs on the opening tag
- 2Why a plain <a> needs href to become a real link
- 3How the browser reads attributes with the opening tag
- 4id is unique; class can be shared
- 5title shows a tooltip on hover
Line by line
id="main-cta"A unique name for this one element on the page. Use it later in CSS as
#main-cta, or in JavaScript. Only one element may have this id.class="btn"A reusable group name. Many links or buttons can share
class="btn". In HTML the attribute isclass, notclassName.href="https://codexcider.com"The destination URL. Without
href, an<a>is not really a working link.title="Go to CodeX Cider"Extra text that appears when someone rests the mouse on the link. It is a small tip, not the main label.
Try it yourself
Make a link to https://www.google.com with id="search-link", class="btn", and title="Open Google". Put every attribute in the opening <a> tag.
Hint: Opening tag holds the attributes. Then the link text. Then </a>.
Common mistakes
- 1.Writing
classNamein an HTML file. That name is for React. In HTML the attribute isclass. - 2.Using the same
idtwice on one page. An id must be unique. Useclasswhen many elements share a group.
Interview prep (Click to reveal)
Q: What is an HTML attribute and what is its syntax?↓
An attribute provides additional metadata or configuration to an HTML element. It is written as a name="value" pair inside the opening tag (e.g. href="https://codexcider.com").
Q: What is the crucial difference between id and class?↓
An id must be unique within the entire document, used for single-element hooks (in CSS with #id or JS with getElementById). A class can be assigned to multiple elements to apply shared styles (in CSS with .classname).
Q: Can attributes be placed on a closing tag?↓
No, never. Attributes are strictly placed on the opening tag (or self-closing tag). Putting attributes inside a closing tag (like </a href="...">) is invalid HTML and completely ignored by browsers.