HTML・First steps

What is an attribute?

You can add "settings" to a tag. That's an attribute. href, alt… let's get an early, gentle grip on the syntax you'll see tons of from here on.

We said that a tag is a sandwich that wraps content. Actually, a tag can do one more thing. Adding a “little setting” to a tag—that’s the star of this lesson, the attribute. The name sounds a bit stiff, but the idea is simple.

The shape of an attribute

For example, a link that jumps to another page when you click it. You make this by adding a setting for “where to jump” to a tag called <a>.

<a href="https://www.youtube.com/">To YouTube</a>

Broken apart, it looks like this.

PartMeaning
aThe tag name (here, a link)
hrefThe attribute name. “This is the destination address”
"https://www.youtube.com/"The attribute value. The actual destination

The name="value" set is telling it “the destination is here.” The tag handles “what it is,” and the attribute handles “what setting it has”—this teamwork is the basis of HTML.

You only write it “inside the opening tag”

An attribute is always written inside the opening tag. You don’t write it in the closing tag (</a>).

And you can add as many attributes as you like. When you add two or more, separate them with a space.

<img src="shima.png" alt="A photo of Shima">

This is an image tag. “Which image (src)” and “what image it is (alt)“—two settings, lined up and separated by a space.

The attributes you’ll meet from here

For reference, let me give a little preview of the attributes that show up in the coming lessons. They’re all the same name="value" shape.

AttributeMeaningWhere it appears
hrefA link’s destinationThe link <a> lesson
srcThe location of the image to displayThe image <img> lesson
altThe image’s descriptionThe image <img> lesson
targetHow the link opens (a new tab, etc.)The link <a> lesson

Even when the kind of tag differs, the way you write attributes stays the same. Learn this one shape today, and you’ll be able to read every attribute from here on.

Summary of this lesson

  1. An attribute is a “setting” you add to a tag, in the shape name="value"
  2. You write it only inside the opening tag (as many as you like, separated by spaces)
  3. You don’t need to memorize the names—noticing “this is an attribute” is enough

FAQ

Is there a set order for writing attributes?
No, there isn't. In an img tag, whether you write src or alt first, it works the same way. Whatever order is easiest for you to read is fine.
Can I wrap the value in single quotes?
It works. But HTML out in the world is mostly written with double quotes, so it's a good idea to stick with double quotes at first.
How many attribute names should I memorize?
You don't need to memorize any right now. href in the link lesson, src and alt in the image lesson—they show up alongside the tags each time, so learning them as they appear is plenty.