HTML5 Attributes
Attributes in HTML is used to describe HTML elements providing additional information about HTML elements. They are always included in the opening tag and usually come in name/value pairs like name="value".
Key Points About HTML Attributes
Syntax:
- Attributes are specified in the opening tag.
- The name is followed by an equals sign
=and the value is enclosed in double quotes (") or single quotes (').
<tagname attribute="value">Content</tagname>Common Attributes
- id: Specifies a unique id for an element
<tagname attribute="value">Content</tagname>- class: Specifies one or more class names for an element (used for CSS and Javascript)
<div class="className">Content</div>- style: Specifies an inline CSS style for an element
<div style="color:blue;">Content</div>- title: Provides additional information about an element (displayed as a tooltip)
<div title="This is a tooltip">Content</div>Event Attributes
- Attributes that handle events triggered by user interactions, such as clicks or keypresses
<button onclick="alert('Button clicked!')">Click Me</button>Specific Attributes
-
Attributes specific to particular elements
- src(for
<img>,<script>,<iframe>etc): Specifies the URL of the resource - alt(for
<img>): Provides alternative text for an image. - href(for
<a>): Specifies the URL of the linked document.
- src(for
<a href="https://example.com" id="exampleLink" class="linkClass" title="Example Site" >Visit Example</a><img src="image.jpg" alt="Description of image" />