The basic HTML button tag
A button in HTML is created with the <button> tag. The simplest version looks like this:
<button>Click me</button>
The text between the opening and closing tags is what appears on the button. When you save this in an HTML file and open it in a browser, you'll see a clickable button with that text. The browser automatically styles it to look like a button — it has a border, a background color, and it responds when you hover over it or click it.
By default, this button doesn't do anything when clicked. To make it perform an action, you need to add code that tells it what to do. That's where attributes and JavaScript come in, which we'll cover in the sections below.
Key Takeaways
- The <button> tag creates a clickable button, and the text between the tags is what displays on the button itself.
- You can add the type attribute to specify whether the button submits a form, resets it, or just runs code when clicked.
- The onclick attribute lets you run JavaScript code when someone clicks the button.
- You can style buttons with CSS to change their color, size, font, and appearance to match your website's design.
- Buttons inside a <form> tag automatically submit the form data when clicked, unless you change the type attribute.
Adding the type attribute
The type attribute tells the button what job it should do. There are three main types: submit, reset, and button.
A submit button sends form data to a server. If you place a button inside a form tag without specifying a type, it defaults to submit:
<form> <input type="text" name="username"> <button type="submit">Send</button> </form>
A reset button clears all the fields in a form back to their original values. A button type does nothing on its own — it's used when you want to attach custom code with JavaScript. If you're building a button that doesn't sit inside a form, use type="button" to prevent unexpected behavior:
<button type="button">Click for more info</button>
Making a button run code with onclick
The onclick attribute runs JavaScript code when someone clicks the button. Here's a simple example that shows an alert message:
<button onclick="alert('Hello!')">Say hello</button>
When you click this button, a popup window appears with the message "Hello!". You can put any JavaScript code inside the quotes after onclick=. For more complex actions, you can call a function instead:
<button onclick="myFunction()">Click me</button>
Then elsewhere in your HTML file, inside a <script> tag, you define what myFunction does. This keeps your code organized and makes it easier to reuse the same action on multiple buttons.
Styling buttons with CSS
By default, buttons look plain. CSS lets you change their appearance. You can target buttons by their tag name, a class, or an ID. Here's an example that changes the background color, text color, and padding:
button { background-color: blue; color: white; padding: 10px 20px; border: none; border-radius: 5px; }
This CSS rule applies to every button on the page. The padding property adds space inside the button around the text. The border-radius property rounds the corners. The border: none removes the default border that browsers add.
You can also style buttons when someone hovers over them or clicks them, which gives users visual feedback:
button:hover { background-color: darkblue; cursor: pointer; }
Buttons inside forms vs. standalone buttons
A button behaves differently depending on whether it's inside a <form> tag. Inside a form, a button with type="submit" automatically sends all the form data to a server when clicked. You don't need to write any code for this — it's built-in behavior.
Outside a form, a button does nothing unless you add an onclick attribute or JavaScript code. This is useful for buttons that trigger actions on the current page, like showing or hiding content, changing colors, or running calculations.
If you place a button inside a form but don't want it to submit the form, use type="button". This prevents the default submit behavior and lets you attach your own code with onclick.
Common button attributes and what they do
Beyond type and onclick, several other attributes control how buttons work. The disabled attribute makes a button unclickable and grayed out:
<button disabled>Not available</button>
The name and value attributes are used with form buttons. When a form submits, these attributes tell the server which button was clicked and what value to send. The id attribute gives the button a unique name so you can target it with CSS or JavaScript. The class attribute lets you group buttons together and style them all the same way.
You can also add the title attribute to show a tooltip when someone hovers over the button:
<button title="Click to save your changes">Save</button>
Frequently Asked Questions
What's the difference between a button and a link?
A link (created with the <a> tag) takes you to a different page or location. A button performs an action on the current page or submits form data. Buttons are better for actions; links are better for navigation. You can style a link to look like a button with CSS, but semantically they serve different purposes.
Can I put an image inside a button?
Yes. You can place an <img> tag between the opening and closing button tags, or combine text and an image. The button will display whatever HTML content you put inside it.
How do I disable a button after it's clicked?
You can use JavaScript in the onclick attribute to set the disabled property. For example: onclick="this.disabled=true" will disable the button immediately after someone clicks it. This is useful for preventing accidental double-clicks on submit buttons.
Why doesn't my button do anything when I click it?
Check that the button has a type attribute set correctly and that any JavaScript code in the onclick attribute is spelled correctly. If the button is inside a form, make sure the form has an action attribute pointing to where the data should be sent. Open your browser's developer tools (usually F12) to check for error messages.
Can I make a button look like a regular link?
Yes, with CSS. You can remove the border, background color, and padding, and style the text to look like a link. This is useful when you want the visual appearance of a link but the functional behavior of a button — for example, a button that runs JavaScript code instead of navigating to a new page.