HTML Attributes
In HTML, attributes are used to provide additional information about an element. They are always included inside the opening tag of the element and are written as name-value pairs. Each attribute has a specific purpose and can affect the behavior or appearance of the element.
Basic Syntax
The general syntax for an attribute is:
<element attribute="value">
Here is an example of an img element with the src attribute:
<img src="watermelon.jpg" alt="Cat in a watermelon">
Result:


Common Attributes
There are many attributes available in HTML for different elements. Some of the most common attributes include:
| Attribute | Description |
|---|---|
id |
Specifies a unique identifier for an element |
class |
Specifies one or more class names for an element (used for styling with CSS) |
href |
Specifies the URL of the link |
src |
Specifies the URL of the image |
Example
Here is an example of how attributes are used in HTML:
<div id="main-content" class="mycontainer">
<p>This is a paragraph of text.</p>
<a href="html_introduction.html">Learn more</a><br />
<img src="watermelon.jpg" alt="Cat in a watermelon">
</div>
In the above example, the div element has both id and class attributes. The a element has the href attribute set to link to another page, and the img element has the src attribute set to display an image.