Background Position

Utilities for controlling the position of an element’s background image.

Default class reference

Class
Properties
bg-bottombackground-position: bottom;
bg-centerbackground-position: center;
bg-leftbackground-position: left;
bg-left-bottombackground-position: left bottom;
bg-left-topbackground-position: left top;
bg-rightbackground-position: right;
bg-right-bottombackground-position: right bottom;
bg-right-topbackground-position: right top;
bg-topbackground-position: top;

Usage

Use the bg-{side} utilities to control the position of an element’s background image.

Background Position - 图1

  1. <div class="bg-no-repeat bg-left-top ..." style="background-image: url(...);"></div>
  2. <div class="bg-no-repeat bg-top ..." style="background-image: url(...);"></div>
  3. <div class="bg-no-repeat bg-right-top ..." style="background-image: url(...);"></div>
  4. <div class="bg-no-repeat bg-left ..." style="background-image: url(...);"></div>
  5. <div class="bg-no-repeat bg-center ..." style="background-image: url(...);"></div>
  6. <div class="bg-no-repeat bg-right ..." style="background-image: url(...);"></div>
  7. <div class="bg-no-repeat bg-left-bottom ..." style="background-image: url(...);"></div>
  8. <div class="bg-no-repeat bg-bottom ..." style="background-image: url(...);"></div>
  9. <div class="bg-no-repeat bg-right-bottom ..." style="background-image: url(...);"></div>

Responsive

To control the position of an element’s background image at a specific breakpoint, add a {screen}: prefix to any existing background position utility. For example, adding the class md:bg-top to an element would apply the bg-top utility at medium screen sizes and above.

  1. <div class="bg-center md:bg-top ..." style="background-image: url(...)"></div>

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

Customizing

Background Positions

By default, Tailwind provides nine background-position utilities. You change, add, or remove these by editing the theme.backgroundPosition section of your Tailwind config.

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. backgroundPosition: {
  5. bottom: 'bottom',
  6. + 'bottom-4': 'center bottom 1rem',
  7. center: 'center',
  8. left: 'left',
  9. - 'left-bottom': 'left bottom',
  10. - 'left-top': 'left top',
  11. right: 'right',
  12. 'right-bottom': 'right bottom',
  13. 'right-top': 'right top',
  14. top: 'top',
  15. + 'top-4': 'center top 1rem',
  16. }
  17. }
  18. }

Variants

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

You can control which variants are generated for the background position utilities by modifying the backgroundPosition 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. + backgroundPosition: ['hover', 'focus'],
  7. }
  8. }
  9. }

Disabling

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

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

←Background Opacity   Background Repeat→