HTML Button

HTML buttons allow users to interact with websites and web applications. They are commonly used to submit forms, reset form fields, open menus, trigger JavaScript functions, and perform other actions. HTML provides the <button> element to create clickable buttons that can be customized using CSS and JavaScript.

What Is an HTML Button?

An HTML button is an interactive element that users can click or activate using the keyboard. Its behavior depends on the type attribute and how it is connected to a form or JavaScript function.

For example, a website may use a button to submit a contact form, display a message, or open a navigation menu.

To understand how buttons work inside forms, explore HTML Forms and HTML Form Elements.

HTML Button Example

The following example demonstrates how to create a basic HTML button.

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

The <button> element creates a clickable button, while the text between its opening and closing tags defines its label.

You can also create a button inside a form:

<form>
  <label for="name">Name:</label>
  <input type="text" id="name" name="name">

  <button type="submit">Submit</button>
  <button type="reset">Reset</button>
</form>

The Submit button sends the form data, while the Reset button restores the form controls to their initial values.

Types of HTML Buttons

The type attribute determines how a button behaves.

  • type="button" creates a regular button that does not submit the form automatically.
  • type="submit" submits the associated form.
  • type="reset" restores form controls to their initial values.

For example:

<button type="button">Open Menu</button>
<button type="submit">Submit Form</button>
<button type="reset">Reset Form</button>

These buttons have different purposes. Always specify the type attribute when using a button inside a form because a button without a type defaults to submit behavior in that context.

Learn more about HTML Submit Button and HTML Reset Button.

Using HTML Buttons with JavaScript

JavaScript allows buttons to perform actions when users click them. For example, you can display a message using an event listener.

<button type="button" id="message-button">Click Me</button>

<p id="message"></p>

<script>
  const button = document.getElementById("message-button");
  const message = document.getElementById("message");

  button.addEventListener("click", function () {
    message.textContent = "Welcome to Web4Learners!";
  });
</script>

When the user clicks the button, JavaScript updates the paragraph with a message.

Using addEventListener() helps keep JavaScript behavior separate from the HTML structure.

Styling HTML Buttons with CSS

CSS can improve the appearance of buttons by changing their colors, spacing, borders, and shape.

<style>
  .custom-button {
    background-color: #007bff;
    color: white;
    padding: 12px 20px;
    border: none;
    border-radius: 6px;
    font-size: 16px;
    cursor: pointer;
  }

  .custom-button:hover {
    background-color: #0056b3;
  }
</style>

<button type="button" class="custom-button">
  Learn More
</button>

The .custom-button class applies the styling, while the :hover selector changes the background when a user points to the button.

Important HTML Button Attributes

Several attributes help control button behavior.

  • type specifies the button’s purpose.
  • disabled prevents users from activating the button.
  • name identifies the button when its value is submitted with a form.
  • value defines the button’s submitted value when applicable.
  • autofocus requests that the browser focus the button when the page loads.

Use these attributes according to the button’s purpose. Avoid disabling controls without explaining why they are unavailable.

HTML Button vs Input Button

The <button> element can contain text and other permitted HTML content, making it flexible for custom designs. The <input type="button"> element creates a button whose label is specified through its value attribute.

Both can trigger JavaScript actions, but <button> is often more convenient for flexible styling and accessible labeling.

Frequently Asked Questions

Does an HTML button work without JavaScript?

Yes. Submit and reset buttons work with forms without JavaScript. A regular button needs an associated action, such as JavaScript, to perform a custom task.

How do I disable an HTML button?

Add the disabled attribute to prevent user interaction.

Can I add an image or icon inside a button?

Yes. A <button> can contain an image or an icon along with text, provided the content is valid HTML and the button remains accessible.

Why should I specify the button type?

Specifying type="button", type="submit", or type="reset" makes the intended behavior clear and helps prevent accidental form submissions.