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.

    Background Size - 图2

    1. <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.

    Background Size - 图4

    all

    sm

    Background Size - 图6

    md

    Background Size - 图8

    xl

    1. <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:

    1. // tailwind.config.js
    2. variants: {
    3. // ...
    4. - backgroundSize: ['responsive'],
    5. + backgroundSize: ['responsive', 'hover', 'focus'],
    6. }

    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: