Focus visible
Configure a theme option to apply an outline keyboard focus across Material UI components.
Starting from v9.x, Material UI provides built-in support for visual keyboard focus indicator through CSS. The demos on this page opt out of the ripple to show only the focus visible indicator.
Usage
Set focusVisible: true on the theme to render a default focus indicator on every ButtonBase-derived component when it receives keyboard focus:
import { createTheme } from '@mui/material/styles';
const theme = createTheme({ focusVisible: true });
The default focus indicator is a two-pixel solid outline with palette.primary.main color, offset by two pixels:
Press Tab to move keyboard focus — the ring appears on focus.
Inner focus indicator
Some components, for example Tab, render the focus indicator from the inside to avoid overflow-clipped container or overlapping with other elements.
Press Tab, then use the arrow keys — the ring insets so the Tabs scroller cannot clip it.
To see the full list of components that show inner focus indicator, check out the full demo below.
Customization
The focusVisible can be customized by passing a CSS object to merge with the default styles.
Changing the outline color
To customize the outline, for example changing the color, pass an object with specified outline color to the focusVisible node to merge with the default outline styles:
// Recolor only; width and offset stay at the curated 2px.
createTheme({ focusVisible: { outlineColor: '#9c27b0' } });
Use box-shadow as a second layer
A boxShadow can be additive on top of the outline. This is useful for a two-color ring (WCAG technique C40) that stays visible on any background.
To make the boxShadow flip for the inner focus indicator components, use focusVisibleVars.behavior from the styles:
import { focusVisibleVars } from '@mui/material/styles';
createTheme({
focusVisible: {
boxShadow: `${focusVisibleVars.behavior} 0 0 0 4px rgba(0, 0, 0, 0.4)`,
},
});
Replace outline with box-shadow
To replace the outline entirely with box-shadow indicator, set outline: 'none':
import { focusVisibleVars } from '@mui/material/styles';
createTheme({
focusVisible: {
outline: 'none',
boxShadow: `${focusVisibleVars.behavior} 0 0 0 3px #1976d2`,
},
});
Full focus visible demo
The complete set of components that render the focus indicator when focusVisible is enabled. Use the keyboard (Tab and arrow keys) to move focus and reveal the ring.
Button
IconButton
ButtonGroup
ToggleButton
Fab
Chip
Checkbox
Radio
Switch
Pagination
ButtonBase
AccordionSummary
Details
TableSortLabel
| Name | Size |
|---|---|
| file.txt | 12 KB |
Slider
Link
Breadcrumbs
Rating
Tab
Stepper
MenuItem
- Profile
- Settings
ListItemButton
BottomNavigation
CardActionArea
Select
Autocomplete