How to Use HTML Attributes โ€” HTML Tutorial #3 for Beginners

How to Use HTML Attributes โ€” HTML Tutorial #3 for Beginners

Hey friend! ๐Ÿ‘‹

Welcome back to the HTML Basics for Beginners series!

But here’s something you might be wondering…

So far, our HTML tags look like this:

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. ๐Ÿš€

How to Use HTML Attributes

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

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.

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 what
attribute="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.

Simple, right? Let’s look at the format next.

HTML Attribute Syntax: name="value"

Almost every attribute follows the same simple format:

  • 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:

See the Pen Codeboid โ€“ HTML Tutorial #3 โ€” Attributes # 2 by Codeboid (@Codeboid) on CodePen.

Let’s break it down:

AttributeNameValue
id="intro"idintro
class="highlight"classhighlight
title="Welcome message"titleWelcome 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:

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:

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 HTML
  • src โ€” used with <img> to tell the browser where the image file is. โžก๏ธ Tutorial #5: How to Add Images in HTML
  • alt โ€” used with <img> to describe the image in words. โžก๏ธ Tutorial #5: How to Add Images in HTML
  • width and height โ€” 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.

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:

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.

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: red come from CSS, which we’ll cover fully in a later tutorial. For now, just focus on how to write the style attribute. 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 ;:

See the Pen Codeboid โ€“ HTML Tutorial #3 โ€” Style Attributes # 2 by Codeboid (@Codeboid) on CodePen.

โš ๏ธ Good to know: The style attribute 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:

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.

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:

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:

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.

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

See the Pen Codeboid โ€“ HTML Tutorial #3 โ€” ID Attributes # 2 by Codeboid (@Codeboid) on CodePen.

class vs id โ€” What’s the Difference?

classid
Can multiple elements share it?โœ… YesโŒ No โ€” must be unique
Can one element have multiple?โœ… YesโŒ Only one per element
Used forStyling groups, targeting groupsStyling single element, targeting one specific element

๐Ÿ’ก Simple way to remember:
class = a school uniform โ€” many students wear the same one
id = 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:

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

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

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

See the Pen Codeboid โ€“ HTML Tutorial #3 โ€” Readonly Attribute by Codeboid (@Codeboid) on CodePen.

๐Ÿ“Œ Note: Same here โ€” <input> is just being used to demonstrate readonly. Full forms tutorial coming soon!

checked โ€” Pre-tick a Checkbox

See the Pen Codeboid โ€“ HTML Tutorial #3 โ€” Checked Attribute by Codeboid (@Codeboid) on CodePen.

All Boolean Attributes at a Glance

๐Ÿ’ก 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:

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.

โœ… Always Wrap Values in Quotes

Always use double quotes around attribute values. Single quotes work too โ€” but double quotes are the standard.

โœ… One Space Between Attributes

Keep one clean space between each attribute. Don’t cram them together.

โœ… 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.

Best Practices Summary

RuleโŒ Avoidโœ… Do This
CaseTITLE="..."title="..."
Quotesid=introid="intro"
Spacingid="a"class="b"id="a" class="b"
PlacementAttribute in closing tagAttribute 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:

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:

  1. Change style="color: darkblue;" on the heading to your favourite colour
  2. Add a title tooltip to the <h2> heading
  3. Add a third paragraph with class="highlight"
  4. Add a new input with readonly and any value you like

Full Cheat Sheet (Save This!)

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:

What’s Next?

Coming Up in Tutorial #4: How to Add Links in HTML

You’ll learn:

  • How to create clickable links with <a> and href
  • 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: and tel:
  • How to write accessible link text
  • Mini project: My Favorite Websites ๐ŸŒ

๐Ÿ‘‰ Continue to Tutorial #4: How to Add Links in HTML โ†’

Scroll to Top