Lesson 5 of 21 – HTML Attributes
25%
25%

HTML Attributes

HTML attributes provide extra information about HTML elements. They help control the behavior, appearance, or functionality of elements on a webpage.

Attributes are written in the opening tag. They are usually written as name-value pairs like name="value".

What Are HTML Attributes?

HTML attributes give additional information about an element. They are always written inside the start tag. Most attributes come in name and value pairs.

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

Here, href is the attribute and "https://example.com" is its value.

Important Facts About HTML Attributes

  • Attributes provide additional information about elements.
  • Attributes are always written in the start tag.
  • Attributes usually appear as name="value".
  • All HTML elements can have attributes.

Common HTML Attributes

id

The id attribute uniquely identifies an element. It is used when we want to select one specific element.

<div id="main-content"></div>
class

The class attribute assigns one or more class names to an element. It is used for CSS styling and JavaScript.

<p class="text-center large-text"></p>
style

The style attribute is used to apply inline CSS styles.

<h1 style="color: blue; font-size: 24px;">Welcome</h1>
href

The href attribute specifies the URL of a link.

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

The src attribute specifies the source of an image or embedded media.

<img src="image.jpg" alt="Sample Image">
alt

The alt attribute gives alternative text for an image if the image cannot be displayed.

<img src="logo.png" alt="Company Logo">
title

The title attribute provides extra information as a tooltip when the user hovers over an element.

<button title="Click here to submit">Submit</button>
data-*

Data attributes are custom attributes that start with data-. They are used to store extra information.

<div data-user-id="12345"></div>
type

The type attribute specifies the type of an input element or button.

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

The placeholder attribute shows hint text inside an input box.

<input type="email" placeholder="Enter your email">
disabled

The disabled attribute makes an element inactive.

<button disabled>Submit</button>
value

The value attribute defines the value of input, option, or button elements.

<input type="text" value="Default Text">
action

The action attribute tells where to send form data when the form is submitted.

<form action="/submit" method="POST"></form>
method

The method attribute defines the HTTP method used for form submission. Common values are GET and POST.

<form method="POST"></form>
target

The target attribute defines where to open the linked document.

<a href="https://example.com" target="_blank">Open in new tab</a>
rel

The rel attribute defines the relationship between the current page and the linked page.

<a href="https://example.com" rel="nofollow">Example</a>
autocomplete

The autocomplete attribute tells the browser whether to suggest previously entered values.

<input type="text" autocomplete="off">
required

The required attribute makes an input field mandatory before submitting a form.

<input type="email" required>
readonly

The readonly attribute makes an input field read-only. The user can see the value but cannot edit it.

<input type="text" value="Fixed Value" readonly>
checked

The checked attribute preselects a checkbox or radio button.

<input type="checkbox" checked>

Key Points About HTML Attributes

  • Attributes are written as name-value pairs.
  • They are placed inside the start tag.
  • Global attributes such as class, id, style, and title can be used on many elements.
  • Attribute names are usually written in lowercase.
  • Some attributes like checked, disabled, and readonly are boolean attributes.
  • Data attributes allow us to store custom information in HTML elements.
💡 Remember
Attributes help us control and customize HTML elements. They are a very important part of HTML.

🧠 Quiz

Which attribute specifies the URL of a hyperlink?