Udemy logo

html button tagButtons! Everybody loves buttons. With the use of the HTML button tag, you can create your very own customizable buttons for your website or blog. If you’re unfamiliar with the HTML button tag, there are a few things you need to know to use it properly, and a few cool attributes you can learn to cater its use specifically to your needs. We’ll go over them all in this guide, and provide you with some handy resources to learn even more HTML coding tips. Check out this entry level HTML workshop for starters!

How to Create a Button

First thing’s first: how to create a button with the HTML button tag?

<button>Click Here!</button>

Without any fancy CSS to make it look nice, the above HTML should generate a button that looks like this:

button 1

You can learn how to make your site elements look nice with this beginner’s course on CSS. Looks aside, this button has another more important problem. It’s useless! Nothing happens when you click on it, though depending on your web browser, it will display the default “active button” animation when clicked. Still, a button that doesn’t do anything or take you anywhere is pointless. So what can you do?

How to Create a Button-Triggered Prompt

There are a couple event attributes that can be set inside the <button> tag and will trigger if their assigned action is carried out. For instance, the onclick event script is triggered when the mouse clicks on the <button> element.

<button type="button" onclick="alert('You did it!')">Click Here!</button>

The above code generates an HTML button that looks the same as the one pictured previously. When you click on this button, an alert box that displays “You did it!” will appear. You can also use the ondblclick event script, which requires the user to double-click on the button in order for the event to trigger. Confused about how this all works? Learn the ins and outs of HTML with this HTML prep course.

Attribute Values

One thing that might be particularly confusing about the above code is the type attribute. What does it mean? In short, the HTML button tag has three type attribute values.

The button type makes the button a clickable button, the submit type enables the button to submit form data, and the reset type enables the button to reset form data back to its starting values.

How to Create a Disabled Button

With the disabled HTML button tag attribute, you can create a button that is grayed out. This disabled button will appear faded by default, with no function or active button animation when the user clicks it. The disabled attribute is a boolean, meaning its presence will denote the button’s disabled status as true. As long as this condition is true, the button will remain inactive. While you don’t necessarily need to know about boolean logic to work with basic HTML, anyone who calls themselves a coder or a web developer should at least know the gist of it. Read up on boolean logic as it relates to  the Java programming languagein this guide.

Back to buttons. A disabled one in particular is useful for scenarios where a condition somewhere else on the page must be met before the user can be allowed to click on that button. See how to create one below.

<button type="button" disabled>Click Here! Just kidding.</button>

This will generate the following:

button 2

Even though this is just an image, the real thing will act just as the depiction above – static, inactive, and pretty useless. However, it’s useful, for instance, when you want a user to click a button only once. For instance, a user clicks a submit button to send a form to your database. Clicking it twice submits the form more than once. To stop this from happening, you can set the button’s attribute to disabled to limit the user to only one submission.

Create an Autofocused Button

Have you ever tabbed around a page and watched each link and button highlight itself in succession? The autofocus attribute creates a button that automatically highlights itself upon the page loading.

<button type="button" autofocus onclick="alert('You did it!')">Click Here!</button>

When the page with this code loads, the button will appear like this, automatically:

button 5

Because of this, the user can hit Enter on their keyboard, and the button will active, since it is already the focused element on the page. Learn more web development skills in this course.

Create a Form Submittable by a Button

If your website requires users to fill out information in a form and then send it in, you can assign a button to that form for submission purposes. This would look something like this:

<form action="example_form.asp" method="get" id="nameage">
Name: <input type="text" name="usersname"><br>
Age: <input type="text" name="usersage"><br>
<button type="submit" form="nameage" value="Submit">Submit</button>

This will create a form and a button that looks like this:

button 3

If you’re confused about the example_form.asp part of the <form> tag’s action attribute, check out this web development course on ASP. All you need to know in terms of the HTML button tag here is that the information input into the above form is submitted via the button, which is tied to the form with id of “nameage” through the form attribute in the button tag.

Specify the Form’s Target Using Individualized Buttons

With the use of the formtarget attribute, you can specify different targets for the form data in multiple buttons, giving the user options. Example:

<form action="example_form.asp" method="get">
 City: <input type="text" name="city"><br>
 State: <input type="text" name="state"><br>
<button type="submit">Submit</button>
<button type="submit" formtarget="_blank">Submit to a new window</button>

The only visual difference between these buttons, if you’re using the default style, is what the text inside them says. The difference that counts is where each button will display the response to the user’s submitted input.

button 6

The regular “Submit” button, which has no specified formtarget attribute, will simply open the response in the same frame as the form. The “Submit to a new window” button’s formtarget attribute value of “_blank” will open the response in a new window or tab, depending on the user’s internet browser. There are five possible formtarget attribute values:

Learn more web development basics with this introductory course, and create your first website with the help of this one.

Page Last Updated: May 2014

Top courses in HTML

The HTML & CSS Bootcamp 2024 Edition
Colt Steele
4.8 (2,502)
Highest Rated
Build Responsive Real-World Websites with HTML and CSS
Jonas Schmedtmann
4.7 (107,039)
Build Websites from Scratch with HTML & CSS
Brad Hussey, Code College
4.6 (14,600)
The Complete Guide to HTML
Colt Steele
4.7 (3,182)
Build Mega Responsive Website: HTML, CSS, JavaScript
Code And Create, George Lomidze, Lasha Nozadze
4.8 (58)
Highest Rated
Mastering HTML5: From Beginner to Advanced
Mehmood Khalil, Zaheer Irshad
4.9 (81)
HTML and CSS for Beginners - Build a Website & Launch ONLINE
Edwin Diaz, Coding Faculty Solutions
4.4 (36,243)
HTML, CSS, JavaScript, React - Online Certification Course
YouAccel Training, Blue Digital Media
4.3 (577)

More HTML Courses

HTML students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo