View Categories

HTML Attributes

HTML attributes provide additional information about elements within an HTML document. Every HTML element can have attributes, which are always defined in the start tag. They are specified using a name/value pair format, such as name=”value”. These attributes affect how content is displayed and how users interact with elements on web pages.

Key Aspects of HTML Attributes #

Components of an 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 a linked resource, typically used in anchor elements for hyperlinks.
altProvides alternative text for images, displayed if the image cannot be loaded.
any_custom_attrRepresents any custom attribute name and its purpose within the HTML document.
Attribute Name in HTML

Syntax:

<element attribute_name="attribute_value">

Meta Tag Attributes #

Meta tags provide essential information about HTML documents. They are self-closing tags and are critical for browser functionality, search engine optimisation (SEO), character set declaration, and viewport control. Some common meta tag attributes include:

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

HTML Global Attributes #

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

AttributeDescription
classGroups elements together for styling.
styleApplies inline CSS styles.
srcSpecifies the source of resources, such as images or media files.
contenteditableAllows the content within an element to be editable.
roleDefines an element’s accessibility role.
tabindexSets the order of focus during keyboard navigation.
idAssigns a unique identifier for targeting CSS or JavaScript.
hrefSpecifies the destination URL for hyperlinks.
altProvides alternative text for images, improving accessibility and SEO.
titleDisplays a tooltip when the user hovers over the element.
langSpecifies the language of the element’s content for translation and accessibility.
Attribute in HTML

Examples of Common HTML Attributes #

HTML src Attribute

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

<!DOCTYPE html>
<html>
<head>
    <title>HTML img src Attribute</title>
</head>
<body>
    <img src="https://example.com/image.png">
</body>
</html>

HTML alt Attribute

The alt attribute provides alternative text for images if the image cannot be displayed. It’s important for accessibility and screen readers.

<!DOCTYPE html>
<html>
<head>
    <title>HTML img alt Attribute</title>
</head>
<body>
    <img src="https://example.com/image.png" alt="An example image">
</body>
</html>

HTML width and height Attributes

These attributes adjust the width and height of an image (in pixels).

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Width and Height</title>
</head>
<body>
    <img src="https://example.com/image.png" width="300" height="100">
</body>
</html>

HTML id Attribute

The id attribute assigns a unique identifier to an element for use in CSS or JavaScript.

<!DOCTYPE html>
<html>
<head>
    <style>
        #example-id {
            color: green;
        }
    </style>
</head>
<body>
    <h1 id="example-id">Welcome to Our Website</h1>
</body>
</html>

HTML title Attribute

The title attribute displays a tooltip when the user hovers over the element.

<!DOCTYPE html>
<html>
<head>
    <title>HTML title Attribute</title>
</head>
<body>
    <h3 title="This is a tooltip!">Hover over me</h3>
</body>
</html>

HTML href Attribute

The href attribute specifies the destination URL for links. The target="_blank" attribute can be used to open the link in a new tab.

<!DOCTYPE html>
<html>
<head>
    <title>Link Example</title>
</head>
<body>
    <a href="https://www.example.com">Click to open</a><br>
    <a href="https://www.example.com" target="_blank">Click to open in a new tab</a>
</body>
</html>

HTML style Attribute

The style attribute is used to apply inline CSS to elements.

<!DOCTYPE html>
<html>
<head>
    <title>HTML style Attribute</title>
</head>
<body>
    <h2 style="color: blue;">This is blue text</h2>
</body>
</html>

HTML lang Attribute

The lang attribute specifies the language of the HTML document.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HTML lang Attribute</title>
</head>
<body>
    <p lang="en">This text is in English.</p>
    <p lang="fr">Ce texte est en français.</p>
</body>
</html>

Want to learn HTML with an instructor, either offline or online? Find experienced tutors near you and join Coaching Wallah—completely free for students!

Leave your comment