How to Use HTML Attributes โ HTML Tutorial #3 for Beginners
Hey friend! ๐
Welcome back to the HTML Basics for Beginners series!
In Tutorial #2.1, you learned how to format text โ quotes, code snippets, special characters, and more. Your pages are starting to look really solid! ๐
But here’s something you might be wondering…
So far, our HTML tags look like this:
<p>Hello, world!</p>
<h1>My Page</h1>They work โ but they’re pretty plain. They just show up and do their basic job.
What if you want to give an element a name? Or add a small tooltip? Or tell the browser that a button should be turned off?
You need something extra. And that something extra is called an attribute.
Think of it like ordering a coffee. โ
“Coffee” is the order โ that’s your HTML tag.
“Large, oat milk, no sugar” is the extra detail โ that’s your attribute.
Same coffee. Totally different result.
And today, you’re going to learn exactly how they work. ๐

What You’ll Learn Today about HTML Attributes
By the end of this tutorial, you’ll be able to:
โ
Explain what HTML attributes are and why they exist
โ
Write attributes using the correct name="value" syntax
โ
Recognise common element-specific attributes like href, src, and alt
โ
Use global attributes like style, title, class, and id
โ
Understand boolean attributes โ the on/off switches of HTML
โ
Follow best practices for writing clean, professional HTML
Let’s go! ๐ฅ
Before You Start
Prerequisites:
โ
Completed Tutorial #2.1 โ HTML Text Formatting: Quotes, Code & Special Characters
โ
A text editor (VS Code, Notepad, etc.)
โ
A web browser (Chrome, Firefox, etc.)
Ready? Let’s dive in!
Why HTML Attributes Matter
Before we touch any code, let’s understand why attributes exist in the first place.
Imagine you’re giving someone directions.
You could say: “Go to the store.”
That works โ but it’s vague. Which store? How far? Which road?
Now try: “Go to the grocery store on Main Street, 2km away, on the left side.”
Much better. The extra details make the instruction useful.
That’s exactly what HTML attributes do for HTML tags.
The tag says what the element is. The attribute says how it should behave.
<!-- The tag alone โ just shows up, nothing special -->
<p>Hello, world!</p>
<!-- The tag + attribute โ now it has a tooltip on hover! -->
<p title="This is a greeting">Hello, world!</p>See the Pen Codeboid โ HTML Tutorial #3 โ Attributes by Codeboid (@Codeboid) on CodePen.
Same tag. Same text. But the second one does something extra โ hover over it in your browser and you’ll see a little tooltip pop up!
The attribute is what gives it that extra behaviour.
๐ก Simple mental model:<tag> = the whatattribute="value" = the how
Step 1: What Are HTML Attributes?
HTML attributes are extra instructions you place inside an HTML tag.
They give the browser more information about how to display or handle that element.
Here’s the most important rule to remember:
๐จ Attributes always go inside the opening tag โ never the closing tag.
<!-- โ
Correct โ attribute in the opening tag -->
<p title="This is a paragraph">Hello, world!</p>
<!-- โ Wrong โ never put attributes in the closing tag -->
<p>Hello, world!</p title="This is a paragraph">Simple, right? Let’s look at the format next.
HTML Attribute Syntax: name="value"
Almost every attribute follows the same simple format:
name="value"- name โ what you want to adjust (e.g.
id,class,title) =โ connects the name to the value- “value” โ the setting you want to apply (always wrapped in double quotes)
Here’s a real example:
<p id="intro" class="highlight" title="Welcome message">
Hello, world!
</p>See the Pen Codeboid โ HTML Tutorial #3 โ Attributes # 2 by Codeboid (@Codeboid) on CodePen.
Let’s break it down:
| Attribute | Name | Value |
|---|---|---|
id="intro" | id | intro |
class="highlight" | class | highlight |
title="Welcome message" | title | Welcome message |
One tag, three attributes โ each one adding a specific piece of information. Easy! ๐
Multiple Attributes on One Tag
You can add as many attributes as you need on one tag. Just separate them with a space:
<h1 id="page-title" class="main-heading" title="Welcome!">
My Page
</h1>See the Pen Codeboid โ HTML Tutorial #3 โ Attributes # 3 by Codeboid (@Codeboid) on CodePen.
The order doesn’t matter to the browser โ but being consistent makes your code easier to read.
Practice Challenge ๐ฏ
Look at this tag and name every attribute and its value:
<p id="about" class="intro-text" title="About this page">
This is my webpage.
</p>Then try writing your own <h2> tag with at least two attributes.
Step 2: A Quick Peek โ Element-Specific Attributes
Not all attributes are universal. Some are built to work with specific tags only.
You’ll learn these in full detail in the upcoming tutorials โ but it’s good to know they exist right now, so nothing feels mysterious when you see them. ๐
hrefโ used with<a>to set where a link goes. โก๏ธ Tutorial #4: How to Add Links in HTMLsrcโ used with<img>to tell the browser where the image file is. โก๏ธ Tutorial #5: How to Add Images in HTMLaltโ used with<img>to describe the image in words. โก๏ธ Tutorial #5: How to Add Images in HTMLwidthandheightโ used with<img>to set the display size. โก๏ธ Tutorial #5: How to Add Images in HTML
No need to memorise these right now โ just know they exist.
You’ll get hands-on practice with every single one very soon! ๐
Step 3: Global Attributes โ The Multitaskers
Here’s where it gets really interesting.
Global attributes are special โ they work on almost any HTML element, not just specific ones.
Think of them as tools you can reach for on any tag, any time. ๐ ๏ธ
The four most useful ones for beginners are: style, title, class, and id.
Let’s go through each one.
The title Attribute โ Tooltip on Hover
The title attribute adds a small tooltip that pops up when a user hovers their mouse over an element.
<p title="This is extra info!">Hover over me slowly...</p>
<h2 title="This is the about section">About Me</h2>Try it in your browser right now โ hover your mouse slowly over the element and watch the tooltip appear!
See the Pen Codeboid โ HTML Tutorial #3 โ Title Attributes by Codeboid (@Codeboid) on CodePen.
It works on paragraphs, headings, buttons โ anything. ๐
๐ก Best for: Adding helpful hints or extra context without cluttering the page.
Practice Challenge ๐ฏ
Add a title tooltip to three different elements โ a heading, a paragraph, and a button:
<h1 title="Welcome to my page">My Website</h1>
<p title="This is the introduction">Hello there!</p>
<button title="Click to continue">Next</button>Open it in your browser and hover over each one!
The style Attribute โ Quick Inline Styling
The style attribute lets you add a visual style directly to any element. Basically, we add CSS using the style attribute.
<p style="color: red;">This text is red!</p>
<h2 style="color: blue;">This heading is blue!</h2>See the Pen Codeboid โ HTML Tutorial #3 โ Style Attributes by Codeboid (@Codeboid) on CodePen.
๐ New to CSS? Don’t worry about what’s inside the quotes just yet โ things like
color: redcome from CSS, which we’ll cover fully in a later tutorial. For now, just focus on how to write thestyleattribute. You can copy the examples as-is and experiment! We’ll explain everything inside the quotes when we get to CSS. ๐
You can also combine multiple styles by separating them with a semicolon ;:
<p style="color: white; background-color: black;">
White text on a black background!
</p>See the Pen Codeboid โ HTML Tutorial #3 โ Style Attributes # 2 by Codeboid (@Codeboid) on CodePen.
โ ๏ธ Good to know: The
styleattribute is great for learning and quick experiments. But in real projects, styles usually live in a separate CSS file โ we’ll get to that later in the series!
Practice Challenge ๐ฏ
Try changing the colour of a heading and a paragraph:
<h1 style="color: darkblue;">My Blue Heading</h1>
<p style="color: green;">My green paragraph!</p>Play around with different colour names โ red, orange, purple, pink. Have fun with it! ๐จ
The class Attribute โ A Reusable Label
The class attribute gives an element a label โ and the same label can be used on as many elements as you want.
<p class="highlight">This paragraph is labelled "highlight".</p>
<p class="highlight">So is this one!</p>
<p>But not this one.</p>See the Pen Codeboid โ HTML Tutorial #3 โ Class Attributes # 1 by Codeboid (@Codeboid) on CodePen.
Right now the label doesn’t change how it looks โ but when you learn CSS later, you’ll be able to style all elements with the same class at once. Super powerful! ๐ช
One element can also have multiple classes โ just separate them with a space:
<p class="highlight large bold">This has three class labels!</p>See the Pen Codeboid โ HTML Tutorial #3 โ Class Attributes # 2 by Codeboid (@Codeboid) on CodePen.
๐ก Think of class as: A label on a group of students wearing the same uniform. One label, many students.
Practice Challenge ๐ฏ
Give three paragraphs the same class name, and one paragraph a different class:
<p class="intro">First paragraph.</p>
<p class="intro">Second paragraph.</p>
<p class="intro">Third paragraph.</p>
<p class="note">This one is different.</p>The id Attribute โ A Unique Name
The id attribute gives an element a unique name. Unlike class, no two elements on the same page should share the same id.
<h1 id="page-title">Welcome to My Page</h1>
<p id="intro-text">This is the introduction.</p>See the Pen Codeboid โ HTML Tutorial #3 โ ID Attributes # 1 by Codeboid (@Codeboid) on CodePen.
The label doesn't change how it looks. However, id is useful for:
- CSS โ styling one specific element
- JavaScript โ targeting one specific element
- In-page links โ jumping to a section on the page when clicked
<!-- Clicking this jumps to the "about" section -->
<a href="#about">Go to About Section</a>
<!-- The line breaks below push the "About Me" section further down
so you can actually see the page jump when you click the link! -->
<br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br>
<br><br><br><br>
<!-- This is the section it jumps to -->
<h2 id="about">About Me</h2>See the Pen Codeboid โ HTML Tutorial #3 โ ID Attributes # 2 by Codeboid (@Codeboid) on CodePen.
class vs id โ What’s the Difference?
class | id | |
|---|---|---|
| Can multiple elements share it? | โ Yes | โ No โ must be unique |
| Can one element have multiple? | โ Yes | โ Only one per element |
| Used for | Styling groups, targeting groups | Styling single element, targeting one specific element |
๐ก Simple way to remember:class = a school uniform โ many students wear the same oneid = a student ID card โ every student has a unique one
Practice Challenge ๐ฏ
Add a unique id to the heading and a shared class to both paragraphs:
<!-- Before -->
<h1>My Webpage</h1>
<p>First paragraph.</p>
<p>Second paragraph.</p>
<!-- After -->
<h1 id="main-title">My Webpage</h1>
<p class="body-text">First paragraph.</p>
<p class="body-text">Second paragraph.</p>Step 4: Boolean Attributes โ The On/Off Switches
These are a little different โ and honestly, they’re kind of fun. ๐
Boolean attributes don’t follow the name="value" format. They work like a light switch:
- Present in the tag โ the feature is ON
- Missing from the tag โ the feature is OFF
No value needed. Just the word itself!
disabled โ Turn Off a Button
<!-- Normal button โ works fine -->
<button>Click Me</button>
<!-- Disabled button โ greyed out, can't be clicked -->
<button disabled>Can't Click Me</button>You don’t write disabled="true". Just disabled is enough!
See the Pen Codeboid โ HTML Tutorial #3 โ Disabled Attribute by Codeboid (@Codeboid) on CodePen.
required โ Make a Field Mandatory
<form>
<input type="text" placeholder="Your name" required>
<button type="submit">Submit</button>
</form>Try leaving the field empty and clicking Submit โ the browser won’t let you through! ๐
See the Pen Codeboid โ HTML Tutorial #3 โ Required Attribute by Codeboid (@Codeboid) on CodePen.
๐ Note: You’ll notice we’re using
<input>and<form>here to show the boolean attribute in action. We’ll cover these elements and forms in full detail in a later tutorial โ so don’t worry about them too much right now!
readonly โ Show It But Lock It
<input type="text" value="You can read this but not edit it" readonly>See the Pen Codeboid โ HTML Tutorial #3 โ Readonly Attribute by Codeboid (@Codeboid) on CodePen.
๐ Note: Same here โ
<input>is just being used to demonstratereadonly. Full forms tutorial coming soon!
checked โ Pre-tick a Checkbox
<input type="checkbox" checked> I agree to the termsSee the Pen Codeboid โ HTML Tutorial #3 โ Checked Attribute by Codeboid (@Codeboid) on CodePen.
All Boolean Attributes at a Glance
<button disabled>Disabled Button</button>
<input type="text" required>
<input type="text" readonly>
<input type="checkbox" checked>๐ก Think of boolean attributes as: A checkbox. Either it’s ticked โ or it’s not. No in-between.
Practice Challenge ๐ฏ
Create a small form with all four boolean attributes:
<p>Required field:</p>
<input type="text" placeholder="Your name" required>
<p>Read-only field:</p>
<input type="text" value="Cannot edit this" readonly>
<p>Pre-ticked checkbox:</p>
<input type="checkbox" checked> Subscribe to newsletter
<p>Disabled button:</p>
<button disabled>Submit</button>Open it in your browser and interact with each one. Notice what they do! ๐
Step 5: Best Practices for Writing Clean Code
You now know how attributes work. Let’s make sure you write them the right way from day one.
These small habits will make your code look professional and avoid headaches later. ๐งน
โ Always Write Attribute Names in Lowercase
HTML doesn’t force you to do this โ but lowercase is the standard that every developer follows.
<!-- โ Avoid โ works but looks sloppy -->
<p TITLE="hello" CLASS="intro">Hello</p>
<!-- โ
Always do this -->
<p title="hello" class="intro">Hello</p>โ Always Wrap Values in Quotes
Always use double quotes around attribute values. Single quotes work too โ but double quotes are the standard.
<!-- โ No quotes โ can break in some browsers -->
<p id=intro class=highlight>Hello</p>
<!-- โ
Double quotes โ always do this -->
<p id="intro" class="highlight">Hello</p>
<!-- โ
Single quotes โ useful when the value contains double quotes -->
<p title='She said "Hello!"'>Hover over me</p>โ One Space Between Attributes
Keep one clean space between each attribute. Don’t cram them together.
<!-- โ Hard to read -->
<p id="intro"class="highlight"title="Hello">Welcome</p>
<!-- โ
Clean and easy to read -->
<p id="intro" class="highlight" title="Hello">Welcome</p>โ Attributes Go in the Opening Tag Only
We covered this in Step 1, but it’s worth saying again โ never put attributes in the closing tag.
<!-- โ Wrong -->
<p>Hello</p class="intro">
<!-- โ
Correct -->
<p class="intro">Hello</p>Best Practices Summary
| Rule | โ Avoid | โ Do This |
|---|---|---|
| Case | TITLE="..." | title="..." |
| Quotes | id=intro | id="intro" |
| Spacing | id="a"class="b" | id="a" class="b" |
| Placement | Attribute in closing tag | Attribute in opening tag only |
Mini Project โ Attributes Practice Page ๐ ๏ธ
Let’s put everything together and build a small practice page.
Create a new file called attributes-practice.html and type this out:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>HTML Attributes Practice</title>
</head>
<body>
<!-- id and style -->
<h1 id="page-title" style="color: darkblue;">My Attributes Practice Page</h1>
<!-- title tooltip -->
<p title="This is the introduction">
Hover over this paragraph to see a tooltip! ๐
</p>
<!-- class on multiple elements -->
<h2>My Favourite Things</h2>
<p class="favourite">I love coding!</p>
<p class="favourite">I love music!</p>
<p class="favourite">I love travel!</p>
<hr>
<!-- Boolean attributes -->
<h2 id="boolean-section">Boolean Attributes</h2>
<p>Required field โ try clicking Submit without typing anything:</p>
<form>
<input type="text" placeholder="Your name" required>
<button type="submit">Submit</button>
</form>
<p>Read-only field โ try editing it:</p>
<input type="text" value="This is read-only" readonly>
<p>Pre-ticked checkbox:</p>
<input type="checkbox" checked> I agree to the terms
<p>Disabled button:</p>
<button disabled>Can't Click Me</button>
<hr>
<p><small>Copyright © 2026 My Attributes Page</small></p>
</body>
</html>What’s Happening Here?
โ
id="page-title" โ unique name for the heading
โ
style="color: darkblue;" โ quick colour (CSS syntax โ we’ll cover this soon!)
โ
class="intro-text" โ reusable label on the paragraph
โ
title="..." โ tooltip appears on hover
โ
class="highlight" on two paragraphs โ same label on both
โ
required โ field must be filled before submitting
โ
readonly โ visible but not editable
โ
checked โ checkbox is pre-ticked
โ
disabled โ button is turned off
Your Turn! ๐ฏ
Customise this page:
- Change
style="color: darkblue;"on the heading to your favourite colour - Add a
titletooltip to the<h2>heading - Add a third paragraph with
class="highlight" - Add a new input with
readonlyand any value you like
Full Cheat Sheet (Save This!)
โโ ATTRIBUTE SYNTAX โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
name="value" โ Standard format
<tag name="value">Content</tag> โ Attributes go in the OPENING tag only
โโ ELEMENT-SPECIFIC ATTRIBUTES (coming in upcoming tutorials) โโโโโ
href="URL" โ Link destination โ Tutorial #4: HTML Links
src="path" โ Image source โ Tutorial #5: HTML Images
alt="text" โ Image description โ Tutorial #5: HTML Images
width="pixels" โ Image width โ Tutorial #5: HTML Images
height="pixels" โ Image height โ Tutorial #5: HTML Images
โโ GLOBAL ATTRIBUTES (work on any element) โโโโโโโโโโโโโโโโโโโโโโโโ
title="text" โ Tooltip on hover
style="css" โ Inline styling (CSS โ covered in a later tutorial)
class="name" โ Reusable label for CSS and JavaScript
id="name" โ Unique name for CSS, JavaScript, and in-page links
โโ BOOLEAN ATTRIBUTES (no value needed โ just write the word!) โโโโ
disabled โ Turns off a button or input
required โ Makes a form field mandatory
readonly โ Shows content but blocks editing
checked โ Pre-ticks a checkbox
โโ BEST PRACTICES โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
Always lowercase title not TITLE
โ
Always use quotes id="intro" not id=intro
โ
Space between attrs id="a" class="b" not id="a"class="b"
โ
Opening tag only never put attributes in the closing tag
Quick Recap
Today you learned:
โ
Attributes are extra instructions inside the opening tag
โ
The standard syntax is name="value"
โ
Element-specific attributes like href, src, and alt exist โ coming in Tutorials #4 and #5
โ
Global attributes work on any element: title, style, class, id
โ
Boolean attributes are on/off switches: disabled, required, readonly, checked
โ
Best practices: lowercase names, always quote values, one space between attributes
โ
Built a complete Attributes Practice Page
Common Mistakes to Avoid
โ Putting attributes in the closing tag โ opening tag only!
โ Forgetting quotes around values โ id=intro can break your code
โ Using the same id on two elements โ id must be unique per page
โ Confusing class and id โ class is reusable, id is unique
โ Writing attributes in UPPERCASE โ always use lowercase
โ Forgetting a space between attributes โ id="a"class="b" will break things
โ Writing disabled="true" โ just write disabled, no value needed
Get the Source Code
You can find all the code from this tutorial on GitHub:
๐ Codeboid/html-series
What’s Next?
Coming Up in Tutorial #4: How to Add Links in HTML
You’ll learn:
- How to create clickable links with
<a>andhref - How to use absolute vs. relative URLs
- How to open links in a new tab with
target - How to link to email addresses and phone numbers with
mailto:andtel: - How to write accessible link text
- Mini project: My Favorite Websites ๐

