HTML Attributes are special words used within the opening tag of an HTML element. They provide additional information about HTML elements. HTML attributes are used to configure and adjust the element’s behavior, appearance, or functionality in a variety of ways.
Each attribute has a name and a value, formatted as name=”value”. Attributes tell the browser how to render the element or how it should behave during user interactions.
Syntax:
<tagname attribute_name = “attribute_value”> content… </tagname>
Code Example of Using HTML Attributes
This HTML code demonstrates the use of the src
attribute within the <img>
tag to display an image.
HTML
<!DOCTYPE html>
<html>
<head>
<title>HTML img src Attribute</title>
</head>
<body>
<img src=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/Geek_logi_-low_res.png">
</body>
</html>
In this example:
- Tag :
<img>
- Attribute :
src
- Value of Attribute :
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/Geek_logi_-low_res.png"
- Purpose : The
<img>
tag is used for embedding images in an HTML page. The src
attribute within the <img>
tag specifies the path to the image file you wish to display. This attribute is crucial as it directs the browser to the image’s location on the internet or a local directory.
Components of Attribute
An HTML attribute consists of two primary components:
1. attribute_name: This is the name of the attribute, which specifies what kind of additional information or property you are defining for the element. Common attribute names include href
, src
, class
, id
, etc.
2. attribute_value: The value is assigned to the attribute to define the specific setting or behavior. It is always placed in quotes.
Types of HTML Attributes
HTML attributes can be broadly categorized based on their function and the type of elements they modify. For example –
Global Attributes
These attributes can be used with any HTML element (though their effects might vary based on the element):
Attribute | Description |
---|
class | Groups elements and allows styling. |
style | Inline CSS styles. |
src | Specifies the source of various resources, such as image URLs for the img element, video URLs for the video element, and audio URLs for the audio element. |
contenteditable | Determines whether the content within the element is editable. |
role | Specifies the element’s accessibility role. |
tabindex | Determines the order of focus during keyboard navigation. |
id | Assigns a unique identifier to an element, allowing targeting with CSS or JavaScript. |
href | Defines the hyperlink destination within the a element, enabling navigation. |
alt | Provides alternative text for images, essential for accessibility and SEO. |
title | Creates a tooltip that appears when a user hovers over the element. |
lang | Specifies the language of the element’s content, aiding with translation and accessibility. |
Some other main types of HTML attributes are:
- Event Attributes – These define the actions to be taken on specific browser events.
- Input Attributes – Specific to input elements within
<form>
tags. - Image Attributes – Specific to the
<img>
element for handling images. - Link Attributes – Specific to linking elements like
<a>
and <link>
. - Table Attributes – Used with table elements like
<table>
, <th>
, <tr>
, and <td>
. - Style Attributes – Define styles directly on an element.
- Media Attributes – Related to media elements like
<audio>
and <video>
. - Accessibility Attributes – Help improve accessibility, such as
alt
for images and aria-*
attributes. - Meta Attributes – Used with meta elements to specify metadata like
charset
.
Common HTML Attributes
Let’s take look at some of the most commonly used HTML attributes:
1. HTML alt Attribute
The alt attribute in HTML provides alternative text for an image if the image cannot be displayed. It improves accessibility and provides context for screen readers.
Example: This example explains the HTML alt Attributes to specify the name of the file when the image is not loaded properly.
HTML
<!DOCTYPE html>
<html>
<head>
<title>HTML img alt Attribute</title>
</head>
<body>
<!--If the image is not found or the img field
is left blank the alt value gets displayed-->
<img src=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/Geek_logi_-low_res.png"
alt="The Logo"><br>
<img src=""
alt="Since the src value is blank,the alt value is displayed">
</body>
</html>
2. HTML width and height Attribute
The width and height Attribute is used to adjust the width and height of an image(in pixels).
Example: This example explains the HTML width & height Attributes to specify the different sizes of the images.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Width and Height</title>
</head>
<body>
<img src=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/Geek_logi_-low_res.png"
width="300px"
height="100px">
</body>
</html>
3. HTML id Attribute
The id attribute in HTML assigns a unique identifier to an element, allowing it to be targeted by CSS and JavaScript for styling and manipulation purposes.
Example: This example explains the HTML id Attribute to specify the unique value for the specific element.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
#geeks {
color: green;
}
</style>
</head>
<body>
<h1 id="geeks">Welcome to GeeksforGeeks</h1>
</body>
</html>
4. HTML title Attribute
The title attribute is used to explain an element by hovering the mouse over it. The behavior differs with various elements but generally, the value is displayed while loading or hovering the mouse pointer over it.
Example: This example explains the HTML title Attributes to specify the metadata for the element by hovering the mouse over it.
HTML
<!DOCTYPE html>
<html>
<head>
<title>HTML title Attribute</title>
</head>
<body>
<h3 title="Hello GeeksforGeeks">
Hover to see the effect
</h3>
</body>
</html>
5. HTML href Attribute
The href attribute in HTML, used with the <a> tag, specifies a link destination. Clicking the linked text navigates to this address. Adding `target=”_blank”` opens it in a new tab.
Example: This example explains the HTML href Attributes specify the link address of the file.
HTML
<!DOCTYPE html>
<html>
<head>
<title>link Attribute</title>
</head>
<body>
<a href="https://www.geeksforgeeks.org/">
Click to open in the same tab
</a><br>
<a href="https://www.geeksforgeeks.org/"
target="_blank">
Click to open in a different tab
</a>
</body>
</html>
6. HTML style Attribute
The style attribute is used to provide various CSS effects to the HTML elements such as increasing font-size, changing font-family, coloring, etc.
Example: This example explains the HTML style Attributes to specify the style properties for the HTML element.
HTML
<!DOCTYPE html>
<html>
<head>
<title>style Attribute</title>
</head>
<body>
<h2 style="font-family:Chaparral Pro Light;">
Hello GeeksforGeeks.
</h2>
<h3 style="font-size:20px;">
Hello GeeksforGeeks.
</h3>
<h2 style="color:#8CCEF9;">
Hello GeeksforGeeks.
</h2>
<h2 style="text-align:center;">
Hello GeeksforGeeks.
</h2>
</body>
</html>
7. HTML lang attribute
The language is declared with the lang attribute. Declaring a language can be important for accessibility applications and search engines.
Example: This example explains the HTML lang Attributes that specify the language of the HTML page.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>lang attribute</title>
<style>
body {
text-align: center;
}
h1 {
color: green;
}
.lang-info {
font-style: italic;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>lang attribute</h2>
<p lang="en">
A computer science portal for geeks
</p>
<p lang="fr" class="lang-info">
A computer science portal for geeks
</p>
<p lang="es" class="lang-info">
A computer science portal for geeks
</p>
</body>
</html>
Important Points About HTML Attributes
1. Always Use Lowercase Attributes:
You can use either uppercase or lowercase letters for defining attributes. For example, both alt
and ALT
in an <img>
tag are valid. However, it is recommended to use lowercase attributes as per W3C guidelines for consistency and better readability.
2. Always Quote Attribute Values:
The HTML standard does not require quotes around attribute values in certain situations. However, W3C recommends always using quotes for attribute values, and quotes are mandatory for stricter document types like XHTML. Using quotes helps avoid errors, especially when the attribute value contains spaces or special characters.
3. Declare Quote as an Attribute Value:
You can use either single ('
) or double ("
) quotes for attribute values in HTML, but it is essential to be consistent throughout your document. If the attribute value contains a double quote, then use single quotes to enclose it, and vice versa.For simplicity, it is a good practice to consistently use double quotes, as it aligns with the convention used in many HTML examples and tutorials.
Example:
<input type="text" placeholder='Enter your "username" here'>
In this example, the attribute value itself contains double quotes ("username"
), so the entire value is enclosed within single quotes to avoid confusion.
4. Boolean Attributes Should Be Written Without Values:
Boolean attributes do not require a value. If the attribute is present, it is considered true
. For example, the checked
attribute of an <input>
element is correctly written as:
Example:
<input type="checkbox" checked>
Writing checked="checked"
also works, but it is redundant. Simply including the attribute is enough to represent a true
state.
5. Proper Attribute Order for Readability:
Although HTML does not enforce an order for attributes, following a consistent order improves readability and maintainability. It is common practice to order attributes like this: id
, class
, other global attributes, specific attributes, and finally, event attributes.
Example:
<button id="btn1" class="button-class" type="submit" onclick="handleClick()">Submit</button>
6. Avoid Deprecated Attributes:
Certain HTML attributes, such as align
, bgcolor
, and border
, are considered deprecated. It is better to use CSS for styling instead of outdated attributes.
Example (Avoid deprecated attribute):
<p style="text-align: center;">This text is centered.</p>
Instead of using the align
attribute, use the style
attribute or a CSS class to achieve the same effect
Summing up!
Understanding HTML attributes is essential for creating well-structured and accessible web pages. By using these attributes effectively, you can enhance user experience and optimize your content for search engines.
Please refer to the HTML Attributes Complete Reference for all the attributes in detail.
Similar Reads
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
HTML Introduction
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It defines the structure of a webpage by using a series of elements, tags, and attributes to organize text, images, links, and other multimedia elements. HTML is a markup language,
7 min read
HTML Editors
An HTML Editor is a software application designed to help users create and modify HTML code. It often includes features like syntax highlighting, tag completion, and error detection, which facilitate the coding process. There are two main types of HTML editors: Text-Based Editors - Allow direct codi
5 min read
HTML Basics
HTML (HyperText Markup Language) is the standard markup language for creating and structuring web pages. It defines the structure of a webpage using elements and tags.HTML is responsible for displaying text, images, and other content.It serves as the foundation for building websites and web applicat
6 min read
HTML Comments
HTML comments are used to add notes or explanations in the HTML code that are not displayed by the browser. They are useful for documenting the code, making it easier to understand and maintain.To add a comment, use the syntax <!-- your comment here -->. [GFGTABS] HTML <!-- This is a commen
4 min read
HTML Elements
An HTML Element consists of a start tag, content, and an end tag, which together define the element's structure and functionality. Elements are the basic building blocks of a webpage and can represent different types of content, such as text, links, images, or headings. For example, the <p> el
5 min read
HTML Attributes
HTML Attributes are special words used within the opening tag of an HTML element. They provide additional information about HTML elements. HTML attributes are used to configure and adjust the element's behavior, appearance, or functionality in a variety of ways. Each attribute has a name and a value
9 min read
HTML Headings
HTML headings are used to define the titles and subtitles of sections on a webpage. They help organize the content and create a structure that is easy to navigate. Proper use of headings enhances readability by organizing content into clear sections.Search engines utilize headings to understand page
4 min read
HTML Paragraphs
A paragraph in HTML is simply a block of text enclosed within the <p> tag. The <p> tag helps divide content into manageable, readable sections. Itâs the go-to element for wrapping text in a web page that is meant to be displayed as a distinct paragraph. Syntax: <p> Content</p
5 min read
HTML Text Formatting
HTML text formatting refers to the use of specific HTML tags to modify the appearance and structure of text on a webpage. It allows you to style text in different ways, such as making it bold, italic, underlined, highlighted, or struck-through. Table of Content Categories of HTML Text FormattingLogi
4 min read