HTML Attributes are special words used within the opening tag of an HTML element. They provide additional information about the element. 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.
An HTML attribute consists of two primary components:
attribute_name: This specifies what kind of additional information or property you are defining for the element. Common attribute names include href, src, class, and id.attribute_value: The value is assigned to the attribute to define the specific setting or behavior. It is almost always placed in quotes.Syntax:
<tagname attribute_name="attribute_value"> content... </tagname>
Let's take a look at some of the most commonly used HTML attributes and how they modify elements.
The <img> tag is used for embedding images. The src attribute specifies the path to the image file.
<!DOCTYPE html>
<html>
<body>
<img src="/images/intricate.png" style="width:100px; height:auto;">
</body>
</html>
Provides alternative text for an image if it cannot be displayed. It is crucial for accessibility (screen readers) and SEO.
<!DOCTYPE html>
<html>
<body>
<img src="/images/missing-image.jpg" alt="The IntricateDevo Logo">
</body>
</html>
Adjusts the dimensions of an image. The values are typically provided in pixels.
<!DOCTYPE html>
<html>
<body>
<img src="/images/intricate.png" width="150" height="150">
</body>
</html>
Assigns a unique identifier to an element, allowing it to be targeted by CSS styling or JavaScript logic.
<!DOCTYPE html>
<html>
<head>
<style>
#intricate-heading {
color: #3B82F6;
font-family: 'Segoe UI', Verdana, sans-serif;
font-size: 36px;
font-weight: normal;
line-height: 1.2;
}
</style>
</head>
<body>
<h1 id="intricate-heading">Welcome to IntricateDevo</h1>
</body>
</html>
Used specifically with the <a> (anchor) tag, it specifies the destination URL of a link.
<!DOCTYPE html>
<html>
<body>
<a href="https://google.com/">Click to open in the same tab</a>
<br><br>
<a href="https://google.com/" target="_blank">Click to open in a new tab</a>
</body>
</html>
Used to provide various inline CSS styles directly to an HTML element (like color, font size, or alignment).
<!DOCTYPE html>
<html>
<body>
<h2 style="color: blue;">Hello IntricateDevo.</h2>
<p style="font-family: 'Segoe UI', Verdana, sans-serif; font-size: 18px; line-height: 1.5;">
This is larger text.
</p>
<p style="font-family: 'Segoe UI', Verdana, sans-serif; font-size: 17px; line-height: 1.5; text-align: center;">
This text is centered.
</p>
</body>
</html>
HTML attributes can be broadly categorized based on their function. Global Attributes can be used with almost any HTML 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. |
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. |
onclick, onmouseover).<form> tags (e.g., type="text", placeholder).<img> element for handling images.<a> and <link>.<table>, <th>, <tr>, and <td>.<audio> and <video>.alt for images and aria-* attributes.charset.<img ALT="Logo">), but W3C guidelines strongly recommend using lowercase attributes for consistency.class="container") is mandatory for strict document types and prevents errors when values contain spaces.<input type="text" placeholder='Enter your "username" here'>true.<input type="checkbox" checked>align, bgcolor, and border are considered deprecated. Always use CSS for styling instead!Which attribute is used to provide alternative text for an image if the image fails to load?
Which attribute is used to specify the destination URL of a link in the <a> tag?