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 Attribute | class Attribute |
|---|---|
| Must be unique | Can be used multiple times |
| One element per id | Multiple elements can share |
| Used for specific element | Used for groups of elements |
| Higher CSS specificity | Lower 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.
| Good | Bad | Won't Work |
|---|---|---|
| href="page.html" | href=page.html | href=my page.html |
| class="main" | class=main | class=main content |
| title="Hello" | title=Hello | title=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
| Attribute | Used With | Purpose |
|---|---|---|
| href | <a> | Specifies the URL of a link |
| src | <img>, <script> | Specifies the source file |
| alt | <img> | Alternative text for images |
| width, height | <img> | Specifies dimensions |
| style | All elements | Inline CSS styles |
| title | All elements | Tooltip text |
| id | All elements | Unique identifier |
| class | All elements | Class name(s) |
| lang | <html> | 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