Skip to main content

Display

Utilities for controlling the display box type of an element.

Import

import { display } from '@mui/system';

API

From display/display.js:34:
export default compose(displayPrint, displayRaw, overflow, textOverflow, visibility, whiteSpace);

Display Property

Control the display type:
<Box display="block">Block</Box>
<Box display="inline">Inline</Box>
<Box display="inline-block">Inline Block</Box>
<Box display="flex">Flex</Box>
<Box display="grid">Grid</Box>
<Box display="none">Hidden</Box>
From display.js:14-16:
export const displayRaw = style({
  prop: 'display',
});

Responsive Display

Show/hide elements at different breakpoints:
{/* Hide on mobile, show on tablet and up */}
<Box display={{ xs: 'none', sm: 'block' }}>
  Hidden on mobile
</Box>

{/* Show only on mobile */}
<Box display={{ xs: 'block', sm: 'none' }}>
  Mobile only
</Box>

{/* Flex on desktop, block on mobile */}
<Box display={{ xs: 'block', md: 'flex' }}>
  Responsive display
</Box>
Control display for print media:
<Box displayPrint="none">
  Hidden when printing
</Box>

<Box displayPrint="block">
  Visible when printing
</Box>
From display.js:4-12:
export const displayPrint = style({
  prop: 'displayPrint',
  cssProperty: false,
  transform: (value) => ({
    '@media print': {
      display: value,
    },
  }),
});

Overflow

Control content overflow:
<Box overflow="auto">Auto scroll</Box>
<Box overflow="hidden">Clip overflow</Box>
<Box overflow="scroll">Always scroll</Box>
<Box overflow="visible">Visible overflow</Box>
From display.js:18-20:
export const overflow = style({
  prop: 'overflow',
});

Directional Overflow

<Box sx={{ overflowX: 'auto', overflowY: 'hidden' }}>
  Horizontal scroll only
</Box>

Text Overflow

Control text overflow behavior:
<Box
  textOverflow="ellipsis"
  overflow="hidden"
  whiteSpace="nowrap"
  width={200}
>
  This text will be truncated with ellipsis...
</Box>

<Box textOverflow="clip" overflow="hidden" width={200}>
  This text will be clipped
</Box>
From display.js:22-24:
export const textOverflow = style({
  prop: 'textOverflow',
});

Visibility

Control element visibility:
<Box visibility="visible">Visible</Box>
<Box visibility="hidden">Hidden but takes space</Box>
<Box visibility="collapse">Collapsed (for table elements)</Box>
From display.js:26-28:
export const visibility = style({
  prop: 'visibility',
});

White Space

Control text wrapping:
<Box whiteSpace="normal">Normal wrapping</Box>
<Box whiteSpace="nowrap">No wrapping</Box>
<Box whiteSpace="pre">Preserve whitespace</Box>
<Box whiteSpace="pre-wrap">Preserve whitespace with wrapping</Box>
<Box whiteSpace="pre-line">Collapse whitespace, preserve line breaks</Box>
From display.js:30-32:
export const whiteSpace = style({
  prop: 'whiteSpace',
});

Common Patterns

Truncate Text

<Box
  overflow="hidden"
  textOverflow="ellipsis"
  whiteSpace="nowrap"
  maxWidth={300}
>
  This is a long text that will be truncated with ellipsis when it exceeds the maximum width
</Box>

Multi-line Truncate

<Box
  sx={{
    display: '-webkit-box',
    overflow: 'hidden',
    WebkitBoxOrient: 'vertical',
    WebkitLineClamp: 3,
  }}
>
  This text will be truncated after 3 lines with an ellipsis. This is useful for card descriptions
  and preview text where you want to show a limited amount of content.
</Box>

Scrollable Container

<Box
  overflow="auto"
  maxHeight={400}
  sx={{
    '&::-webkit-scrollbar': {
      width: 8,
    },
    '&::-webkit-scrollbar-thumb': {
      backgroundColor: 'divider',
      borderRadius: 1,
    },
  }}
>
  Scrollable content...
</Box>

Hide/Show on Print

{/* Hide on screen, show when printing */}
<Box display="none" displayPrint="block">
  This content only appears when printing
</Box>

{/* Show on screen, hide when printing */}
<Box display="block" displayPrint="none">
  This content is hidden when printing
</Box>

Responsive Flex/Grid

<Box
  display={{ xs: 'block', sm: 'flex', md: 'grid' }}
  sx={{
    gap: 2,
    gridTemplateColumns: { md: 'repeat(3, 1fr)' },
  }}
>
  <Box>Item 1</Box>
  <Box>Item 2</Box>
  <Box>Item 3</Box>
</Box>

With sx Prop

<Box
  sx={{
    display: 'flex',
    overflow: 'hidden',
    '&:hover': {
      overflow: 'auto',
    },
  }}
>
  Show scrollbar on hover
</Box>

TypeScript

import { Box } from '@mui/system';

type DisplayValue = 'block' | 'inline' | 'flex' | 'grid' | 'none';

interface DisplayBoxProps {
  display?: DisplayValue | { xs?: DisplayValue; sm?: DisplayValue; md?: DisplayValue };
  children: React.ReactNode;
}

function DisplayBox({ display, children }: DisplayBoxProps) {
  return <Box display={display}>{children}</Box>;
}

Available Properties

PropCSS PropertyValues
displaydisplayblock, inline, inline-block, flex, grid, none, etc.
displayPrintdisplay (in @media print)Same as display
overflowoverflowauto, hidden, scroll, visible
textOverflowtext-overflowellipsis, clip
visibilityvisibilityvisible, hidden, collapse
whiteSpacewhite-spacenormal, nowrap, pre, pre-wrap, pre-line

Build docs developers (and LLMs) love