Button
TheButton component is a fundamental UI element that allows users to trigger actions and events. Material UI's Button component provides a consistent, accessible, and customizable button implementation.
Buttons can be used to trigger actions, submit forms, or navigate to different pages.
Variants
Material UI buttons come in several variants to suit different use cases:
Contained Button
The most prominent button style, used for primary actions:
jsx<Button variant="contained" color="primary">Primary Action</Button>
Outlined Button
Used for secondary actions or when you want a less prominent button:
jsx<Button variant="outlined" color="secondary">Secondary Action</Button>
Text Button
The most subtle button style, often used for tertiary actions:
jsx<Button variant="text" color="inherit">Text Button</Button>
Colors
Buttons support different color schemes:
Sizes
Buttons come in three sizes to accommodate different design needs:
Common Props
The Button component accepts various props to customize its appearance and behavior:
jsx<Buttonvariant="contained" // 'text' | 'outlined' | 'contained'color="primary" // 'inherit' | 'primary' | 'secondary' | 'success' | 'error' | 'info' | 'warning'size="medium" // 'small' | 'medium' | 'large'disabled={false} // booleanfullWidth={false} // booleanstartIcon={<Icon />} // ReactNodeendIcon={<Icon />} // ReactNodeonClick={handleClick} // function>Button Text</Button>
Accessibility
Material UI buttons are built with accessibility in mind:
- Keyboard navigation: Buttons can be focused and activated using the keyboard
- Screen readers: Proper ARIA labels and roles are automatically applied
- Color contrast: Meets WCAG guidelines for color contrast ratios
- Focus indicators: Clear visual focus indicators for keyboard users
Best Practices
- Use clear, action-oriented text that describes what the button does
- Choose appropriate variants - contained for primary actions, outlined for secondary
- Maintain consistent sizing within your interface
- Provide loading states for buttons that trigger async operations
- Use appropriate colors to convey meaning (success, error, etc.)
Examples
Button with Icons
jsximport { Button } from '@mui/material';import { Send, Download } from '@mui/icons-material';<Button variant="contained" startIcon={<Send />}>Send Message</Button><Button variant="outlined" endIcon={<Download />}>Download File</Button>
Disabled State
Use thedisabled attribute to specify that a button should be disabled.
jsx<Button variant="contained" disabled>Disabled Button</Button>
Full Width Button
Use thefullWidth attribute to specify that a button should be full width.
jsx<Button variant="contained" fullWidth>Full Width Button</Button>