HTML <button> Tag: Usage, Attributes, and Real-World Examples

By Cristian G. Guasch •  Updated: 09/25/23 •  9 min read

Diving headfirst into the vast sea of HTML can feel overwhelming, but it doesn’t have to be. Today, we’ll delve into one specific element that’s fundamental yet powerful: the humble <button> tag. This tag is a workhorse in interactive web design, allowing us to create clickable buttons that users can interact with.

As I pull back the curtain on the <button> tag, you’ll discover its endless potential when combined with other elements and attributes. We’ll explore how this simple tool can lead to complex outcomes – think form submission, dialog boxes triggering, or even animations!

Lastly, we won’t shy away from actual examples. After all, what good is theory if we don’t see it in action? By the end of our journey together today, you’ll not only understand what a <button> tag does but also how to use it effectively within your own projects! So let’s roll up our sleeves and get acquainted with the versatile world of HTML buttons.

Understanding the HTML <button> Tag

Diving headfirst into web development, I’m sure you’ve come across the term ‘HTML’ quite a bit. It’s the backbone of any webpage we interact with daily. Now, let me introduce you to a tiny yet powerful element in HTML – the <button> tag.

Functioning as an interactive entity on a webpage, the <button> tag is used widely around the globe. What it does? Well, it creates clickable buttons that can perform various actions once clicked by users. These actions might be anything from submitting forms to triggering JavaScript events.

Here’s how simple it is to use:

<button type="submit">Click Me!</button>

Upon clicking this button, if placed within a form, your data gets submitted due to its type attribute being set as ‘submit’. The text inside these tags? That’s what appears on your button; in this case – “Click Me!”.

But hey! Don’t limit yourself thinking that’s all there is to the humble <button> tag! You’d be surprised at how versatile it can be! Let me explain – there are several attributes associated with this tag which offer us more functionality:

So if you want a disabled button linked with a specific form and focused when loaded? Your code would look like:

<button type="submit" disabled form="myForm" autofocus>Can't Click Me!</button>


In my adventures through coding land though, I’ve seen beginners make some common mistakes using this tag:

  1. Forgetting closing tags: Each opening <button> tag should have corresponding closing </button> tag.
  2. Not using attributes wisely: Always remember, type attribute is crucial when your button is inside a form.

So there you have it, the HTML <button> tag demystified. Go ahead, experiment with it and unlock its full potential!

Attributes of the HTML <button> Tag

Diving right into it, let’s discuss the various attributes of the HTML <button> tag. These attributes enhance and control the behavior of a button on a web page.

First off, there’s the type attribute. This decides what happens when you click on a button. It can be set to three possible values: “submit”, “reset”, or “button”. The default value is “submit”. Here’s an example:

<button type="submit">Click me</button>

In this case, clicking ‘Click me’ would submit a form if one exists.

Next up, we have disabled. When this attribute is present, it means that your button can’t be clicked or focused upon. For instance:

<button disabled>Can't touch this</button>

With this code snippet, our ‘Can’t touch this’ button isn’t clickable at all!

It’s also worth mentioning autofocus, which automatically puts focus on your button when the page loads. Check out how it works below:

<button autofocus>Focus on me!</button>

In contrast to our last example, ‘Focus on me!’ will grab users’ attention immediately upon loading.

Another key player is formaction, used only with buttons of type “submit”. This attribute specifies where to send form-data when a form is submitted.

Lastly but certainly not leastly – yes I know that’s not really a phrase – we have value. This optional attribute defines an initial value for the button element.

Now you might think these are quite straightforward so far? Well, they’re just scratching the surface! Having discussed just five out of many more potential attributes that could revamp your buttons in unimaginable ways, there’s always more to learn and experiment with! Remember folks – coding truly is an ever-evolving landscape.

Practical Examples Using HTML <button>

I’m going to dive into a few practical examples using the HTML <button> tag. It’s important to remember that this is more than just a decoration on your web page – it’s a functional part of your site’s interactivity.

Let me start with a basic example. If you’re looking to create a simple button, here’s how you’d do it:

<button type="submit">Click Me</button>

In this case, we’ve created a generic “submit” button labeled “Click Me”. Simple enough, right? But what if we want our button to perform different actions based on user interactions?

Enter JavaScript. By integrating JavaScript, we can make our buttons far more dynamic. Let’s look at an example where clicking the button changes its label:

<button id="dynamicButton" onclick="changeLabel()">Click Me</button>

function changeLabel() {
  document.getElementById("dynamicButton").innerHTML = "You Clicked!";

Here, I’ve included an onclick attribute in my <button> tag. Once clicked, the function changeLabel() executes and changes the button label from “Click Me” to “You Clicked!”.

But let’s not stop here! What about styling? We can utilize CSS for making our buttons visually appealing too!

.styledButton {
  background-color: #4CAF50; /* Green */
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;

<button class="styledButton">I am styled!</button>

In this snippet, I’ve introduced some CSS properties for our .styledButton class which are then applied on our <button> tag, making it look more attractive.

There are numerous ways to leverage the HTML <button> tag, and these examples just scratch the surface. The key is to experiment and find what works best for your specific needs. Common mistakes include forgetting to close the <button> tag or misusing attributes like type and value. Remember, practice makes perfect!

Common Mistakes and How to Avoid Them

Let’s delve into the common mistakes developers often make when working with the HTML <button> tag. Remember, even seasoned pros can slip up on occasion, so don’t beat yourself up if you’ve committed some of these blunders!

One classic pitfall involves forgetting to specify a type attribute for your button. If you neglect this crucial step, browsers will default to treating it as a submit button. This can lead to unexpected page refreshes or form submissions that might leave you scratching your head in confusion! Here’s an example:

<button>Click me!</button>

To avoid this mishap, always specify the type of your button explicitly; whether it’s submit, reset, or just button.

<button type="button">Click me!</button>

Moving along, another mistake is improperly nesting buttons within anchor (<a>) tags. It’s tempting because styling links like buttons offers visual consistency across your site. But here’s what happens if you do:

<a href="/next-page">
   <button>Go to next page</button>

This snippet won’t work as expected because HTML doesn’t allow block elements (like <button>) inside inline elements (like <a>). Instead, style your anchors using CSS to look like buttons.

Lastly is overlooking accessibility considerations such as missing out on adding descriptive text or ARIA roles for screen readers’ benefit. For instance:

<button><img src="play-icon.png"></button>

The above code leaves screen reader users stranded because there’s no textual description of what action the button performs! To fix this issue:

<button aria-label="Play video"><img src="play-icon.png"></button>

I hope highlighting these missteps helps steer clear from them in future coding endeavors, enhancing your overall HTML <button> tag usage!

Conclusion: Mastering the HTML <button> Tag

Delving into the world of HTML, I’ve found that mastering the <button> tag can be a game-changer. It’s a simple yet powerful tool in our coding toolbox, enabling us to create interactive elements in our web pages.

Let’s take one more look at its basic usage:

<button type="submit">Click me!</button>

Here, we’re declaring a button with the type attribute set to “submit”. When clicked, this button submits form data.

Remember, there’s much more than just the ‘type’ attribute. There are several other attributes associated with <button> such as disabled, form, value and so on. Each has its own purpose and use case which you can leverage depending upon your need.

Don’t forget about event handling too! We commonly pair buttons with JavaScript events for added functionality. For example:

<button onclick="myFunction()">Click me</button>

In this snippet, when someone clicks on the button, it’ll trigger myFunction().

There are common mistakes I see while working with <button>. One is forgetting to specify a type attribute. Without it, some browsers default to “submit”, which may not always be what you want. So it’s best practice to always include a type attribute!

Also remember that although both <input> tags with type="button" and <button> tags create buttons, they aren’t identical! The major difference lies in their content model – while an input element cannot have content inside it (it’s empty), a button element can contain text or even other HTML elements.

With patience and practice, you’ll find yourself confidently usingHTML <button> tag like second nature. Keep experimenting and happy coding!

Cristian G. Guasch

Hey! I'm Cristian Gonzalez, I created HTML Easy to help you learn HTML easily and fast.

Related articles