Push Code Far

How to Disable a Button in HTML

By: Tony | Published: May 28, 2025

Hi there, future programmer! Let's think of a button on a website like the "Start" button of your favorite game. At times, we might want to "turn it off" so that no one can click on it ever again. That is called a disabled button—it is grayed out and won't respond.

Today, you will be learning to create a button and disable a button in HTML, CSS, and JavaScript. Are you ready? Let's do it!

HTML – Create and Disable the Button

Let's begin by creating a button with HTML, the computer code that constructs websites. HTML is like the groundwork of your site. Here is the code for a basic button:

HTML Disabled Button

<button disabled>Can't Click Me!</button>
<button id="mainButton">Click Me!</button>
<button onclick="disableButton()">
Disable the Button</button>

The disabled word within the tag instructs the computer, "Hey, don't let anyone click this!" It's such as locking up a toy, so nobody can play with it at the moment. When you add "disabled", the button is grayed-out, and you cannot do anything with it by clicking. 

Go ahead and insert this code into an HTML file and open it in a browser. You will have a grayed-out button that is "turned off." Awesome, huh? HTML is really easy, and you just created your first button!

Now we will use CSS to make it look even more like it's "off" so that people understand it's not clickable yet.

CSS – Let the Disabled Button Stand Out a Bit

Now let us get our disabled button to stand out a little using CSS, the language for adding color and style. It's akin to dressing your button up in a far-out getup! Here's how:

CSS Disabled Button

button {

padding: 10px 20px;

font-size: 16px;

}

button:disabled {

background-color: gray;

color: white;

opacity: 0.5;

cursor: not-allowed;

}

The first one creates a regular button with padding (like a cushion) and large text. The button: disabled section creates the button when it is disabled.

It makes the button gray, makes the text white, decreases the opacity of the background (such as turning down a light), and displays a "no-click" cursor. It's like putting up a notice that says, "This button is off! " Test it out by adding this CSS to your HTML file.

You’ll see the disabled button look super different, making it clear it’s not clickable.

Awesome job!

JavaScript – Disable the Button with Code

Now, let’s use JavaScript to control our button, like using a remote to turn off a toy! JavaScript is like a helper that listens for your commands. Here’s the code:

Disabled JavaScript Button

function disableButton() {

document.getElementById
("mainButton").disabled = true;

}

We created a button with an id of "mainButton" and one to create a click. The onclick="disableButton()" is saying, basically, "When I click on this, run the disableButton function!" That function gets the button by document.getElementById("mainButton") and does .disabled = true, which makes it grayed-out. It's like pressing a remote to lock a toy. Try this out in your HTML page—click the second button, and the first one goes gray! You're officially a JavaScript wizard!

Conclusion

Whoo-hoo, you've succeeded! You coded a button in HTML, dressed it up in CSS, and caused it to grey out in JavaScript. It's like putting a toy together, dressing it up, and adding a remote control to switch it on! Go now and have fun with the next thrilling challenge: create a button that greys out when pressed. The more you practice, the easier coding gets.