HTML & CSS - Mastery / HTML Advanced Elements

Labels and Buttons

↔️ Labels and Buttons

When you're building a form or any kind of interactive webpage, two super important elements are labels and buttons. They might look small, but they make your site more user-friendly and accessible.


Master Code Block (This will be used for further examples)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Forms Example</title>
  </head>
  <body>
    <!--All content to be added here-->
  </body>
</html>

🏷️ <label> — Describes What the Input Is For

Think of a label as a tag that tells the user what to enter in a particular input box.


🔍 Why use for?

The for="username" in the label connects it to the input with id="username"

This helps screen readers and improves accessibility

Also, when you click the label, the input box gets focused — cool right?

📌 You can also wrap the input inside the label (another valid method):

<body>
    <h1>Login Form</h1>
    <form action="/submit-login" method="post">
      <label>
        Username:
        <input
          type="text"
          name="username"
          placeholder="Enter your username"
          required
        />
      </label>
      <br /><br />
      <input type="submit" value="Login" />
    </form>
</body>

🟩 <button> — Triggers an Action

Buttons are what the user clicks to do something — like submit a form, open a popup, or perform an action.


🔘 Common type values

You can turn <input /> into a button-like control using its type attribute:

  • submit → sends the form data to the server

  • reset → clears all fields back to their default values

  • button → a plain button with no built-in behavior — you wire it up with JavaScript

  • image → renders as a clickable image that submits the form (needs a src attribute, like an <img>)

The <button> element supports the same three behavioral types:

  • submit → sends the form data (this is the default if you don't specify type at all — a common gotcha inside forms)

  • reset → clears the form

  • button → does nothing on its own until you attach JavaScript

So functionally, <input type="submit"> and <button type="submit"> do the same job — the difference is just what you're allowed to put inside them.

✨ Why <button> is more flexible

An <input>'s label is limited to a plain text value. A <button>, on the other hand, can hold real markup — icons, bold text, even nested elements


✅ input type="submit" vs button type="submit"

Both are used to submit a form, but there are key differences in syntax, styling, and flexibility.

🟠 1. <input type="submit">

<input type="submit" value="Send">

✅ Pros:

  • Simple and short

  • Works immediately

  • No closing tag

⚠️ Limitations:

  • Can’t include inner HTML (like icons or bold text)

  • Limited styling flexibility


🟢 2. <button type="submit">

<button type="submit">📩 <strong>Send</strong> Form</button>

✅ Pros:

  • Can contain HTML content (icons, images, bold/italic text)

  • Easier to style and customize

  • Can also be used as type="button" or type="reset"

⚠️ Considerations:

  • Slightly more verbose


🎯 When to Use What?

Use Case

Use This

Simple forms, no special content

<input type="submit">

Custom design, icons, or rich text

<button type="submit">

Need JavaScript + no form submission

<button type="button">


✅ Example Showing Both