HTML Attributes
HTML attributes are special keywords that are defined inside the HTML tags to provide additional information about an element. They define properties and behavior for elements and are always included in the opening tag.
Characteristics of HTML Attributes:
They appear inside the opening tag.
They are always written as name/value pairs:
name="value"
.Some attributes are specific to certain elements, while others (like
id
,class
, andstyle
) can be used with many different elements.
id
:- Used to uniquely identify an element.
<div id="header">This is the header</div>
- Used to uniquely identify an element.
src
:- Used with the
<img>
and<script>
elements to specify the source of an external file.
<img src="image.jpg" alt="Image description">
- Used with the
href
:Used in the
<a>
(anchor) element to specify the destination of a link.<a href="
https://www.example.com">Visit
Example</a>
alt
:Provides alternative text for an image if it cannot be displayed.
<img src="image.jpg" alt="Description of the image">
style
:Used to apply inline CSS directly to an element.
<p style="color: blue; font-size: 20px;">This is styled text.</p>
title
:- Provides additional information that appears as a tooltip when the user hovers over the element.
<button title="Click me">Hover over me</button>
- Provides additional information that appears as a tooltip when the user hovers over the element.
target
:- Used in
<a>
to specify where to open the linked document.
<a href="
https://www.example.com
" target="_blank">Open in new tab</a>
- Used in
name
:Used in form elements like
<input>
,<select>
, and<textarea>
to identify the form data.
<input type="text" name="username">
Example :
<!DOCTYPE html> <html lang="en"> <head> <a href="https://www.example.com" target="_blank" title="Click to visit">Visit Example</a> </html>
Subscribe to my newsletter
Read articles from dheeraj koranga directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by