







Even among paragraphs, some are best read quietly, and some should stay a little more sharply in the reader's eye. Photos are the same. Sometimes putting a single photo on the screen is not enough. You have to say which photo to show, and for people who cannot see it, the scene needs to be conveyed in words. An HTML attribute is a small piece of information that adds a specific name, address or description to the role a tag plays.
In the previous article, we said that if a tag is a document's role label, an attribute is a note attached to that label. This time we look at four notes you will meet often when making web documents: `class`, `id`, `src` and `href`. All four are written inside a tag, but each does a different job. `class` groups similar friends, `id` points to something that exists only once, `src` finds what to fetch, and `href` tells where to go next.
1.We revisit what information an attribute adds to a tag.
2.We learn how `class` gathers several elements into the same group.
3.We see why `id` calls one place in a document by a unique name.
4.We understand how `src` and `href` make roads for photos and links.
If a tag says, “This is an image,” or “This is a link,” an attribute answers the questions that come next. Which image is it? Where do I go when I click the link? How is this paragraph different from the other paragraphs? An attribute is where you write the detail a tag alone could not say.
An attribute is usually attached after the tag name, in the form `name="content"`. The name on the left decides what will be told, and the content on the right writes the actual answer to that question. For example, `src="flower.jpg"` means that in the note slot called `src`, you have written the location of a photo called `flower.jpg`. At first, instead of memorizing this shape in full, it is enough to think that an attribute is a question and a value is the answer written to it.
Attributes are not mere decoration that makes writing showy. They help people find their way through a web document, organize parts of the same nature, and convey a photo's meaning better. If tags build the skeleton of a document, attributes tell in concrete terms how each part relates to the others.
Picture a school where students in the same club get the same badge. The students wearing the photography club badge are different people, but they share one thing in common: the photography club. HTML's `class` is like such a badge. Among many titles, paragraphs, buttons and photos, it gives the same name to those of similar nature and gathers them into one group.
For instance, if a piece of writing has several paragraphs worth remembering in particular, you can give those paragraphs the name `class="important"`. Later, you can pick out only the paragraphs with that name and style them the same way, or find them together when needed. Each is still a separate paragraph, but they come to share a common role: “important content.”
The good thing about `class` is that it can be reused in many places. If a quotation of the same look appears again and again in each chapter of a book, you needn't give each a new name. You can gather them all under one name, like `class="quote"`. When one tag has several things in common, it is also possible to attach more than one name. At first, though, rather than creating too many names, it is better to group by a standard whose reason a reader could guess just by looking.
`class` is a name that says, “This belongs to this group.” That is why it suits giving consistency to repeating elements better than finding something that exists only once. Just as many people wearing the same badge gather together, any number of elements with the same class can exist in a document.
A Way to Think About `class`
`class` is a shared name attached to several elements of a similar nature.
The same name can be used again and again on many paragraphs, images and buttons.
Elements gathered into the same group become easier to style or find together later.
It is good to choose names that bring to mind “what role it plays” rather than “what it should look like.”
If `class` is a club name, `id` is closer to a student number or the exact address of a house. You attach an `id` when you need to point to exactly one place in a document. For example, you can use it for places that must not be confused with others, such as the “Contact” section at the very end of a long text, or one particular title that opens a chapter of a book.
If a part is written as `id="contact"`, it amounts to a promise that in the document there is just one place with the name `contact`. If you give the same name to several places, the question “Which contact should I go to?” arises, and the document's road can blur. That is why it is better to think of `id` not as the name of a repeating group but as a unique name called only once within a single page.
This unique name is especially useful in long documents. A link can take readers straight to the top of the text or to a particular subheading. On paper you might guide readers with “See chapter 3 on page 127,” but on the web you can send them directly to that heading's `id`. One small signpost inside a document becomes a destination that helps readers retrace their steps or find a needed place again.
Because `id` is a strong name, it is best used sparingly. If you give every sentence a different id just to make things look neat, the document becomes needlessly complicated. First ask whether you truly must point to exactly one place, and if so, give it a simple, clear name that does not overlap with any other.
The Difference Between `class` and `id`
`class` is the name of a shared group that can be attached to many places.
`id` is a unique name attached to only one place within a single page.
`class` suits repeating quotations or notices, while `id` suits a particular subheading or destination.
For both, the more a name reveals the meaning and role of the text, the easier it is to understand later.
Imagine putting a photo inside a web page. The image tag says, “Show a photo here,” but it does not know by itself where the photo is. This is where `src` is needed. Coming from the word “source,” meaning “where it comes from,” this attribute tells the browser which file to find and show.
`src="sunset.jpg"` means, “Please fetch the photo called sunset.jpg.” Only when the photo's name and location are exact can the browser find the image you want. If you write the address wrong, a photo may fail to appear on screen, just as a parcel goes to the wrong place. `src` is not a word that makes or decorates a photo. It is the road that brings a photo already prepared into the document.
You can meet `src` outside photos too, when loading video or other material. At first, though, it is easiest to remember it together with images. `src` is the attribute that answers the question, “What should be shown in this spot, and where is it?” And to convey what the photo says within the text, you will often place a note called `alt` right beside `src`.
`href` is the road guidance attached to a link. When you click a word or sentence, you can move to another text, another page, or a particular spot on the same page. If the link tag says, “Here is a road you can click,” `href` answers, “Where do I arrive if I follow this road?”
Writing `href="about.html"` means, “Please connect to the page called about.html.” You can write the address of a faraway website, or point to a particular `id` inside the same document. That makes href useful when guiding readers to the original place of a source quoted in the text, or sending them straight to the item they want in a long notice.
A good link does not push readers somewhere unfamiliar. It carefully guides them to material that connects naturally with what they are reading, a source they can learn more from, or the page they will need next. The address you write in `href` is not mere letters, but a road readers trust and follow. When you make a link, you need the attitude of saying clearly where it goes and also checking that the place is still valid.
Easy Questions for Telling `src` from `href`
Do you need to bring a photo or material onto the screen? Then think of `src`.
Does the reader need to click and move somewhere else? Then think of `href`.
`src` is the address that brings something into the document.
`href` is the address of a road that leads out of the document to somewhere else.
`class`, `id`, `src` and `href` are all short words, but each creates a different relationship inside a document. A class connects several elements into one group, and an id gives a unique name to one place. A src brings needed material in, and an href lets readers cross to other documents and ideas. The text on screen may look the same, but with this information the document has a more accurate map.
At first you needn't use all four at once. When you put in a photo, start by thinking about why src is needed, and when you make a link, about where the href points. When similar elements repeat many times, reach for class, and when you must point to exactly one place, reach for id. Attributes are not a list of words to memorize, but words that carefully decide what a document connects and how it will be read, and by whom.
Key Takeaways
Attributes add more specific information to a tag, such as a name, an address or a description.
`class` is a repeatable name that gathers several elements by a shared nature.
`id` is a unique name that points exactly to one place in a document.
`src` tells where to fetch a photo or material from, and `href` tells where the reader will go.
