1. 背景
  2. background-size(背景尺寸)

背景

background-size

用于控制元素背景图片尺寸的工具类。

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 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.

Copyright © 2025 Tailwind Labs Inc.·Trademark Policy