HTML attributes provide additional information about HTML elements. They are always specified in the opening tag of an element and come in name-value pairs, separated by an equal sign. Attributes can control various aspects of an element, such as its appearance, behavior, and functionality.
Some common HTML attributes include id
, class
, style
, href
, and src
. Below are examples of how these attributes are used in HTML elements:
id
and class
AttributesThe id
attribute provides a unique identifier for an element, while the class
attribute specifies one or more class names for styling purposes:
<div id="container" class="box">
<p class="text">This is a paragraph.</p>
</div>
This is a paragraph.
style
, href
, and src
AttributesThe style
attribute applies inline CSS styles to an element. The href
attribute specifies the URL of a linked resource (used in <a>
tags), and the src
attribute specifies the URL of an image (used in <img>
tags):
<a href="https://www.shariqsp.com">Visit Example</a>
<img src="https://www.shariqsp.com/apple-touch-icon.png" alt="Example Image" style="border: 1px solid #ddd;" />