HTML Boolean Attributes with Examples

HTML Boolean Attributes

HTML Boolean attributes control the behavior of elements and do not need a value. These attributes simplify logic and reduce code errors.

What Are Boolean Attributes in HTML?

Boolean attributes are attributes that do not need values. The presence of the attribute alone controls behavior. You can use them without a value.

They follow true or false logic. The attribute is true when present. It is false when absent.

You write Boolean attributes like this:

<input disabled>

The browser treats this as true for disabled. If you remove the attribute, it is false:

<input>

The browser does not look for disabled="false". That form has no effect.

Syntax Variations: With or Without Values

You can write Boolean attributes in two ways:

<input disabled>
<input disabled="disabled">

Both are valid. Both give the same result. But the first is cleaner. It avoids repetition.

So, which is better and why?

The form without a value is better. It matches HTML rules and avoids confusion about values. Browsers read both the same, but developers prefer the cleaner form.

Here are the differences between standard attributes that need values and boolean attributes that do not:

Boolean:

  • It treats it as true when it exists.
  • It treats it as false when it doesn’t exist.

Standard:

  • The browser reads the value directly.
  • Meaning changes with values.

Common Boolean Attributes and Their Usage

These Boolean attributes appear often in forms:

  • disabled
  • checked
  • readonly
  • required
  • autofocus
  • multiple
  • selected
  • hidden
  • novalidate

You can use them in the form tags and other tags. Let’s see examples:

<input type="text" disabled>

This input does not accept text. The user cannot click or type.

<input type="checkbox" checked>

This checkbox starts as checked.

<textarea readonly></textarea>

This textarea shows text but does not allow edits.

<input type="email" required>

This input must have a value before the form submits.

List of All Boolean Attributes in HTML5

Form elements:

  • disabled
  • readonly
  • required
  • autofocus
  • multiple
  • checked
  • selected
  • novalidate

Media elements:

  • autoplay
  • muted
  • controls
  • loop

Other elements:

  • hidden
  • itemscope
  • open
  • default
  • async
  • defer
  • formnovalidate
  • inert
  • reversed
  • scoped
  • truespeed
  • typemustmatch

All modern browsers support Boolean attributes. These include Chrome, Firefox, Safari, Edge, and Opera. Support covers both HTML4 and HTML5. Older browsers also support most Boolean logic.

Examples

Disable a Submit Button:

<button type="submit" disabled>Submit</button>

This button does not work. The browser blocks clicks. You can enable it later with JavaScript.

Autofocus on Input Field:

<input type="text" autofocus>

This input gets focus first when the page loads. It lets users type immediately.

Loop an Audio Clip Automatically:

<audio controls autoplay loop>
  <source src="sound.mp3" type="audio/mpeg">
</audio>

This plays audio when the page loads. It loops the clip. The loop works only if autoplay also exists.

Required Email with Readonly Value:

<input type="email" value="test@example.com" required readonly>

The input must have a value. The form does not submit without it. But the user cannot edit it.

Wrapping Up

In this article, you learned how HTML Boolean attributes work and how browsers treat them. You also saw the syntax and a full attribute list.

Here is a quick recap:

  • Boolean attributes use presence, not values.
  • They follow true/false logic.
  • Clean syntax avoids attribute="attribute".
  • Browsers support them widely.
  • You use them in forms and media. Also, use them in control tags.
  • You can apply them alone or with JavaScript.

What are HTML boolean attributes and how do they work in <input> elements?

HTML boolean attributes are attributes that can be either true or false by their presence or absence. When present, the attribute is considered true regardless of its value. For example, in an <input> element, the presence of the checked or disabled attribute activates that behavior.
<input type="checkbox" checked>
<input type="text" disabled>
If the attribute is omitted, the behavior is not applied:
<input type="checkbox">
<input type="text">

Can HTML boolean attributes have values like <checked="false"> or <disabled="false">?

While you can technically assign a value like checked="false", HTML will still treat the boolean attribute as true if it's present. HTML doesn't evaluate the value string; it only checks if the attribute exists.
<input type="checkbox" checked="false"> <!-- Still checked -->
To make it false, simply omit the attribute:
<input type="checkbox"> <!-- Not checked -->

What are common HTML boolean attributes used in <form> elements?

Common boolean attributes in form elements include:

<ul>
  <li><code>checked</code> - For <input type="checkbox"> and <input type="radio"></li>
  <li><code>disabled</code> - Disables form elements</li>
  <li><code>readonly</code> - Makes input read-only</li>
  <li><code>required</code> - Marks a field as mandatory</li>
  <li><code>autofocus</code> - Sets focus on page load</li>
  <li><code>multiple</code> - Allows multiple file selection</li>
</ul>
Each one works based on its presence, not its value.

How does JavaScript check or toggle HTML boolean attributes?

In JavaScript, use the hasAttribute, setAttribute, or removeAttribute methods to manage boolean attributes:

const input = document.querySelector('input');

// Check if 'disabled' is present
if (input.hasAttribute('disabled')) {
  input.removeAttribute('disabled');
} else {
  input.setAttribute('disabled', '');
}
Alternatively, use the property form for quick toggles:
input.disabled = true; // Adds 'disabled'
input.disabled = false; // Removes 'disabled'

Similar Reads

HTML Aside Tag: How to Create a Side Content

You use the <aside> tag to mark extra content in HTML. It does not replace the main text and shows…

HTML p Tag: How to Create a Paragraph in HTML

Paragraphs help group-related text. They build clear, readable pages. You use them to split ideas or topics. The HTML p…

HTML source Tag: How to Embed Media with Examples

The source tag in HTML helps load different media formats and screen sizes. You can use it inside many other…

HTML Link Attributes Guide for Beginners

HTML link attributes tell the browser how a link works. They set where the link goes, how it opens, and…

HTML main Tag: How to Create a Primary Content Area

The <main> tag in HTML marks the central content of a webpage. It tells browsers and assistive technologies where the…

HTML Span Tag: Inline Container Explained

The HTML span tag acts as a simple container for inline text or other inline elements. It groups text and…

HTML Elements and Tags: Elements vs Tags Explained

HTML builds web pages. It does this with elements and tags. Many new learners confuse them, so you need to…

HTML Global Attributes Overview

HTML global attributes work on most elements. They do not depend on the tag name. You can use them with…

HTML Footer Tag for Document Footers

The <footer> tag defines a footer section in HTML. It creates a clear endpoint for a page or section. Browsers…

HTML track Tag: How to Add Subtitles to Videos

The track tag in HTML adds text tracks like subtitles and captions to media. It improves video access and helps…

Previous Article

Comments in HTML: How They Work with Examples

Next Article

HTML progress Tag: How to Create Progress Bars with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.