Definition and Usage
The type="button" attribute of the <input> tag represents a button with no default behavior.
It is mainly used with JavaScript to control various actions when the user clicks the button.
Basic Example
<input type="button" value="Click the button" onclick="msg();">
<!-- Mainly used with JavaScript. -->
<script>
const msg = () => {
alert("Welcome.");
}
</script>
The implicit role value of <input type="button"> is button.
Using Related Attributes
Let's learn about the attributes related to <input type="button">.
value Attribute
The value attribute specifies the string to be used as the button's label.
<input type="button" value="String to be used as the label">
Note that if the value attribute is not specified, an empty button is displayed.
<!-- The value attribute is not specified. -->
<input type="button">
disabled Attribute
Use the disabled attribute to disable the button. A disabled button cannot be activated by clicking, using the keyboard, and so on.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<input type="button" value="Disabled button" disabled>
There are also various other related attributes that you can use.
See Various Attributes of the HTML <input> Tag for more information!
Styling Limitations
The <input> tag's type="button" is perfectly valid for representing a button control with no default behavior, but HTML also offers <button type="button">, which serves the same purpose.
The <input> tag is a void element, meaning it cannot have any content and does not have a closing tag (</input>).
The <button> tag has a closing tag (</button>), allowing it to contain child content.
Because it can hold inner content, you can include HTML elements or images within its label, giving you far more styling flexibility.
<style>
button {
color: inherit;
text-decoration: none;
padding: 0.5em 1em;
background-color: #fff;
border: 1px solid #777;
border-radius: 0.5em;
display: inline-flex;
align-items: center;
}
/* The input tag cannot use
::before or ::after. */
button::after {
content: "";
display: inline-flex;
width: 0.5em;
height: 0.5em;
border-left: 1px solid black;
border-bottom: 1px solid black;
transform: rotateZ(225deg);
margin-left: 0.2em;
}
</style>
<button type="button">Next Slid</button>
Browser Compatibility
| Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<input type="button"> |
1 | 12 | 1 | 1 |
Specifications
| Specification | |
|---|---|
<input type="button">
|
HTML Standard #button-state-(type=button) |