HTML attributes provide additional information about elements and define their properties and behavior. Attributes are always included in the opening tag and usually follow the syntax:
<element attribute="value">Content</element>
HTMLThey help control styling, functionality, linking, accessibility, and much more.
1. Global Attributes
Global attributes can be applied to almost any HTML element.
a. id Attribute
The id
attribute assigns a unique identifier to an HTML element. It is mainly used for CSS styling or JavaScript interaction.
<p id="intro">This is an introduction paragraph.</p>
HTMLIn CSS:
#intro {
color: blue;
font-weight: bold;
}
CSSIn JavaScript:
document.getElementById("intro").innerHTML = "New content!";
JavaScript- Each
id
value must be unique across the document.
b. class Attribute
The class
attribute allows grouping of elements with the same class name for styling and JavaScript manipulation.
<div class="box">Box 1</div>
<div class="box">Box 2</div>
HTMLIn CSS:
.box {
background-color: lightgray;
padding: 10px;
}
CSSIn JavaScript:
let elements = document.getElementsByClassName("box");
JavaScript- Unlike
id
, multiple elements can share the sameclass
.
c. style Attribute
The style
attribute allows you to apply inline CSS styles directly to an element.
<h1 style="color: red; font-size: 24px;">Hello World</h1>
HTML- Not recommended for large-scale styling (use CSS instead).
d. title Attribute
The title
attribute defines additional information that appears when the user hovers over an element.
<p title="This is additional information">Hover over me</p>
HTMLe. data-* Attribute
Custom data-*
attributes allow developers to store extra information that can be accessed via JavaScript.
<div data-user-id="123">User Info</div>
HTMLJavaScript to retrieve the value:
let userId = document.querySelector("div").getAttribute("data-user-id");
console.log(userId); // Outputs: 123
JavaScript2. Link Attributes (<a>)
These attributes control hyperlinks.
a. href Attribute
Defines the URL where the link redirects.
<a href="https://example.com">Visit Example</a>
HTMLb. target Attribute
Controls where the link opens.
_self
: Default behavior (same tab)._blank
: Opens in a new tab._parent
: Opens in the parent frame._top
: Opens in the full body of the window.
<a href="https://example.com" target="_blank">Open in a new tab</a>
HTMLc. rel Attribute
Defines the relationship between the current document and the linked document.
nofollow
: Prevents search engines from following the link.noopener
: Prevents new tabs from accessing the original page.
<a href="https://example.com" rel="nofollow noopener">External Link</a>
HTML3. Image Attributes (<img>)
These attributes define an image’s properties.
a. src Attribute
Specifies the image file location.
<img src="image.jpg" alt="An example image">
HTMLb. alt Attribute
Provides alternative text if the image fails to load or for screen readers.
<img src="missing.jpg" alt="Image could not be loaded">
HTMLc. width and height Attributes
Define the image dimensions in pixels.
<img src="logo.png" width="150" height="100">
HTML4. Form Attributes (<input>, <form>)
Used for handling user inputs.
a. type Attribute
Defines the type of input field.
<input type="text">
<input type="password">
<input type="email">
<input type="checkbox">
<input type="radio">
<input type="submit">
HTMLb. name Attribute
Identifies the form field when data is submitted.
<input type="text" name="username">
HTMLc. value Attribute
Sets a default value in an input field.
<input type="text" name="username" value="JohnDoe">
HTMLd. placeholder Attribute
Displays a hint inside an input field.
<input type="text" placeholder="Enter your name">
HTMLe. required Attribute
Makes an input field mandatory.
<input type="email" required>
HTMLf. disabled and readonly Attributes
disabled
: Prevents user interaction.readonly
: Allows viewing but not editing.
<input type="text" value="Read Only" readonly>
<input type="text" value="Disabled" disabled>
HTML5. Table Attributes (<table>, <td>, <th>)
Used for structuring tables.
a. colspan Attribute
Merges multiple columns into a single cell.
<td colspan="2">Merged Column</td>
HTMLb. rowspan Attribute
Merges multiple rows into a single cell.
<td rowspan="2">Merged Row</td>
HTML6. Media Attributes (<audio>, <video>)
Used to control media playback.
a. controls Attribute
Displays media controls.
<audio src="audio.mp3" controls></audio>
HTMLb. autoplay and loop Attributes
autoplay
: Starts playback automatically.loop
: Repeats the media continuously.
<video src="video.mp4" controls autoplay loop></video>
HTMLConclusion
HTML attributes enhance elements by adding extra functionality. Whether controlling styles, linking resources, handling user inputs, or defining table and media properties, attributes help in creating interactive and structured web pages.