TExcelTAD
Home › HTML › HTML Attributes

HTML Attributes

Elements describe what content is; attributes add extra details. They answer questions like "where does this link go?", "which picture should be shown?" or "what language is this text in?".

Attribute syntax

Attributes always live in the opening tag and are written as name="value" pairs:

<a href="https://example.com" title="Visit example">Example</a>
   ^^^^ ^^^^^^^^^^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^
   name       value           name      value
  • An element can have several attributes, separated by spaces.
  • Order doesn't matter.
  • Attribute names are case-insensitive, but lowercase is the convention.

Common attributes

Attribute Used on Purpose
href <a>, <link> Destination address
src <img>, <script>, <iframe>… Source file to load
alt <img> Text alternative for an image
width, height <img>, <canvas>, <video>… Size in pixels
style any element Inline CSS
title any element Extra info shown as a tooltip
lang any element (usually <html>) Language of the content
id any element A unique name for one element
class any element One or more group names for styling

Attributes that work on every element (id, class, style, title, lang, hidden…) are called global attributes.

href and src

html · try it yourself
<p><a href="https://developer.mozilla.org">Open MDN Web Docs</a></p>
<img width="120" height="60" alt="An orange rounded rectangle"
     src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='60'%3E%3Crect width='120' height='60' rx='12' fill='%23e34c26'/%3E%3C/svg%3E">

href tells the link where to go; src tells the image which file to display. Without them, neither element does anything useful.

alt, width and height

The alt text is shown if the image cannot load and is read aloud by screen readers. Setting width and height lets the browser reserve space before the image arrives, which prevents the page from jumping around.

html · try it yourself
<img src="missing-picture.png" alt="Photo of a sleepy cat on a sofa" width="200" height="120">
<p>The image file above doesn't exist, so the browser shows the alt text instead.</p>

style, title and lang

html · try it yourself
<p style="color: navy; font-size: 20px;">This paragraph is styled inline.</p>
<p title="I appear when you hover">Hover over this paragraph to see its title.</p>
<p lang="fr">Bonjour tout le monde !</p>

Boolean attributes

Some attributes are either present or absent — their value doesn't matter. Writing just the name switches the feature on.

html · try it yourself
<input type="checkbox" checked> Subscribed<br>
<input type="text" value="Can't edit me" disabled><br>
<p hidden>You will never see this paragraph.</p>
<button type="button" disabled>Not clickable</button>

checked, disabled, hidden, required and readonly are all boolean attributes. Writing disabled="false" does not turn the feature off — the attribute is still present. To turn it off, remove it.

Quotes: always use them

HTML lets you omit quotes around simple values (<p lang=en>), but values with spaces break without them:

<p title=Hello world>   → title is just "Hello"; "world" becomes a separate, unknown attribute
<p title="Hello world"> → title is "Hello world"

Double quotes are most common. If the value itself contains double quotes, wrap it in single quotes instead: title='She said "hi"'.

Tip: Always quote attribute values and write attribute names in lowercase. It avoids bugs and keeps your code consistent with most style guides.

Test yourself

Where are attributes written?

  1. In the closing tag
  2. In the opening tag
  3. Between the tags as content
  4. In a separate file

Which attribute provides a text alternative for an image?

  1. title
  2. src
  3. alt
  4. desc

How do you turn a boolean attribute like disabled off?

  1. Set it to "false"
  2. Set it to "0"
  3. Remove the attribute entirely
  4. Write disabled="off"