Open In App

HTML Attributes

Last Updated : 05 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

HTML attributes play an important role in defining the attributes and behavior of HTML elements. These attributes act as modifiers, providing additional information that empowers how elements are displayed and interpreted by browsers. By effectively utilizing attributes, you can make good-looking and interactive web pages that deliver exceptional user experiences. They provide essential meta-information related to the tags, influencing how content is displayed and interacted with.

Components of Attribute

An HTML attribute consists of two primary components:

  1. Attribute Name: This serves as a unique identifier that specifies the type of information being provided. Common examples include id, class, src, href, and alt.
  2. Attribute Value: This component offers the actual data associated with the attribute name. Values are enclosed within single or double quotes, depending on the attribute and whether the value itself contains quotes.

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:

  • charset: Specifies the character encoding for the HTML document.
  • name: Defines the name of the metadata attribute.
  • content: Provides information associated with the specified name.
  • http-equiv: Sets an HTTP header for the content.
  • scheme: Specifies the format used to interpret the content value.

HTML Global Attributes

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

  • class: Groups elements and allows styling.
  • style: Inline CSS styles.
  • src: This attribute 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: This attribute assigns a unique identifier to an element, allowing you to target it with CSS or JavaScript for styling and interactivity.
  • class: This attribute enables you to assign one or more CSS classes to an element, facilitating the application of styles.
  • href: This attribute defines the hyperlink destination within the a element (anchor tag), allowing users to navigate to another web page or section of the current page.
  • alt: This attribute provides alternative text for images, essential for accessibility and SEO. Search engines use this text to understand the image’s content, and it’s displayed when images fail to load.
  • style: This attribute allows you to embed inline CSS styles directly within the opening tag of an element.
  • title: This attribute creates a tooltip that appears when a user hovers over the element.
  • lang: This attribute specifies the language of the element’s content, aiding with translation and accessibility.

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=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/Geek_logi_-low_res.png">
</body>

</html>

Output:

HTML src Attribute Example Output

HTML alt Attribute

This is an alt Attribute 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>

    <!--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 alt Attribute 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 width & height Attribute Example Output

HTML id Attribute

The id attribute is used to provide a unique identification of an element. Situations may arise when we need to access a particular element that may have a similar name as the others. In that case, we provide different IDs 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 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 id Attribute 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
<html> 
<head> 
    <title>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 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
<!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

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:

style Attribute

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:

Screenshot-2023-12-21-154424

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads