Introduction
In this chapter, you will learn about HTML attributes, which provide additional information about HTML elements. Attributes are used to define the properties and behavior of elements, making them more functional and dynamic.
What are HTML Attributes?
HTML attributes are special words used inside the opening tag of an element to control the element’s behavior or provide additional information. Attributes are always specified in the start tag and usually come in name/value pairs like name="value"
.
Commonly Used HTML Attributes
id
- Description: Uniquely identifies an element within a document.
- Example:
<p id="intro">This is an introductory paragraph.</p>
class
- Description: Specifies one or more class names for an element, allowing for CSS and JavaScript manipulations.
- Example:
<p class="highlight">This paragraph is highlighted.</p>
style
- Description: Used to apply inline CSS styles to an element.
- Example:
<p style="color: red; font-size: 20px;">This is a styled paragraph.</p>
href
- Description: Specifies the URL of a link.
- Example:
<a href="https://www.example.com">Visit Example</a>
src
- Description: Specifies the source URL of an image.
- Example:
<img src="image.jpg" alt="An example image">
alt
- Description: Provides alternative text for an image, used by screen readers and displayed if the image fails to load.
- Example:
<img src="image.jpg" alt="An example image">
title
- Description: Provides additional information about an element, often displayed as a tooltip when the mouse hovers over the element.
- Example:
<p title="This is a tooltip">Hover over this paragraph to see the tooltip.</p>
target
- Description: Specifies where to open the linked document.
- Example:
<a href="https://www.example.com" target="_blank">Open in new tab</a>
rel
- Description: Specifies the relationship between the current document and the linked document.
- Example:
<a href="https://www.example.com" rel="noopener noreferrer">Visit Example</a>
Attribute Values
- Boolean Attributes: Attributes like
disabled
,checked
, andreadonly
are boolean attributes. Their presence alone is enough to apply the effect.<input type="checkbox" checked>
- Empty Values: Some attributes can have empty values, which are still valid.
<input type="text" value="">
Global Attributes
Global attributes can be used with any HTML element. Some common global attributes include:
accesskey
: Specifies a shortcut key to activate/focus an element.contenteditable
: Indicates whether the content of an element is editable.data-*
: Used to store custom data private to the page or application.draggable
: Specifies whether an element is draggable.hidden
: Hides the element.tabindex
: Specifies the tab order of an element.
Example Using Multiple Attributes
<a href="https://www.example.com" target="_blank" class="btn" title="Visit Example" rel="noopener noreferrer">Visit Example</a>
href="https://www.example.com"
: Specifies the URL of the link.target="_blank"
: Opens the link in a new tab.class="btn"
: Assigns thebtn
class to the link.title="Visit Example"
: Provides a tooltip.rel="noopener noreferrer"
: Specifies the relationship between the current document and the linked document.
Conclusion
HTML attributes are powerful tools that provide additional information and control over HTML elements. By using attributes effectively, you can create more interactive and functional web pages. Understanding attributes is essential for anyone looking to build and enhance web applications.