HTML Attributes

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>
HTML

They help control styling, functionality, linking, accessibility, and much more.

Global attributes can be applied to almost any HTML element.

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>
HTML

In CSS:

#intro {
    color: blue;
    font-weight: bold;
}
CSS

In JavaScript:

document.getElementById("intro").innerHTML = "New content!";
JavaScript

  • Each id value must be unique across the document.

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>
HTML

In CSS:

.box {
    background-color: lightgray;
    padding: 10px;
}
CSS

In JavaScript:

let elements = document.getElementsByClassName("box");
JavaScript

  • Unlike id, multiple elements can share the same class.

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).

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>
HTML

Custom data-* attributes allow developers to store extra information that can be accessed via JavaScript.

<div data-user-id="123">User Info</div>
HTML

JavaScript to retrieve the value:

let userId = document.querySelector("div").getAttribute("data-user-id");
console.log(userId); // Outputs: 123
JavaScript

These attributes control hyperlinks.

Defines the URL where the link redirects.

<a href="https://example.com">Visit Example</a>
HTML

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>
HTML

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>
HTML

These attributes define an image’s properties.

Specifies the image file location.

<img src="image.jpg" alt="An example image">
HTML

Provides alternative text if the image fails to load or for screen readers.

<img src="missing.jpg" alt="Image could not be loaded">
HTML

Define the image dimensions in pixels.

<img src="logo.png" width="150" height="100">
HTML

Used for handling user inputs.

Defines the type of input field.

<input type="text">
<input type="password">
<input type="email">
<input type="checkbox">
<input type="radio">
<input type="submit">
HTML

Identifies the form field when data is submitted.

<input type="text" name="username">
HTML

Sets a default value in an input field.

<input type="text" name="username" value="JohnDoe">
HTML

Displays a hint inside an input field.

<input type="text" placeholder="Enter your name">
HTML

Makes an input field mandatory.

<input type="email" required>
HTML

  • disabled: Prevents user interaction.
  • readonly: Allows viewing but not editing.
<input type="text" value="Read Only" readonly>
<input type="text" value="Disabled" disabled>
HTML

Used for structuring tables.

Merges multiple columns into a single cell.

<td colspan="2">Merged Column</td>
HTML

Merges multiple rows into a single cell.

<td rowspan="2">Merged Row</td>
HTML

Used to control media playback.

Displays media controls.

<audio src="audio.mp3" controls></audio>
HTML

  • autoplay: Starts playback automatically.
  • loop: Repeats the media continuously.
<video src="video.mp4" controls autoplay loop></video>
HTML

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.