The Wayback Machine - https://web.archive.org/web/20240930160653/https://www.geeksforgeeks.org/html-attributes/
Open In App

HTML Attributes

Last Updated : 06 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

HTML attributes provide additional information about elements within an HTML document. Every HTML element can have attributes. Attributes are always defined in the start tag. They are specified using a name/value pair format, where the attribute name defines the property, and its value provides specific details, like name=”value”. These attributes impact content display and interaction on web pages.

Let’s explore the key aspects of HTML attributes:

Components of Attribute

An HTML attribute consists of two primary components:

Attribute NameDescription
idA unique identifier for an HTML element, used for styling or JavaScript interaction.
classDefines one or more class names for an element, used for styling and applying CSS rules.
srcSpecifies the source URL for external resources like images, audio, or video.
hrefSpecifies the URL of the linked resource, typically used in anchor (a) elements for hyperlinks.
altProvides alternative text for images, displayed if the image cannot be loaded or is unavailable.
any_custom_attrDescribes any custom attribute name and its purpose in the HTML document.

Syntax:

<element attribute_name= "attribute_value">

Meta Tag Attributes

Meta tags provide essential information about HTML documents. They are self-closing tags and significantly impact browser functionality, search engine optimization (SEO), character set declaration, and viewport control. Here are some commonly used meta tag attributes:

AttributeDescription
charsetDefines the character encoding for the HTML document.
nameSpecifies the name of the metadata attribute.
contentProvides information associated with the specified name.
http-equivSets an HTTP header for the content, typically used for backward compatibility.
schemeSpecifies the format used to interpret the content value, often for data formats.

HTML Global Attributes

Global attributes apply to all types of HTML tags. Some commonly used global attributes include:

AttributeDescription
classGroups elements and allows styling.
styleInline CSS styles.
srcSpecifies 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.
contenteditableDetermines whether the content within the element is editable.
roleSpecifies the element’s accessibility role.
tabindexDetermines the order of focus during keyboard navigation.
idAssigns a unique identifier to an element, allowing targeting with CSS or JavaScript.
hrefDefines the hyperlink destination within the a element, enabling navigation.
altProvides alternative text for images, essential for accessibility and SEO.
titleCreates a tooltip that appears when a user hovers over the element.
langSpecifies the language of the element’s content, aiding with translation and accessibility.

HTML src Attribute

The src attribute in HTML specifies the URL of a resource (such as an image, audio, or video) to be embedded or included in the webpage.

Example: This example explains the HTML src Attributes to specify the source address of the file.

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>

Output:

HTML-Attributes

HTML Attributes Example Output

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>

Output:

HTML-Attributes-1

HTML Attributes Example Output

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>

Output:

HTML-Attributes-2

HTML Attributes Example Output

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>

Output:

HTML-Attributes-Example

HTML Attributes Example Output

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>

Output:

HTML title attribute Example Output

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>

Output:

href attribute Example Output

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>

Output:

HTML-style-Attribute

HTML style Attribute Example Output

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>

Output:

HTML-lang-attribute

HTML lang attribute Example Output

Please refer to the HTML Attributes Complete Reference for all the attributes in detail.

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.

HTML Attributes – FAQs

What is an HTML attribute?

An HTML attribute provides additional information about an HTML element. Attributes are always included in the opening tag and usually come in name/value pairs like name=”value”.

What are some common HTML attributes?

Some common attributes include id, class, src, href, alt, title, style, and data-*.

What is the id attribute used for?

The id attribute is used to uniquely identify an HTML element. Example: <div id=”header”>.

What is the class attribute used for?

The class attribute is used to define one or more class names for an element, allowing CSS and JavaScript to select and manipulate groups of elements. Example: <p class=”intro”>.

How is the src attribute used?

The src attribute specifies the URL of an image, script, or other external resource. Example: <img src=”image.jpg” alt=”Description”>.



Previous Article
Next Article

Similar Reads

How to remove all attributes of an HTML element using jQuery ?
In this article, we will see how to remove all attributes of an HTML element using jQuery. To remove all attributes of elements, we use the removeAttributeNode() method and .removeAllAttributes(). Syntax: $.fn.removeAllAttributes = function() { return this.each(function() { $.each(this.attributes, function() { this.ownerElement.removeAttributeNode(
1 min read
HTML DOM attributes Property
The attributes property in HTML DOM returns the group of node attributes specified by NamedNodeMap objects. The NamedNodeMap object represents the collection of attribute objects and can be accessed by index number. The index number starts at 0. Syntax: node.attributes Return Value: It returns the NamedNodeMap object which is the collection of node
2 min read
Difference between 'hidden' and 'aria-hidden' attributes in HTML
The HyperText Markup Language (HTML) is a powerful web development tool combined with CSS and JavaScript. Apart from these, HTML also uses Accessible Rich Internet Application (ARIA) to make web content affable for a person with disabilities. Although ARIA is beneficial, there are keywords common to both HTML and ARIA, creating perplexity among ama
2 min read
Why are dashes preferred for CSS selectors / HTML attributes ?
CSS is a hyphen-delimited syntax language which means while writing HTML attributes we can use hyphen(-). Example: font-size, line-height, background-color, etc... There are various reasons to preferring hyphen(-): In CSS, one can use underscore(_) instead of hyphen(-) for CSS selectors(class, id, span, ...), but preference is given based on its ea
2 min read
What is the difference between properties and attributes in HTML?
HTML is the standard language for creating documents that are intended to be displayed on web browsers. It is very usual to get confused with attributes and properties while performing DOM object manipulation in JavaScript. Before learning about the differences, let us first define the Document Object Model. DOM: It is the acronym for the Document
4 min read
How to add attributes to an HTML element in jQuery?
Given an HTML document containing some elements and the task is to add the attributes to the HTML elements. Approach: Given an HTML document containing <label>, <input> and <button> elements. The <input> element contains the type and id attribute. When user click on the button, the jQuery function is called. We use $(selecto
1 min read
Placeholder vs Value Attributes in HTML
Placeholder Attribute:The placeholder attribute specifies a short hint that describes the expected value of an input field/textarea. The short hint is revealed in the field before the user enters a value. It is just a temporary hint and has nothing to do with logical executions in the backend. Syntax: <input placeholder = "text"> <textarea
3 min read
What are HTML tags & Attributes mandatory for a good Web page in SEO ?
Search engine optimization (SEO) is the process that involves enhancing the visibility of a website on search engines. This is achieved by increasing the website's ranking on search engine results pages, such as Google. The higher the website appears on these pages, the more likely it is to receive a larger volume of traffic. SEO involves understan
5 min read
Attributes that work differently between React and HTML
There is a minor difference between React(JSX) and HTML attributes. Like for example, the HTML attributes like class and for are replaced with className and htmlFor in React. There are a number of attributes that work differently between React and HTML. Grammar in JSX is mostly the same as in HTML, but there are subtle differences to watch out for.
3 min read
What is the purpose of the HTML "aria-*" attributes?
The HTML "aria-*" attributes, part of WAI-ARIA (Web Accessibility Initiative - Accessible Rich Internet Applications), are used to enhance the accessibility of web content. These attributes provide additional information to assistive technologies, such as screen readers, in interpreting and presenting content to users with disabilities. Syntax<b
1 min read
Difference Between Async & Defer Attributes in HTML
The async and defer attributes on a <script> tag in HTML are used to control the loading and execution behavior of external JavaScript files. While both of them have their own set of advantages and disadvantages, one should know which attribute is used at the right time. The difference between the async and defer attributes in HTML are as fol
5 min read
What are HTML Attributes ?
HTML attributes are the entities that provide the extra information about the tags. Attributes are specified using name and value pair. Some HTML tags are used without attributes while for some tags it's important to specify attributes along with them. In paired tags attributes are specified in the opening tag. The value of the attribute is specifi
3 min read
How to Create Optional HTML attributes with Razor View Engine ?
It's common practice in web development to dynamically create HTML components with optional attributes in response to specific criteria. When working with ASP.NET Core apps and the Razor view engine, this necessity is especially prevalent. With Razor, users can easily generate dynamic web content by integrating C# code with HTML markup in an easy a
3 min read
HTML Event Attributes Reference
Events are actions that happen in the browser when the user does something For example when users click on the mouse or type something on the keyboard. HTML Event Attributes: In HTML, we can use event actions on the browser, and with the help of the event, the user responds to the system. Event Attributes can be used with HTML elements to perform v
5 min read
How to use ARIA Attributes to Enhance Accessibility in HTML ?
Accessibility is a fundamental aspect of web development, ensuring that all users, regardless of ability, can access and interact with online content. HTML provides various features to improve accessibility, and one powerful tool is ARIA (Accessible Rich Internet Applications) attributes. ARIA attributes extend HTML semantics, providing additional
2 min read
How to Add and Remove HTML Attributes with jQuery?
Using jQuery, we can dynamically add and remove HTML attributes such as placeholder, style, and more to enhance the interactivity and functionality of web elements based on user actions. We will explore two different approaches to adding and removing HTML attributes with jQuery. Below are the possible approaches to add and remove HTML attributes wi
2 min read
HTML Global Attributes
HTML Global Attributes are attributes that can be used with any HTML element. They include id, class, style, title, lang, tabindex, and others, providing common functionality and enhancing element behavior and presentation across web pages. List of Global Attributes: Global Attributes Description Example accesskeyIt is the keyboard shortcuts to act
3 min read
Tags vs Elements vs Attributes in HTML
In HTML, tags represent the structural components of a document, such as <h1> for headings. Elements are formed by tags and encompass both the opening and closing tags along with the content. Attributes provide additional information or properties to elements, enhancing their functionality or appearance. Table of Content HTML Tags: HTML eleme
2 min read
Difference between "blank" and "_blank" target attributes in HTML
If you have ever noticed on the website that few links got opened either in a new tab or in a new window or sometimes, it might also happen that in some websites, click on the link, for the first time, it will open in a new tab & that particular open tab will update & reused every time whenever we click to the link. Both these scenarios can
3 min read
HTML data-* Attributes
The HTML data-* attribute allows storing custom data for elements, enhancing interactivity in web applications through JavaScript without server-side involvement or AJAX calls. Syntax <li data-book-author="Rabindra Nath Tagore"> Gitanjali </li>The data-* attributes can be used to define our own custom data attributes. It stores custom d
3 min read
HTML Attributes Complete Reference
HTML attributes are special words placed inside the opening tags of HTML elements to define their characteristics. Each attribute consists of two parts: the attribute name and its value, separated by an equal sign (=) and enclosed in double quotes (" "). Syntax of HTML Attribute<tag_name attribute_name="value"> Contents... </tag_name>Ov
7 min read
Difference between id and class Attributes in HTML
The id and class attributes in HTML are used for element identification and styling. The id attribute uniquely identifies a single element, while the class attribute can be applied to multiple elements. Both are essential for CSS styling and JavaScript manipulation. HTML id AttributeThe id attribute is a unique identifier that is used to specify th
3 min read
jQuery | Set Content and Attributes
Set Content: It is used to set the content using jQuery. The following methods are used to set the content which are listed below: text(): It is used to set or return the text content of selected elements. html(): It is used to set or return the innerHTML content of the selected element. val(): This parameter is used to set or return the value of a
2 min read
jQuery | Get Content and Attributes
Get Content: In order to get the content of DOM object, there are three simple methods.jQuery methods for DOM manipulation which are listed below: text(): It is used to set or return the text content of selected elements. html(): It is used to set or return the content of selected elements including HTML markup. val(): It is used to set or return t
2 min read
How to conditionally add attributes to React components?
We can conditionally add attributes to React components with the following approaches: Approach 1: Evidently, with some attributes, React is smart enough to omit the attribute if the value you pass to it is not truthy. For example: state= { disabled: false, required: true } return ( <input type="text" disabled={disabled} required={required} /
2 min read
XML | Attributes
Prerequisite: XML | BasicsThe XML attribute is a part of an XML element. The addition of attribute in XML element gives more precise properties of the element i.e, it enhances the properties of the XML element.Syntax: <element_name attribute1 attribute2 ... > Contents... </element_name> In the above syntax element_name is the name of an
3 min read
Blaze UI Divider Attributes
Blaze UI is a user interface toolkit that provides a strong foundation and many components to building maintainable and scalable websites efficiently. All the features of Blaze UI rely on native browser features so it does not require an additional framework or library to work. In this article, we will be seeing Blaze UI Divider Attributes. The Div
3 min read
PHP | SimpleXMLElement attributes() Function
Pre-requisite: Read XML BasicsThe SimpleXMLElement::attributes() function is an inbuilt function in PHP which is used to retrieve the attributes and its value from an XML tag in a SimpleXML object. Syntax: SimpleXMLElement SimpleXMLElement::attributes( $namespace, $is_prefix ) Parameter: This function accepts two parameters as mentioned above and d
2 min read
Copy all the attributes of one element and apply them to another with JavaScript
The task is to copy all attributes of one element and apply them to another with the help of JavaScript. Here 2 approaches are discussed. Approach 1: Select both elements(target and source element).Get the properties of source element by using el.prop('attributes') method.Use .each() method on each object property and set that property to the targe
2 min read
What are these attributes `aria-labelledby` and `aria-hidden` ?
The term, ARIA with attributes such as aria-labelledby and aria-hidden stands for Accessible Rich Internet Applications. These are the set of standards and guidelines that makes web applications more accessible to people with disabilities. This is used in the interactive content in an HTML document such as error messages, progress bars, progressive
3 min read