On this page11 sections
  1. Learning Objectives
  2. 1. The Button Element
  3. 2. Button Types
  4. 3. Doing Something on Click
  5. 4. Disabled Buttons
  6. 5. Styling Buttons
  7. 6. Button or Link?
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 018: Buttons

Category4: Page Structure
Lecture018 of 048
Estimated video length8 min
TargetHTML5 · any modern browser
LevelBeginner

A button is something people press to make something happen: submit a form, open a menu, play a video. HTML's <button> element is keyboard- and screen-reader-friendly out of the box. A link, by contrast, is for going somewhere.


Learning Objectives

  • Create buttons with <button> and choose the right type
  • Run JavaScript when a button is clicked
  • Disable a button
  • Know when to use a button and when to use a link

1. The Button Element

Example

html
<button type="button">Click me</button>

Anything can go between the tags, including icons and bold text:

html
<button type="button">🔔 <strong>Notify me</strong></button>

2. Button Types

typeWhat it does
submitSends the form it's inside (the default inside a form)
resetClears the form back to its starting values
buttonNothing by itself; you attach JavaScript

Example

html
<form>
  <input name="city" placeholder="Your city">
  <button type="submit">Send</button>
  <button type="reset">Clear</button>
</form>

Always write the type. Inside a form, a button with no type submits, which surprises people.


3. Doing Something on Click

Example

html
<p id="count">Clicked 0 times</p>
<button type="button" onclick="clicks++; document.getElementById('count').textContent = 'Clicked ' + clicks + ' times';">
  Add one
</button>
<script>let clicks = 0;</script>

Lecture 020 covers JavaScript in HTML properly.


4. Disabled Buttons

Example

html
<button type="button" disabled>Sold out</button>

A disabled button can't be clicked or focused, and browsers grey it out.


5. Styling Buttons

Example

html
<style>
  .btn { background: #2f9e44; color: white; border: none; padding: 10px 20px; border-radius: 6px; font-size: 16px; cursor: pointer; }
  .btn:hover { background: #2b8a3e; }
  .btn-outline { background: white; color: #2f9e44; border: 2px solid #2f9e44; }
</style>

<button type="button" class="btn">Buy now</button>
<button type="button" class="btn btn-outline">Add to wishlist</button>

The CSS course covers button design in depth (CSS Lecture 044).


  • Goes to another page or place → <a href="…">, even if it's styled to look like a button.
  • Does something on this page (submit, open, toggle, play) → <button>.

Screen readers announce the two differently, so picking the right one matters.


Common Beginner Mistakes

1. No type inside a form. The button submits the form by surprise. Write type="button" for non-submit buttons.

2. A clickable <div>. It can't be reached with the keyboard. Use <button>.

3. Buttons for navigation. If it just goes to a URL, use a link.

4. Empty icon buttons. A button with only an icon needs aria-label="Close" so screen readers know what it does.


Practice Questions

  1. What is the default type of a button inside a form?
  2. Write a button that clears a form.
  3. Which attribute stops a button from being clicked?
  4. Should a "Read more" control that opens another page be a button or a link?
  5. Why is <button> better than a clickable <div>?
  6. Write a button that shows an alert saying "Hi!" when clicked.

Practice Answers

  1. submit
  2. <button type="reset">Clear</button>
  3. disabled
  4. A link (<a href>), because it goes somewhere.
  5. It works with the keyboard and screen readers automatically.
  6. <button type="button" onclick="alert('Hi!')">Say hi</button>

Key Takeaways

  • <button> is for actions; <a> is for navigation.
  • Always set type: submit, reset or button.
  • onclick runs JavaScript; disabled turns a button off.
  • Style buttons with CSS classes.