1. 布局
  2. 容器

Quick reference

断点
属性
containerNonewidth: 100%;
sm (640px)max-width: 640px;
md (768px)max-width: 768px;
lg (1024px)max-width: 1024px;
xl (1280px)max-width: 1280px;
2xl (1536px)max-width: 1536px;

基本用法

¥Basic usage

使用容器

¥Using the container

container 类设置元素的 max-width 以匹配当前断点的 min-width。如果你更愿意为一组固定的屏幕尺寸进行设计而不是尝试适应完全流畅的视口,这将很有用。

¥The container class sets the max-width of an element to match the min-width of the current breakpoint. This is useful if you’d prefer to design for a fixed set of screen sizes instead of trying to accommodate a fully fluid viewport.

请注意,与你在其他框架中使用的容器不同,Tailwind 的容器不会自动居中,也没有任何内置的水平填充。

¥Note that unlike containers you might have used in other frameworks, Tailwind’s container does not center itself automatically and does not have any built-in horizontal padding.

要使容器居中,请使用 mx-auto 工具:

¥To center a container, use the mx-auto utility:

<div class="container mx-auto">
  <!-- ... -->
</div>

要添加水平填充,请使用 px-* 工具:

¥To add horizontal padding, use the px-* utilities:

<div class="container mx-auto px-4">
  <!-- ... -->
</div>

如果你想默认将容器居中或包含默认的水平填充,请参阅下面的 定制选项

¥If you’d like to center your containers by default or include default horizontal padding, see the customization options below.


有条件地应用

¥Applying conditionally

响应变体

¥Responsive variants

container 类还包括默认情况下像 md:container 这样的响应式变体,它允许你仅在某个断点及更高处使某些东西表现得像容器:

¥The container class also includes responsive variants like md:container by default that allow you to make something behave like a container at only a certain breakpoint and up:

<!-- Full-width fluid until the `md` breakpoint, then lock to container -->
<div class="md:container md:mx-auto">
  <!-- ... -->
</div>

定制化

¥Customizing

默认居中

¥Centering by default

要默认居中容器,请在配置文件的 theme.container 部分将 center 选项设置为 true

¥To center containers by default, set the center option to true in the theme.container section of your config file:

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    container: {
      center: true,
    },
  },
}

添加水平填充

¥Adding horizontal padding

要默认添加水平填充,请使用配置文件的 theme.container 部分中的 padding 选项指定你想要的填充量:

¥To add horizontal padding by default, specify the amount of padding you’d like using the padding option in the theme.container section of your config file:

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    container: {
      padding: '2rem',
    },
  },
}

如果要为每个断点指定不同的填充量,请使用一个对象来提供 default 值和任何特定于断点的覆盖:

¥If you want to specify a different padding amount for each breakpoint, use an object to provide a default value and any breakpoint-specific overrides:

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    container: {
      padding: {
        DEFAULT: '1rem',
        sm: '2rem',
        lg: '4rem',
        xl: '5rem',
        '2xl': '6rem',
      },
    },
  },
};