Background Position 背景位置
控制元素背景图片位置的实用工具。
| Class | Properties |
|---|
基本用法
设置背景位置
使用 bg-{side} 实用程序来控制元素背景图片的位置。
object-left-top

object-top

object-right-top

object-left

object-center

object-right

object-left-bottom

object-bottom

object-right-bottom

html
<div class="bg-no-repeat bg-left-top ..." style="background-image: url(...);"></div>
<div class="bg-no-repeat bg-top ..." style="background-image: url(...);"></div>
<div class="bg-no-repeat bg-right-top ..." style="background-image: url(...);"></div>
<div class="bg-no-repeat bg-left ..." style="background-image: url(...);"></div>
<div class="bg-no-repeat bg-center ..." style="background-image: url(...);"></div>
<div class="bg-no-repeat bg-right ..." style="background-image: url(...);"></div>
<div class="bg-no-repeat bg-left-bottom ..." style="background-image: url(...);"></div>
<div class="bg-no-repeat bg-bottom ..." style="background-image: url(...);"></div>
<div class="bg-no-repeat bg-right-bottom ..." style="background-image: url(...);"></div>有条件地应用
悬停、焦点和其他状态
Tailwind 允许您使用变体修饰符在不同状态下有条件地应用实用程序类。例如,使用 hover:bg-top 只在悬停时应用 bg-top 实用程序。
html
<div class="bg-center hover:bg-top ..." style="background-image: url(...)"></div>有关所有可用状态修改器的完整列表,请查看悬停、焦点和其他状态文档。
断点和媒体查询
您还可以使用变体修饰符来针对响应式断点、深色模式、prefers-reduced-motion 等媒体查询进行定位。例如,使用 md:bg-top 仅在中等屏幕尺寸及以上应用 bg-top 实用程序。
html
<div class="bg-center md:bg-top ..." style="background-image: url(...)"></div>要了解更多信息,请查看有关响应式设计、深色模式和其他媒体查询修饰符的文档。
使用自定义值
定制您的主题
默认情况下,Tailwind 提供了九个 background-position 实用工具。您可以通过编辑 Tailwind 配置文件的 theme.backgroundPosition 部分来更改、添加或删除这些工具。
js
module.exports = {
theme: {
backgroundPosition: {
bottom: 'bottom',
'bottom-4': 'center bottom 1rem',
center: 'center',
left: 'left',
'left-bottom': 'left bottom',
'left-top': 'left top',
right: 'right',
'right-bottom': 'right bottom',
'right-top': 'right top',
top: 'top',
'top-4': 'center top 1rem',
},
},
}有关自定义默认主题的更多信息,请参阅主题自定义文档。
任意值
如果您需要使用一个与主题无关的 background-position 值,可以使用方括号来根据任意值动态生成属性。
html
<p class="bg-[center_top_1rem]">
<!-- ... -->
</p>有关任意值支持的更多信息,请参阅任意值文档。