Background Color

    Usage

    Control the background color of an element using the utilities.

    Control the opacity of an element’s background color using the .bg-opacity-{amount} utilities.

    Background Color - 图2

    1. <div class="bg-blue-500 bg-opacity-100"></div>
    2. <div class="bg-blue-500 bg-opacity-50"></div>
    3. <div class="bg-blue-500 bg-opacity-25"></div>
    4. <div class="bg-blue-500 bg-opacity-0"></div>

    Learn more in the .

    To control the background color of an element at a specific breakpoint, add a {screen}: prefix to any existing background color utility. For example, use md:bg-green-500 to apply the bg-green-500 utility at only medium screen sizes and above.

    For more information about Tailwind’s responsive design features, check out the Responsive Design documentation.

    all

    sm

    Background Color - 图5

    md

    lg

    Background Color - 图7

    xl

    1. <button class="bg-blue-500 sm:bg-green-500 md:bg-indigo-500 lg:bg-red-500 xl:bg-black ...">Button</button>

    Button

    Hover

    To control the background color of an element on hover, add the hover: prefix to any existing background color utility. For example, use hover:bg-blue-500 to apply the bg-blue-500 utility on hover.

    Hover utilities can also be combined with responsive utilities by adding the responsive {screen}: prefix before the focus: prefix.

    1. <button class="... md:bg-blue-500 md:hover:bg-blue-700 ...">Button</button>

    To control the background color of an element on focus, add the prefix to any existing background color utility. For example, use focus:bg-blue-500 to apply the bg-blue-500 utility on focus.

    1. <input class="bg-gray-200 focus:bg-white ...">

    Focus utilities can also be combined with responsive utilities by adding the responsive {screen}: prefix before the focus: prefix.

    Customizing

    By default Tailwind makes the entire available as background colors.

    You can customize your color palette by editing the theme.colors variable in your tailwind.config.js file, or customize just your background colors using the theme.backgroundColor section of your Tailwind config.

    1. // tailwind.config.js
    2. module.exports = {
    3. theme: {
    4. backgroundColor: theme => ({
    5. - ...theme('colors'),
    6. + 'secondary': '#ffed4a',
    7. + 'danger': '#e3342f',
    8. }
    9. }

    By default, only responsive, hover and focus variants are generated for background color utilities.

    You can control which variants are generated for the background color utilities by modifying the backgroundColor property in the variants section of your tailwind.config.js file.

    For example, this config will also generate active and group-hover variants:

    1. // tailwind.config.js
    2. module.exports = {
    3. variants: {
    4. // ...
    5. - backgroundColor: ['responsive', 'hover', 'focus'],
    6. + backgroundColor: ['responsive', 'hover', 'focus', 'active', 'group-hover'],
    7. }

      Background Opacity →