In this article, we will know HTML Attributes, and their implementation through examples. All HTML elements have attributes that will provide additional information about that particular element. It takes 2 parameters, ie, a name & a value which define the properties of the element and are placed inside the element tag.
Points to remember for attributes:
- Attributes always come in name/value pairs like this: attribute_name=”value”.
- Attributes are always added to the start tag of an HTML element.
- Attribute values should always be enclosed in quotes. Double style quotes (“ ”) are the most common, but single style quotes (‘ ’) are also allowed.
- In some rare situations, like when the attribute value itself contains quotes, it is necessary to use single quotes: name=’John “ShotGun” Nelson’ and vice-versa.
The name parameter takes the name of the property we would like to assign to the element and the value takes the property value or extent of the property names that can be aligned over the element. Every name has some value that must be written within quotes.
Syntax:
<element attribute_name="attribute_value">
Supported Attributes: It is a global attribute that is supported by all the tags.
Please refer to the HTML Attributes Complete Reference article for all the attributes in detail.
Below are some of the most commonly used Attributes in HTML.
HTML src Attribute: If we want to insert an image into a webpage, then we need to use the <img> tag and the src attribute. We will need to specify the address of the image as the attribute’s value inside the double quote.
Example: This example explains the HTML src Attributes to specify the source address of the file.
HTML
< html >
< head >
< title >src Attribute</ title >
</ head >
< body >
< img src =
</ body >
</ html >
|
Output:

src Attribute
HTML alt Attribute: This is an alternate tag that is used to show or display something if the primary attribute i.e., the <img> tag, fails to display the value assigned to it. This can also be used to describe the image to a developer who is actually sitting at the coding end.
Example: This example explains the HTML alt Attributes to specify the name of the file when the image is not loaded properly.
HTML
< html >
< head >
< title >alt Attribute</ title >
</ head >
< body >
< img src =
alt = "The Logo" >< br >
< img src = "" alt = "Since the src value is blank,the alt value is displayed" >
</ body >
</ html >
|
Output:

alt Attribute
HTML width and height Attribute: This attribute is used to adjust the width and height of an image.
Example: This example explains the HTML width & height Attributes to specify the different sizes of the images.
HTML
< html >
< head >
< title >Width and Height</ title >
</ head >
< body >
< img src =
width = "300px" height = "100px" >
</ body >
</ html >
|
Output:

width& height attribute
HTML id Attribute: This attribute is used to provide a unique identification to an element. Situations may arise when we will need to access a particular element that may have a similar name as the others. In that case, we provide different id’s to various elements so that they can be uniquely accessed. The properties extending the use of id are generally used in CSS, which we will be learning later.
Example: This example explains the HTML id Attributes 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:

id attribute
HTML title Attribute: The title attribute is used to explain an element on 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 on hovering the mouse over it.
HTML
< html >
< head >
< title >title Attribute</ title >
</ head >
< body >
< h3 title = "Hello GeeksforGeeks" >Hover to see the effect</ h3 >
</ body >
</ html >
|
Output:

title attribute
HTML href Attribute: This attribute is used to specify a link to any address. This attribute is used along with the <a> tag. The link put inside the href attribute gets linked to the text displayed inside the<a> tag. On clicking on the text we will be redirected to the link. By default, the link gets opened in the same tag but by using the target attribute and setting its value to “_blank”, we will be redirected to another tab or another window based on the browser’s configuration.
Example: This example explains the HTML href Attributes specify the link address of the file.
HTML
< html >
< head >
< title >link Attribute</ title >
</ head >
< body >
Click to open in the same tab
</ a >< br >
Click to open in a different tab
</ a >
</ body >
</ html >
|
Output:

href attribute
HTML style Attribute: This 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
< 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:

style Attribute
HTML lang attribute: The language is declared with the lang attribute. Declaring a language is 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-US" >
< body >
...
</ body >
</ html >
|
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Mar, 2023
Like Article
Save Article