Background Size
Utilities for controlling the background size of an element's background image.
Auto
Use .bg-auto
to display the background image at its default size.
<div class="bg-auto bg-center ..." style="background-image: url(...)"></div>
Cover
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.
<div class="bg-contain bg-center ..." style="background-image: url(...)"></div>
Responsive
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.
For more information about Tailwind's responsive design features, check out the Responsive Design documentation.
<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 auto
, cover
, and contain
background sizes. You can change, add, or remove these by editing the theme.backgroundSize
section of your config.
// tailwind.config.js
module.exports = {
theme: {
backgroundSize: {
'auto': 'auto',
'cover': 'cover',
'contain': 'contain',
+ '50%': '50%',
+ '16': '4rem',
}
}
}
Responsive and pseudo-class variants
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
module.exports = {
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 false
in the corePlugins
section of your config file:
// tailwind.config.js
module.exports = {
corePlugins: {
// ...
+ backgroundSize: false,
}
}