HTML button Tag (2024)

Earn income with your HTML skills

Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.

The <button> tag creates a clickable button.

Buttons can contain content, such as text, icons, images, etc.

Three button types are available: button, submit, and reset.

Example

#

Clicking this <button> opens an alert box.

<button type="button" onclick="alert('Like has been saved!');"> Like post &nbsp;<img src="/img/html/heart.png" /></button>

Tip: A <button> is more flexible than an <input> element because it can contain any type of content, such as text, icons, and images.

More Examples

A <button> of type button which, when clicked, starts a JavaScript process.
Note: Once started, the button is disabled to prevent running multiple processes at the same time.

<button id="starter" type="button" onclick="start()">Start process</button> <br /><br /><progress id="progress-bar" value="0" max="100"></progress><script> let start = () => { setInterval( () => { let value = document.getElementById("progress-bar").value; value = Math.min(value + .1, 100) % 100; document.getElementById("progress-bar").value = value; }, 10 ); let button = document.getElementById("starter"); button.setAttribute("disabled", "disabled"); }</script>

Attributes for <button>

A complete list of <button> attributes.

Attribute Value Description
type button
submit
reset
Button's type
name name Defines a name for the button.
value text Initial value for the button
id identifier Defines a unique identifier for the button.
class classnames Sets one or more CSS classes to be applied to the button.
style CSS-styles Sets the style for the button.
data-* value Defines additional data that can be used by JavaScript.
hidden hidden Specifies whether the button is hidden.
title text Sets a title that displays as a tooltip.
tabindex index Sets a tab sequence relative to the other elements.
disabled disabled Specifies whether the button is disabled
form form-id Tells the browser which forms the button belongs to.
formaction URL Only for type="submit", indicates where the submitted form data shall be sent.
formenctype application/x-www-form-urlencoded,
multipart/form-data,
text/plain
Only for type="submit", indicates how the submitted form data should be encoded.
formmethod get
post
Only for type="submit", indicates how the submitted form data will be sent.
formnovalidate Only for type="submit", indicates that form data should not be validated on submit.
formtarget _blank
_self
_parent
_top
framename
Only for type="submit", indicates where the response should be displayed after form submission.
autofocus no value Button gets focused after page load

For additional global attributes see our global attributes list.

Submit button

A <button> of type submit. It will submit the form when clicked.

<form action="/tutorial/action.html"> <input type="text" name="email" placeholder="Enter your email"> <button type="submit">Submit</button></form>

Reset button

A <button> of type reset clears input controls to their original values.
Enter your email and then click reset.

<form> <input type="text" name="email" placeholder="Enter your email"> <button type="reset">Reset</button></form>

Did you know?

HTML button Tag (1)

Which is best: button or input?

The <button> element offers the same functionality as <input> -- specifically button, submit, and reset types. So which one is better?

The difference is that <input> is an empty element without content. The text on an <input> button is set with the value attribute and that is all you can do.

On the other hand, a <button> accepts other elements such as text, icons, and images. So, with <button> you have more options to custom design the control.

Form Tags

The <button> tag is part of a group of tags that are used to create data entry forms. This group is referred to as the Form tag group. Together, they allow you to create comprehensive data input solutions.

Here is a list of form tags

Element Description
<form> Defines a data entry area that contains input elements
<input> Creates an input field in which data can be entered
<label> Creates a label or brief description before input elements
<textarea> Creates a multi-line text input control
<select> Creates a dropdown control
<button> Creates a clickable button that can contain text or an image
<datalist> Specifies a list of options for a textfield (<input>) element
<fieldset> Groups related form elements and displays a box with a legend around these
<legend> Defines a caption for a fieldset

Browser support

Here is when <button> support started for each browser:

HTML button Tag (2)

Chrome

1.0 Sep 2008

HTML button Tag (3)

Firefox

1.0 Sep 2002

HTML button Tag (4)

IE/Edge

1.0 Aug 1995

HTML button Tag (5)

Opera

1.0 Jan 2006

HTML button Tag (6)

Safari

1.0 Jan 2003

You may also like


HTML button Tag (8)

Last updated on Sep 30, 2023


Earn income with your HTML skills

Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.


Guides

On this page

  • Button tag
  • Example
  • More Examples
  • Attributes
  • Submit button
  • Reset button
  • Did you know
  • Form tags
  • Browser support
  • You may also like
HTML button Tag (2024)

FAQs

How to pass value through button in HTML? ›

To pass data from an HTML button to a server or a script, you can set the value attribute of the button to the desired data. When the button is clicked, the browser will send a request to the server along with the data in the value attribute.

How to give HTML button value? ›

<button value="">

Sets the initial value of the button element. There has, historically, been some confusion over the exact purpose of the value attribute on the <button> element. With most other form elements, the value is sent to the server when the form is submitted.

Why should you use button instead of div? ›

Divs are not focusable, for instance, your tab key will not focus a div as it will for any other button on your device. Screen readers and other assistive devices don't recognize divs as clickable elements. Divs do not translate certain key inputs, like space bars or return keys, to clicks when focused.

Should I use button or a tag? ›

The <a> element is used to link to other webpages while the <button> element is used to trigger a JavaScript action.

How do I pass one value to another page in HTML? ›

One way to pass a variable from one HTML page to another using JavaScript is to use the browser's localStorage object. localStorage provides a way to store key-value pairs in the user's web browser, so that they persist across different pages and sessions.

How to get input value from user in HTML? ›

The <input> tag specifies an input field where the user can enter data. The <input> element is the most important form element. The <input> element can be displayed in several ways, depending on the type attribute.

What is the value of button onclick in HTML? ›

Note that the onclick attribute is purely JavaScript. The value it takes, which is the function you want to execute, says it all, as it is invoked right within the opening tag. In JavaScript, you invoke a function by calling its name, then you put a parenthesis after the function identifier (the name).

How do I create a functional button in HTML? ›

The <button> element is used to create an HTML button. Any text appearing between the opening and closing tags will appear as text on the button. No action takes place by default when a button is clicked. Actions must be added to buttons using JavaScript or by associating the button with a form.

What is the difference between button tag and input type button? ›

The main difference, besides the ability to add content inside the button element, is the default behavior. The input element with a type of button has no default behavior, whereas the button element default behavior is to submit. That is, the button type attribute defaults to submit.

Should I use button or div in HTML? ›

Why Divs Don't Make Good Buttons. Using a <div> tag instead of a <button> tag when coding buttons can cause many accessibility problems for people who cannot use a mouse, either due to mobility challenges, limited vision, or situational limitations.

Should you use HTML button? ›

<button> elements are much easier to style than <input> elements. You can add inner HTML content (think <i> , <br> , or even <img> ), and use ::after and ::before pseudo-elements for complex rendering. If your buttons are not for submitting form data to a server, be sure to set their type attribute to button .

Why is div used so much in HTML? ›

div elements are commonly used in HTML because they are used as containers for other HTML elements. They are used to group and organize other elements into logical sections of a web page, and they allow you to apply CSS styles to those sections.

Can a button be a tag? ›

The <button> tag defines a clickable button. Inside a <button> element you can put text (and tags like <i> , <b> , <strong> , <br> , <img> , etc.).

Why use button tag? ›

It is used to specify the name or a keyword which indicates where to display the response after submitting the form. It is used to specify the type of button for button elements. It is also used in <input> element to specify the type of input to display.

Does button tag have value? ›

The value attribute specifies the underlying value that is associated with a button. Important: If you use the <button> element in an HTML <form>, different browsers will submit different values.

How do you add value to a button? ›

HTML <button> data-* Attribute

A data-* attribute on a <button> tag attaches additional data to the button. To create a custom attribute, replace * with a lowercase string, such as data-id , data-status , or data-location .

How do I add text to a button in HTML? ›

The <button> tag defines a clickable button. Inside a <button> element you can put text (and tags like <i> , <b> , <strong> , <br> , <img> , etc.). That is not possible with a button created with the <input> element!

Can button element have a value? ›

The value property sets or returns the value of the value attribute of a button. The value attribute specifies the underlying value that is associated with a button. Important: If you use the <button> element in an HTML <form>, different browsers will submit different values.

Top Articles
Latest Posts
Article information

Author: Mr. See Jast

Last Updated:

Views: 5802

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.