Background Image

Utilities for controlling an element’s background image.

Default class reference

Class
Properties
bg-nonebackground-image: none;
bg-gradient-to-tbackground-image: linear-gradient(to top, var(—tw-gradient-stops));
bg-gradient-to-trbackground-image: linear-gradient(to top right, var(—tw-gradient-stops));
bg-gradient-to-rbackground-image: linear-gradient(to right, var(—tw-gradient-stops));
bg-gradient-to-brbackground-image: linear-gradient(to bottom right, var(—tw-gradient-stops));
bg-gradient-to-bbackground-image: linear-gradient(to bottom, var(—tw-gradient-stops));
bg-gradient-to-blbackground-image: linear-gradient(to bottom left, var(—tw-gradient-stops));
bg-gradient-to-lbackground-image: linear-gradient(to left, var(—tw-gradient-stops));
bg-gradient-to-tlbackground-image: linear-gradient(to top left, var(—tw-gradient-stops));

Linear gradients

To give an element a linear gradient background, use one of the bg-gradient-{direction} utilities, in combination with the gradient color stop utilities.

Background Image - 图1

  1. <div class="bg-gradient-to-r from-yellow-400 via-red-500 to-pink-500 ..."></div>

Responsive

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

  1. <div class="bg-gradient-to-l md:bg-gradient-to-r ..."></div>

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

Customizing

Background Images

By default, Tailwind includes background image utilities for creating linear gradient backgrounds in eight directions.

You can add your own background images by editing the theme.backgroundImage section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. extend: {
  5. backgroundImage: theme => ({
  6. + 'hero-pattern': "url('/img/hero-pattern.svg')",
  7. + 'footer-texture': "url('/img/footer-texture.png')",
  8. })
  9. }
  10. }
  11. }

These don’t just have to be gradients — they can be any background images you need.

These classes will take the form bg-{key}, so hero-pattern will become bg-hero-pattern for example.

Variants

By default, only responsive variants are generated for background image utilities.

You can control which variants are generated for the background image utilities by modifying the backgroundImage 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. module.exports = {
  3. variants: {
  4. extend: {
  5. // ...
  6. + backgroundImage: ['hover', 'focus'],
  7. }
  8. }
  9. }

Disabling

If you don’t plan to use the background image utilities in your project, you can disable them entirely by setting the backgroundImage property to false in the corePlugins section of your config file:

  1. // tailwind.config.js
  2. module.exports = {
  3. corePlugins: {
  4. // ...
  5. + backgroundImage: false,
  6. }
  7. }

←Background Size   Gradient Color Stops→