HTML Attributes

HTML attributes provide additional information about HTML elements. Attributes are always specified in the start tag and usually come in name/value pairs like: name="value"

All HTML elements can have attributes. Attributes provide extra information that is not part of the content.

Attribute Syntax

Attributes are written inside the opening tag, after the tag name:

  • Attributes are always specified in the start tag
  • Attributes usually come in name/value pairs like: name="value"
  • Attribute values should always be enclosed in quotes
  • Multiple attributes are separated by spaces

Common HTML Attributes

The href Attribute

The <a> tag defines a hyperlink. The href attribute specifies the URL of the page the link goes to:

The src Attribute

The <img> tag is used to embed an image in an HTML page. The src attribute specifies the path to the image to be displayed:

The alt Attribute

The required alt attribute for the <img> tag specifies an alternate text for an image, if the image cannot be displayed. This is important for accessibility and SEO.

The width and height Attributes

The <img> tag should also contain the width and height attributes, which specify the width and height of the image (in pixels):

The style Attribute

The style attribute is used to add styles to an element, such as color, font, size, and more:

The title Attribute

The title attribute defines extra information about an element. The value of the title attribute will be displayed as a tooltip when you mouse over the element:

The id Attribute

The id attribute specifies a unique id for an HTML element. The value of the id attribute must be unique within the HTML document. The id attribute is used to point to a specific style declaration in a style sheet or to manipulate the element with JavaScript.

  • The id must be unique - no two elements can have the same id
  • The id is case-sensitive
  • The id must contain at least one character
  • The id must not contain whitespace

The class Attribute

The class attribute is used to specify a class for an HTML element. Multiple HTML elements can share the same class. The class attribute is often used to point to a class name in a style sheet.

An element can have multiple classes, separated by spaces:

Difference Between id and class

id Attributeclass Attribute
Must be uniqueCan be used multiple times
One element per idMultiple elements can share
Used for specific elementUsed for groups of elements
Higher CSS specificityLower CSS specificity

The lang Attribute

You should always include the lang attribute inside the <html> tag, to declare the language of the Web page. This is meant to assist search engines and browsers.

Country codes can also be added to the language code:

Quote Attribute Values

The HTML standard does not require quotes around attribute values. However, W3C recommends quotes in HTML, and demands quotes for stricter document types like XHTML.

GoodBadWon't Work
href="page.html"href=page.htmlhref=my page.html
class="main"class=mainclass=main content
title="Hello"title=Hellotitle=Hello World

Always use quotes around attribute values! It's a best practice and prevents errors.

Single or Double Quotes?

Double quotes around attribute values are the most common in HTML, but single quotes can also be used. In some situations, when the attribute value itself contains double quotes, it is necessary to use single quotes:

Summary of Common Attributes

AttributeUsed WithPurpose
href&lt;a&gt;Specifies the URL of a link
src&lt;img&gt;, &lt;script&gt;Specifies the source file
alt&lt;img&gt;Alternative text for images
width, height&lt;img&gt;Specifies dimensions
styleAll elementsInline CSS styles
titleAll elementsTooltip text
idAll elementsUnique identifier
classAll elementsClass name(s)
lang&lt;html&gt;Language of content
  • Always use quotes around attribute values
  • Use lowercase for attribute names
  • The id must be unique on the page
  • Multiple classes can be applied to one element
  • The alt attribute is required for images
  • Use meaningful attribute values

Attributes make HTML elements more powerful and flexible. Understanding attributes is essential for creating dynamic and interactive web pages.

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Easy

Which of the following is a best practice in programming?

10 XP~2 min
Exercise 2 of 2Easy

Code that is easy to read and understand is called ___ code.

10 XP~2 min