1. Backgrounds
  2. background-size

Backgrounds

背景尺寸

用于控制元素背景图像尺寸的功能类。

ClassStyles
bg-auto
background-size: auto;
bg-cover
background-size: cover;
bg-contain
background-size: contain;
bg-(<custom-property>)
background-size: var(<custom-property>);
bg-[<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-[<value>] syntax to set the background image size based on a completely custom value:

<div class="bg-[200px_100px] ...">  <!-- ... --></div>

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

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

This is just a shorthand for bg-[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.

版权所有 © 2025 Tailwind Labs Inc.·商标政策