Backgrounds
Utilities for controlling the background size of an element's background image.
Class | Styles |
---|---|
bg-auto | background-size: auto; |
bg-cover | background-size: cover; |
bg-contain | background-size: contain; |
bg-(length:<custom-property>) | background-size: var(<custom-property>); |
bg-[<value>] | background-size: <value>; |
Use the bg-cover
utility to scale the background image until it fills the background layer, cropping the image if needed:
<div class="bg-[url(/img/mountains.jpg)] bg-cover bg-center"></div>
Use the bg-contain
utility to scale the background image to the outer edges without cropping or stretching:
<div class="bg-[url(/img/mountains.jpg)] bg-contain bg-center"></div>
Use the bg-auto
utility to display the background image at its default size:
<div class="bg-[url(/img/mountains.jpg)] bg-auto bg-center bg-no-repeat"></div>
Use the bg-[<value>]
syntax to set the background image size based on a completely custom value:
<div class="bg-[auto_100px] ..."> <!-- ... --></div>
For CSS variables, you can also use the bg-(length:<custom-property>)
syntax:
<div class="bg-(length:--my-image-size) ..."> <!-- ... --></div>
This is just a shorthand for bg-[length:var(<custom-property>)]
that adds the var()
function for you automatically.
Prefix a background-size
utility with a breakpoint variant like md:
to only apply the utility at medium screen sizes and above:
<div class="bg-auto md:bg-contain ..."> <!-- ... --></div>
Learn more about using variants in the variants documentation.
Tailwind CSS 中文网(本网站)的目标是为中文开发者提供准确、及时的 Tailwind CSS 中文文档
Tailwind CSS 中文文档由本网站维护人员共同翻译,采用 MIT 开源协议发布
本网站与 tailwindcss.com 及 Tailwind Labs Inc. 并无从属关系
Tailwind 商标由 Tailwind Labs Inc. 所有
京ICP备15031610号-8