cluster1
cluster1

Reputation: 5664

Are the attributes of an HTML-element part of the tag?

I've got to prepare a workshop for HTML-beginners and so I came across over the following.

If I have ...

<p>This is a text-node.</p>

... then <p> and </p> are tags. The whole thing '<p>This is a text-node.</p>' is an element.

So far, so good.

But when I've attributes in it like '<p class="foobar">This is a text-node.</p>' ...

Is then the attribute part of the tag?

Means: The tag is everything from the opening bracket to the closing bracket including the attributes? Or have I got something wrong? Then please correct me.

Upvotes: 1

Views: 76

Answers (2)

Chris
Chris

Reputation: 59491

Correct. A tag is everything between a < and the immediately following />. So the class="foobar" part is indeed inside of your paragraph tag.

As BoltClock correctly pointed out, the class attribute (or any other attribute for that matter), isn't something that goes hand-in-hand with the tag itself. It simply declares special behavior.

The MDN says:

[Attributes] are additional values that configure the elements or adjust their behavior in various ways to meet the criteria the users want.

So in your case the class="foobar" says that this specific p tag has a class by the name of "foobar".

What is a class?

The class global attribute is a space-separated list of the classes of the element. Classes allows CSS and Javascript to select and access specific elements.

So what does this mean? Consider the following:

<p>This is a text-node.</p>
<p class="foobar">This is another text-node.</p>

The above markup has 2 paragraph elements. By giving one of them a class attribute, I can later specify exactly which paragraph tag, or set of paragraph tags, I want to give special behavior (color for example). The usefulness of this becomes more obvious as your markup becomes really large.

Hope this clarifies things up a bit.

Upvotes: 1

BoltClock
BoltClock

Reputation: 723448

Your understanding is correct. The attributes declared within a start tag are part of the start tag. This is true in both HTML and XML.

To be clear, the attributes themselves pertain to the element, and not the tag, since a tag is simply an element declaration if you will (for lack of a better term). However, the attr=value notation is indeed part of the start tag.

For the purposes of an introductory workshop, the distinction in attributes is not so important. What's more important (and I mean very important) is the distinction between elements and tags.

Upvotes: 0

Related Questions