背景
background-position
用于控制元素背景图片位置的工具。
类 | 样式 |
---|---|
bg-bottom | background-position: bottom; |
bg-center | background-position: center; |
bg-left | background-position: left; |
bg-left-bottom | background-position: left bottom; |
bg-left-top | background-position: left top; |
bg-right | background-position: right; |
bg-right-bottom | background-position: right bottom; |
bg-right-top | background-position: right top; |
bg-top | background-position: top; |
bg-(position:<custom-property>) | background-position: var(<custom-property>); |
bg-[<value>] | background-position: <value>; |
示例(Examples)
¥Examples
基本示例(Basic example)
¥Basic example
使用 bg-center
、bg-right
和 bg-left-top
等工具控制元素背景图片的位置:
¥Use utilities like bg-center
, bg-right
, and bg-left-top
to control the position of an element's background image:
悬停在这些示例上以查看完整图片
bg-left-top
bg-top
bg-right-top
bg-left
bg-center
bg-right
bg-left-bottom
bg-bottom
bg-right-bottom
<div class="bg-[url(/img/mountains.jpg)] bg-left-top"></div><div class="bg-[url(/img/mountains.jpg)] bg-top"></div><div class="bg-[url(/img/mountains.jpg)] bg-right-top"></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-left-bottom"></div><div class="bg-[url(/img/mountains.jpg)] bg-bottom"></div><div class="bg-[url(/img/mountains.jpg)] bg-right-bottom"></div>
使用自定义值(Using a custom value)
¥Using a custom value
使用 bg-[<value>]
语法 根据完全自定义的值设置 background position:
<div class="bg-[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>
请参阅 变体文档 详细了解如何使用变体。