Background Position

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

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 bg-gray-400 w-24 h-24" style="background-image: url(...);"></div>
  2. <div class="bg-no-repeat bg-top bg-gray-400 w-24 h-24" style="background-image: url(...);"></div>
  3. <div class="bg-no-repeat bg-right-top bg-gray-400 w-24 h-24" style="background-image: url(...);"></div>
  4. <div class="bg-no-repeat bg-left bg-gray-400 w-24 h-24" style="background-image: url(...);"></div>
  5. <div class="bg-no-repeat bg-center bg-gray-400 w-24 h-24" style="background-image: url(...);"></div>
  6. <div class="bg-no-repeat bg-right bg-gray-400 w-24 h-24" style="background-image: url(...);"></div>
  7. <div class="bg-no-repeat bg-left-bottom bg-gray-400 w-24 h-24" style="background-image: url(...);"></div>
  8. <div class="bg-no-repeat bg-bottom bg-gray-400 w-24 h-24" style="background-image: url(...);"></div>
  9. <div class="bg-no-repeat bg-right-bottom bg-gray-400 w-24 h-24" 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.

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

Background Position - 图2

all

Background Position - 图3

sm

Background Position - 图4

md

Background Position - 图5

lg

Background Position - 图6

xl

  1. <div class="bg-center sm:bg-top md:bg-right lg:bg-bottom xl:bg-left ..." style="background-image: url(...)"></div>

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

Responsive and pseudo-class 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. // ...
  5. - backgroundPosition: ['responsive'],
  6. + backgroundPosition: ['responsive', 'hover', 'focus'],
  7. }
  8. }

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 →