Use loading placeholders for your components or pages to indicate something may still be loading.
About
Placeholders can be used to enhance the experience of your application. They’re
built only with HTML and CSS, meaning you don’t need any JavaScript to create them.
You will, however, need some custom JavaScript to toggle their visibility. Their
appearance, color, and sizing can be easily customized with our utility classes.
Example
In the example below, we take a typical card component and recreate it with placeholders
applied to create a “loading card”. Size and proportions are the same between the two.
import Button from 'react-bootstrap/Button';
import Card from 'react-bootstrap/Card';
import Placeholder from 'react-bootstrap/Placeholder';
function CardExample() {
return (
<div className="d-flex justify-content-around">
<Card style={{ width: '18rem' }}>
<Card.Img variant="top" src="holder.js/100px180" />
<Card.Body>
<Card.Title>Card Title</Card.Title>
<Card.Text>
Some quick example text to build on the card title and make up the
bulk of the card's content.
</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
<Card style={{ width: '18rem' }}>
<Card.Img variant="top" src="holder.js/100px180" />
<Card.Body>
<Placeholder as={Card.Title} animation="glow">
<Placeholder xs={6} />
</Placeholder>
<Placeholder as={Card.Text} animation="glow">
<Placeholder xs={7} /> <Placeholder xs={4} /> <Placeholder xs={4} />{' '}
<Placeholder xs={6} /> <Placeholder xs={8} />
</Placeholder>
<Placeholder.Button variant="primary" xs={6} />
</Card.Body>
</Card>
</div>
);
}
export default CardExample;
How it works
Create placeholders with the Placeholder
component and a grid column prop (e.g., xs={6}
)
to set the width
. They can replace the text inside an element or be added to an existing
component via the as
prop.
Additional styling is applied to PlaceholderButton
s via ::before
to ensure the height
is respected. You may extend this pattern for other situations as needed, or add a
within the element to reflect the height when actual text is rendered in its place.
import Placeholder from 'react-bootstrap/Placeholder';
function BasicExample() {
return (
<>
<p aria-hidden="true">
<Placeholder xs={6} />
</p>
<Placeholder.Button xs={4} aria-hidden="true" />
</>
);
}
export default BasicExample;
The use of aria-hidden="true"
only indicates that the element
should be hidden to screen readers. The loading behaviour of the
placeholder depends on how authors will actually use the placeholder styles,
how they plan to update things, etc. Some JavaScript code may be needed to
swap the state of the placeholder and inform AT users of the update.
Width
You can change the width
through grid column classes, width utilities, or inline styles.
import Placeholder from 'react-bootstrap/Placeholder';
function WidthExample() {
return (
<>
<Placeholder xs={6} />
<Placeholder className="w-75" /> <Placeholder style={{ width: '25%' }} />
</>
);
}
export default WidthExample;
Color
By default, the Placeholder
uses currentColor
. This can be overridden with a custom color
or utility class.
import Placeholder from 'react-bootstrap/Placeholder';
function ColorExample() {
return (
<>
<Placeholder xs={12} />
<Placeholder xs={12} bg="primary" />
<Placeholder xs={12} bg="secondary" />
<Placeholder xs={12} bg="success" />
<Placeholder xs={12} bg="danger" />
<Placeholder xs={12} bg="warning" />
<Placeholder xs={12} bg="info" />
<Placeholder xs={12} bg="light" />
<Placeholder xs={12} bg="dark" />
</>
);
}
export default ColorExample;
Sizing
The size of Placeholder
s are based on the typographic style of the parent element.
Customize them with sizing props: lg
, sm
, or xs
.
import Placeholder from 'react-bootstrap/Placeholder';
function SizeExample() {
return (
<>
<Placeholder xs={12} size="lg" />
<Placeholder xs={12} />
<Placeholder xs={12} size="sm" />
<Placeholder xs={12} size="xs" />
</>
);
}
export default SizeExample;
Animation
Animate placeholders by setting the prop animation
to glow
or wave
to better
convey the perception of something being actively loaded.
import Placeholder from 'react-bootstrap/Placeholder';
function AnimationExample() {
return (
<>
<Placeholder as="p" animation="glow">
<Placeholder xs={12} />
</Placeholder>
<Placeholder as="p" animation="wave">
<Placeholder xs={12} />
</Placeholder>
</>
);
}
export default AnimationExample;
API
Placeholder