Back to HTML

HTML Tags, Elements and Attributes Explained

HTML Tags, Elements and Attributes Explained
Coding Tutorials Team 1 August 2026

Three words get used as though they mean the same thing, and they do not. A tag is a piece of syntax. An element is the thing that syntax creates. An attribute is extra information attached to an element. Keeping them apart makes documentation, validator output and browser errors far easier to read, because all three are written by people who use the terms precisely.

A tag is the markup you type

A tag is the angle brackets and the name inside them. There are opening tags and closing tags, and the closing one carries a forward slash before the name.

<p>      an opening tag </p>     a closing tag

Tag names are not case sensitive, but lowercase is the universal convention and mixing cases in one document looks careless. The name has to follow the opening bracket immediately, with no space, or the parser will not recognise it as a tag at all and will print it as text.

An element is the tag pair and everything between it

The element is the complete unit: opening tag, content, closing tag. This is the thing that becomes a node in the document tree, the thing CSS selects and the thing JavaScript returns when you query the page.

<p>This whole line is one paragraph element.</p>

The distinction matters as soon as you nest things. When documentation says an element must not contain another element, it is talking about content, not about the tags in isolation. A paragraph cannot contain a heading, and if you write one anyway the parser closes your paragraph early and the resulting structure is not what your file looks like.

Some elements have no closing tag

A handful of elements cannot hold content, so a closing tag would be meaningless. These are called void elements, and the common ones are the image, the line break, the horizontal rule, the input and the meta and link elements used in the head.

<img src="photo.jpg" alt="A grey cat asleep"> <br> <hr>

You may see a trailing slash written before the closing bracket. In HTML it is ignored entirely and is a habit carried over from XHTML. It is harmless, so match whatever style the codebase you are working in already uses, and do not attempt to add closing tags to these elements because there is no valid place for the content that would imply.

Attributes go in the opening tag only

An attribute configures an element. It is written as a name, an equals sign, and a value in quotes, and it belongs in the opening tag. Never in the closing one.

<a href="/contact/" class="button">Contact us</a>

Multiple attributes are separated by spaces and their order carries no meaning. Attribute names are case insensitive, values sometimes are not, and a file path is a good example of a value where case can matter depending on the server.

The rules that actually cause breakage are these. Put no spaces around the equals sign. Use straight quotes, not the curly ones a word processor produces, because curly quotes are treated as part of the value. Quote every value even when the syntax technically permits omitting them, since an unquoted value ending at the first space silently truncates any path or sentence containing one.

Boolean attributes have no value

Some attributes are switches. Their presence turns something on and their absence turns it off, and the value is irrelevant.

<input type="checkbox" checked> <button disabled>Send</button>

The trap is assuming you can switch one off by setting it to false. You cannot. Writing disabled with a value of false still disables the button, because the browser only checks whether the attribute exists. Removing it is the only way to turn it off, which is exactly what a JavaScript framework is doing when it appears to toggle one.

Global attributes work everywhere

Most attributes belong to specific elements. The source attribute means nothing on a paragraph. A small set called global attributes may be used on any element, and the ones you will reach for constantly are id, class, title, lang, style and the custom data attributes.

Of these, id must be unique across the entire document. Duplicating one is invalid, breaks fragment links, and causes JavaScript lookups to return only the first match, which produces bugs that look random until you find the duplicate.

Getting the vocabulary right pays off

None of this is trivia. When a validator reports an unexpected end tag, or a specification says an element accepts only phrasing content, the message is precise and it is telling you exactly where to look. Learning to read it saves far more time than it costs.