Button

Struct Button 

Source
pub struct Button;
Expand description

The <button> element - represents a clickable button.

§Purpose

The <button> element represents a clickable button control. Unlike <input type="button">, it can contain rich content like text, images, and other elements. Used for form submission, resetting forms, or triggering custom JavaScript actions. More flexible and semantic than input buttons.

§Content Categories

  • Flow Content
  • Phrasing Content
  • Interactive Content
  • Palpable Content

§Permitted Content Model

  • Phrasing content (but no interactive content descendants)

§Common Use Cases

  • Form submission buttons
  • Form reset buttons
  • Custom action buttons with JavaScript
  • Toggle buttons for UI state changes
  • Buttons with icons or complex content

§Key Attributes

  • type: Button type (“submit”, “reset”, “button”)
  • name: Name for form submission
  • value: Value sent with form submission
  • disabled: Disables the button
  • form: Associates with a form by ID
  • formaction: Override form’s action URL
  • formmethod: Override form’s method
  • formenctype: Override form’s encoding type
  • formnovalidate: Override form’s validation
  • formtarget: Override form’s target

§Example

<!-- Submit button -->
<form action="/submit" method="post">
  <input type="text" name="username">
  <button type="submit">Submit</button>
</form>

<!-- Reset button -->
<form>
  <input type="text" name="data">
  <button type="reset">Reset Form</button>
</form>

<!-- Button with custom action -->
<button type="button" onclick="alert('Clicked!')">Click Me</button>

<!-- Button with icon -->
<button type="submit">
  <svg width="16" height="16">
    <path d="M8 0l8 8-8 8-8-8z"/>
  </svg>
  Submit Form
</button>

<!-- Disabled button -->
<button type="submit" disabled>Please wait...</button>

<!-- Button overriding form attributes -->
<form action="/default" method="post">
  <input type="text" name="data">
  <button type="submit">Normal Submit</button>
  <button type="submit" formaction="/alternative" formmethod="get">
    Alternative Submit
  </button>
</form>

<!-- Button associated with external form -->
<form id="myForm" action="/submit">
  <input type="text" name="field">
</form>
<button type="submit" form="myForm">Submit External Form</button>

<!-- Delete button with confirmation -->
<button type="button" onclick="if(confirm('Delete?')) submit()">
  Delete Item
</button>

§Accessibility

  • Use descriptive button text that explains the action
  • Provide aria-label when button contains only an icon
  • Use type="button" for non-submit actions to prevent accidental submission
  • Ensure sufficient color contrast for button text
  • Make buttons keyboard accessible (they are by default)
  • Use disabled attribute to prevent interaction, not just CSS
  • Provide visual feedback for button states (hover, active, disabled)

§WHATWG Specification

Trait Implementations§

Source§

impl HtmlElement for Button

Source§

const TAG: &'static str = "button"

The HTML tag name (e.g., “div”, “span”, “img”).
Source§

const VOID: bool = false

Whether this is a void element (self-closing, no children allowed).
Source§

impl<T: PhrasingContent> CanContain<T> for Button

Source§

impl CanContain<Text> for Button

Source§

impl FlowContent for Button

Source§

impl InteractiveContent for Button

Source§

impl PalpableContent for Button

Source§

impl PhrasingContent for Button

Auto Trait Implementations§

§

impl Freeze for Button

§

impl RefUnwindSafe for Button

§

impl Send for Button

§

impl Sync for Button

§

impl Unpin for Button

§

impl UnwindSafe for Button

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.