HTML
HTML Attributes
Add extra information to your HTML elements using attributes like class, id, and src to control styling, behavior, and content.
Think of an HTML element as a person. The tag tells you what kind of person they are — a teacher, a student, a doctor. But attributes tell you specific details about that person — their name, age, or where they live. An attribute is extra information you add to an HTML tag. It goes inside the opening tag and gives the browser more details about what to do with that element. Every attribute has a name and a value, separated by an equals sign.Anatomy of an Attribute
Before we jump into specific attributes, let's break down exactly how they work:Global Attributes
Some attributes work on every single HTML element. These are called global attributes. You can add them to paragraphs, images, divs, links — anything.The id Attribute
Anid gives an element a unique name on your page. Think of it like a Social Security number — only one element can have that specific id.
Alex wants to add a unique identifier to the main heading on their portfolio:
<!-- Main heading with unique id for Alex's portfolio -->
<h1 id="main-title">Alex Chen - Web Developer</h1>
<!-- Navigation with unique id -->
<nav id="main-navigation">
<p>Home | Projects | Contact</p>
</nav>
<!-- Content section with unique id -->
<section id="about-me">
<h2>About Me</h2>
<p>I build websites that people actually want to use.</p>
</section>What just happened?
Each element now has a unique identifier. The browser treats them exactly the same visually, but CSS and JavaScript can target them specifically. Try this: Right-click in the preview and "Inspect Element" — you'll see the id attributes in the browser's developer tools.
The class Attribute
Unlike ids,class attributes can be shared by multiple elements. Think of classes like t-shirt sizes — lots of people can wear "medium."
Alex wants to group similar elements together for styling:
<!-- Project cards with shared styling class -->
<div class="project-card">
<h3>E-commerce Website</h3>
<p>Built with HTML, CSS, and JavaScript</p>
</div>
<div class="project-card">
<h3>Portfolio Website</h3>
<p>Responsive design with modern layout</p>
</div>
<div class="project-card featured">
<h3>Task Management App</h3>
<p>React-based application with database</p>
</div>project-card and featured. You separate multiple classes with spaces. That element gets all the styling from both classes.
The title Attribute
Thetitle attribute adds a tooltip that appears when someone hovers over an element. Perfect for extra context or explanations.
<!-- Tooltips for Alex's portfolio navigation -->
<nav>
<a href="#" title="Go to homepage">Home</a> |
<a href="#" title="View my recent projects">Projects</a> |
<a href="#" title="Get in touch with me">Contact</a>
</nav>
<p>I specialize in <span title="HyperText Markup Language">HTML</span>,
<span title="Cascading Style Sheets">CSS</span>, and
<span title="A programming language for web interactivity">JavaScript</span>.</p>What just happened?
Hover your mouse over any of the links or technical terms in the preview. You'll see helpful tooltips appear with extra information. Try this: Add title attributes to abbreviations or technical terms your visitors might not understand.
Element-Specific Attributes
While global attributes work everywhere, many attributes only work with specific elements. These attributes are designed for particular jobs.Link Attributes (href, target)
Thehref attribute tells a link where to go. Without it, you just have regular text. The target attribute controls how the link opens.
<!-- Different types of links for Alex's portfolio -->
<nav>
<a href="index.html">Home</a> |
<a href="projects.html">My Projects</a> |
<a href="contact.html">Contact Me</a>
</nav>
<p>Check out my work on
<a href="#" target="_blank">GitHub</a> (opens new tab)</p>
<p>Jump to <a href="#contact-section">contact section</a> on this page</p>
<p>Email me: <a href="mailto:alex@example.com">alex@example.com</a></p>href value does something different. File names link to other pages. mailto: opens email. #section-name jumps to an element with that id on the same page.
Image Attributes (src, alt, width, height)
Images need thesrc attribute to know what picture to show. The alt attribute describes the image for screen readers and shows text if the image fails to load.
<!-- Alex's portfolio images with proper attributes -->
<h2>My Recent Projects</h2>
<div style="background:#e2e8f0;padding:20px;text-align:center;color:#64748b;border-radius:4px;width:200px;height:150px;display:inline-block;">[Image: E-commerce site screenshot]</div>
<div style="background:#e2e8f0;padding:20px;text-align:center;color:#64748b;border-radius:4px;width:100px;height:100px;display:inline-block;margin-left:20px;">[Alex's headshot]</div>
<p>The first image shows my e-commerce project at 200x150 pixels. The second is my profile photo at 100x100 pixels.</p>Pro tip: Always include alt text for images. Screen readers use it to describe images to visually impaired users. Good alt text: "Alex Chen smiling in a blue shirt." Bad alt text: "image1.jpg"
Data Attributes
Sometimes you need to store custom information with an element. That's wheredata- attributes come in. You can make up any name after "data-" and JavaScript can read it later.
Alex wants to store project information for interactive features:
<!-- Project cards with custom data for Alex's portfolio -->
<div class="project" data-year="2023" data-tech="html,css,js" data-status="completed">
<h3>Restaurant Website</h3>
<p>Responsive website with online ordering system</p>
</div>
<div class="project" data-year="2023" data-tech="react,nodejs" data-status="in-progress">
<h3>Social Media App</h3>
<p>Full-stack application with real-time messaging</p>
</div>
<p>JavaScript can read these data attributes to filter projects by year, technology, or status.</p>data-price on product elements or data-category on blog posts.
Boolean Attributes
Some attributes don't need values — they're either present or not. These are called boolean attributes. If the attribute exists, it's "true." If it's missing, it's "false." Common boolean attributes includedisabled, required, and hidden:
<!-- Contact form with boolean attributes -->
<h2>Contact Alex</h2>
<form>
<p><input type="text" placeholder="Your name" required></p>
<p><input type="email" placeholder="Your email" required></p>
<p><textarea placeholder="Your message" required></textarea></p>
<p><input type="checkbox" checked> Subscribe to newsletter</p>
<p><button disabled>Submit (form not ready)</button></p>
</form>
<div hidden>This content is hidden from view</div>What just happened?
The form fields show red outlines because they're required but empty. The checkbox is pre-checked, the button is disabled (grayed out and unclickable), and the hidden div doesn't appear at all. Try this: Fill in the form fields and notice how the browser validates required fields.
Common Mistakes with Attributes
Even experienced developers make these attribute mistakes. Here's how to avoid them:Mistake: Missing quotes around attribute values
Wrong: class=project card (browser sees two separate attributes)
Right: class="project card" (one attribute with two classes)
Mistake: Duplicate ids on the same page
Wrong: Two elements with id="header"
Right: Use id="header" once and class="header-style" for styling multiple elements
Mistake: Forgetting alt attributes on images
Wrong: <img src="photo.jpg"> (inaccessible)
Right: <img src="photo.jpg" alt="Alex presenting at tech conference">
CLASS and class both work, but stick with lowercase for consistency.
Attributes turn basic HTML elements into powerful, interactive components. They're the bridge between HTML structure, CSS styling, and JavaScript behavior. Master them, and you'll have precise control over every element on your pages.
Quiz
Alex wants to create a unique identifier for the main heading of their portfolio. Which attribute should they use?
What's the main difference between id and class attributes?
Which attribute should Alex include on all images to make their portfolio accessible to screen readers?
Up Next: Text Formatting Tags
Learn how to make text bold, italic, underlined, and highlighted with semantic HTML formatting tags.