HOW-TO

How to add a button in HTML

The <button> tag, a button that opens another page, a button that runs code, a styled button, and why a button sometimes does nothing when you click it.

4 min read·

Have the file already? Upload it and get a link anyone can open. Free plan, no card.

Get a link free

To add a button in HTML, put <button type="button">Click me</button> inside the <body> of your page. That draws a clickable button with the browser’s default look. On its own it does nothing; the sections below show how to make it open a page, run code or submit a form.

A button that opens another page

  • ✓Use a link styled as a button: <a href="pricing.html" class="btn">See pricing</a>. A link is the right element for going somewhere, it works without JavaScript, and people can right-click it to open in a new tab.
  • ✓To open it in a new tab, add target="_blank": <a href="https://example.com" class="btn" target="_blank" rel="noopener">Open site</a>.
  • ✓If you must use a <button>, add an onclick: <button type="button" onclick="location.href='pricing.html'">See pricing</button>. This needs JavaScript and cannot be opened in a new tab by right-clicking.

A button that runs code

Give the button an id and attach a click handler in a <script> at the end of the body:

<button type="button" id="hello">Say hello</button> <script>document.getElementById('hello').addEventListener('click', () => { alert('Hello!'); });</script>

Swap the alert for whatever the button should do: show a hidden section, add up numbers, copy text with navigator.clipboard.writeText('…').

A button that submits a form

Inside a <form>, a <button> with no type submits the form: <form action="/subscribe" method="post"><input name="email" type="email" required><button>Subscribe</button></form>. The action needs a server that accepts the data; a plain HTML file has none. For collecting answers without your own server, see a feedback form with no backend.

Style the button with CSS

Put this in the <head> and add class="btn" to any <button> or <a>:

<style>.btn{display:inline-block;padding:10px 18px;border:0;border-radius:8px;background:#3a6bc2;color:#fff;font:600 16px/1.2 system-ui,sans-serif;text-decoration:none;cursor:pointer}.btn:hover{background:#2f5aa6}.btn:focus-visible{outline:3px solid #9bb8ea;outline-offset:2px}</style>

display:inline-block lets a link take padding like a button; text-decoration:none removes the link underline; the :focus-visible rule keeps the button visible to people moving through the page with the keyboard. To center the button, wrap it in <div style="text-align:center">…</div>.

Why the button does nothing

  • ✓It is a plain <button> with no onclick, no handler and no form around it. Buttons have no built-in action outside a form.
  • ✓The script runs before the button exists. A <script> in the <head> that looks up the button finds nothing; move it to the end of the <body> or add defer.
  • ✓The page reloads instead. A <button> inside a <form> submits by default; add type="button" to buttons that should only run code.
  • ✓The id in the script does not match the id on the button. Ids are case-sensitive.
  • ✓Open the browser console (F12 or Cmd+Option+J in Chrome) and click the button; a red error names the line that failed.

Pages made with AI tools

Claude, ChatGPT and Gemini often write buttons that call code from a separate file or a framework. If a button works in the chat preview but not in the downloaded file, ask for “one self-contained HTML file with the script inline at the end of the body”. More on that in AI page works in chat but breaks when hosted.

Share the page as a link

With FileShare Pro you upload the .html file from the dashboard, copy the link and send it; the buttons work in any browser and the person opening it needs no account. When you change the page, upload it to the same link. Related: how to add a link in HTML and how to center text in HTML. Sign up free to share your first page.

Frequently asked

How do I add a button in HTML?

Put <button type="button">Click me</button> in the <body>. Add an onclick or a click handler to make it do something, or use <a href="page.html"> styled as a button to make it open a page.

How do I make a button link to another page?

Use a link styled as a button: <a href="page.html" class="btn">Go</a>, with CSS that gives .btn padding, a background color and no underline. Add target="_blank" to open it in a new tab.

Why does my button reload the page?

A <button> inside a <form> submits the form by default. Add type="button" so it only runs your code.

Why does my HTML button do nothing?

Usually it has no click handler, the script runs before the button exists, or the id in the script does not match. Move the script to the end of the body and check the browser console for errors.

Got a page to share?

Drop the file, copy the link — live in seconds, free plan included.

Start sharing free