On this page11 sections
Lecture 018: Buttons
| Category | 4: Page Structure |
| Lecture | 018 of 048 |
| Estimated video length | 8 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
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 righttype - 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
<button type="button">Click me</button>Anything can go between the tags, including icons and bold text:
<button type="button">🔔 <strong>Notify me</strong></button>2. Button Types
type | What it does |
|---|---|
submit | Sends the form it's inside (the default inside a form) |
reset | Clears the form back to its starting values |
button | Nothing by itself; you attach JavaScript |
Example
<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
<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
<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
<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).
6. Button or Link?
- 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
- What is the default
typeof a button inside a form? - Write a button that clears a form.
- Which attribute stops a button from being clicked?
- Should a "Read more" control that opens another page be a button or a link?
- Why is
<button>better than a clickable<div>? - Write a button that shows an alert saying "Hi!" when clicked.
Practice Answers
submit<button type="reset">Clear</button>disabled- A link (
<a href>), because it goes somewhere. - It works with the keyboard and screen readers automatically.
<button type="button" onclick="alert('Hi!')">Say hi</button>
Key Takeaways
<button>is for actions;<a>is for navigation.- Always set
type:submit,resetorbutton. onclickruns JavaScript;disabledturns a button off.- Style buttons with CSS classes.