1. 背景
  2. 背景尺寸

背景

背景大小

用于控制元素背景图像大小的工具类。

ClassStyles
bg-auto
background-size: auto;
bg-cover
background-size: cover;
bg-contain
background-size: contain;
bg-size-(<custom-property>)
background-size: var(<custom-property>);
bg-size-[<value>]
background-size: <value>;

示例

填满容器

使用 bg-cover 工具类缩放背景图像,直到填满背景层,如有必要则裁剪图像:

<div class="bg-[url(/img/mountains.jpg)] bg-cover bg-center"></div>

填满而不裁剪

使用 bg-contain 工具类将背景图像缩放到外边缘,而不裁剪或拉伸:

<div class="bg-[url(/img/mountains.jpg)] bg-contain bg-center"></div>

使用默认大小

使用 bg-auto 工具类以其默认大小显示背景图像:

<div class="bg-[url(/img/mountains.jpg)] bg-auto bg-center bg-no-repeat"></div>

使用自定义值

Use the bg-size-[<value>] syntax to set the 背景大小 based on a completely custom value:

<div class="bg-size-[auto_100px] ...">  <!-- ... --></div>

For CSS variables, you can also use the bg-size-(<custom-property>) syntax:

<div class="bg-size-(--my-image-size) ...">  <!-- ... --></div>

This is just a shorthand for bg-size-[var(<custom-property>)] that adds the var() function for you automatically.

响应式设计

Prefix a 背景大小 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.