背景
background-position
用于控制元素背景图片位置的工具。
| 类 | 样式 |
|---|---|
bg-top-left | background-position: top left; |
bg-top | background-position: top; |
bg-top-right | background-position: top right; |
bg-left | background-position: left; |
bg-center | background-position: center; |
bg-right | background-position: right; |
bg-bottom-left | background-position: bottom left; |
bg-bottom | background-position: bottom; |
bg-bottom-right | background-position: bottom right; |
bg-position-(<custom-property>) | background-position: var(<custom-property>); |
bg-position-[<value>] | background-position: <value>; |
示例(Examples)
¥Examples
基本示例(Basic example)
¥Basic example
使用 bg-center、bg-right 和 bg-top-left 等工具控制元素背景图片的位置:
¥Use utilities like bg-center, bg-right, and bg-top-left to control the position of an element's background image:
悬停在这些示例上以查看完整图片
bg-top-left
bg-top
bg-top-right
bg-left
bg-center
bg-right
bg-bottom-left
bg-bottom
bg-bottom-right
<div class="bg-[url(/img/mountains.jpg)] bg-top-left"></div><div class="bg-[url(/img/mountains.jpg)] bg-top"></div><div class="bg-[url(/img/mountains.jpg)] bg-top-right"></div><div class="bg-[url(/img/mountains.jpg)] bg-left"></div><div class="bg-[url(/img/mountains.jpg)] bg-center"></div><div class="bg-[url(/img/mountains.jpg)] bg-right"></div><div class="bg-[url(/img/mountains.jpg)] bg-bottom-left"></div><div class="bg-[url(/img/mountains.jpg)] bg-bottom"></div><div class="bg-[url(/img/mountains.jpg)] bg-bottom-right"></div>使用自定义值(Using a custom value)
¥Using a custom value
使用 bg-position-[<value>] 语法 根据完全自定义的值设置 background position:
<div class="bg-position-[center_top_1rem] ..."> <!-- ... --></div>对于 CSS 变量,还可以使用 bg-position-(<custom-property>) 语法:
<div class="bg-position-(--my-bg-position) ..."> <!-- ... --></div>这只是简写,用于 bg-position-[var(<custom-property>)] 它会自动为你添加 var() 函数。
响应式设计(Responsive design)
¥Responsive design
在 background-position 工具前面使用断点变体如 md: 仅在 medium 屏幕尺寸及以上时应用工具:
<div class="bg-center md:bg-top ..."> <!-- ... --></div>请参阅 变体文档 详细了解如何使用变体。