In HTML, attributes provide additional information about an element. They are used to modify the behavior or appearance of an HTML element and are specified within the start tag of the element. Attributes consist of a name and a value, separated by an equals sign (=) and enclosed in double quotes.
Here’s an example of an HTML element with attributes:
<a href="https://phpgurukul.com/" target="_blank">Click here</a>
In this example, the
<a> element is an anchor element used for creating hyperlinks. It has two attributes:
href: This attribute specifies the destination URL that the link points to, in this case, “https://phpgurukul.com“. When the user clicks on the link, they will be directed to that URL.
target: This attribute specifies where the linked URL should open. In this case, the value “_blank” indicates that the URL should open in a new browser tab or window.
Attributes can be used with various HTML elements, and different elements support different attributes. For example, the
<img> element has attributes like
src (specifying the image URL) and
alt (providing alternative text for the image). Similarly, the
<input> element has attributes like
type (determining the input type) and
name (providing a name for the input field).
Here are some additional details about HTML attributes:
- Syntax: HTML attributes are added to HTML elements using the following syntax:
attributeName="attributeValue". The attribute name is typically written in lowercase, and the attribute value is enclosed in double-quotes.
- Common Attributes: HTML provides a set of common attributes that can be used with most elements. Some examples include:
class: Specifies one or more CSS classes to apply to an element, allowing for styling and grouping of elements.
style: Allows inline CSS declarations to be applied directly to an element, overriding external stylesheets.
title: Provides additional information about an element, typically displayed as a tooltip when hovering over the element.
disabled: Disables an input element, preventing user interaction.
- Global Attributes: In addition to the common attributes, HTML has a set of global attributes that can be used with any element. These attributes include:
accesskey: Specifies a keyboard shortcut to focus or activate an element.
data-*: Allows custom data attributes to be added to elements for storing additional information.
aria-*: Used for accessible web development to provide accessibility-related information to assistive technologies.
onchange: Fires when the value of an element has changed (often used with form elements).
onmouseover: Triggers when the mouse pointer moves over the element.
onsubmit: Fires when a form is submitted.
- Custom Attributes: HTML allows developers to create custom attributes for their own purposes. However, it’s important to note that custom attributes are not recognized by HTML validators or built-in browser functionality. To ensure compliance with HTML standards, it is recommended to prefix custom attributes with “data-“, such as