Background Size
Auto
Use to display the background image at its default size.
Use .bg-cover
to scale the background image until it fills the background layer.
<div class="bg-cover bg-center ..." style="background-image: url(...)"></div>
Contain
Use .bg-contain
to scale the background image to the outer edges without cropping or stretching.
To control the size of an element’s background image at a specific breakpoint, add a {screen}:
prefix to any existing background size utility. For example, adding the class md:bg-contain
to an element would apply the bg-contain
utility at medium screen sizes and above.
all
sm
md
xl
<div class="bg-auto sm:bg-cover md:bg-contain lg:bg-auto xl:bg-cover ..." style="background-image: url(...)"></div>
Customizing
By default Tailwind provides utilities for , cover
, and contain
background sizes. You can change, add, or remove these by editing the theme.backgroundSize
section of your config.
By default, only responsive variants are generated for background size utilities.
You can control which variants are generated for the background size utilities by modifying the backgroundSize
property in the variants
section of your tailwind.config.js
file.
For example, this config will also generate hover and focus variants:
// tailwind.config.js
variants: {
// ...
- backgroundSize: ['responsive'],
+ backgroundSize: ['responsive', 'hover', 'focus'],
}
Disabling
If you don’t plan to use the background size utilities in your project, you can disable them entirely by setting the backgroundSize
property to in the corePlugins
section of your config file: