Container

一个组件,用于将元素的宽度固定在当前断点处。

Class Breakpoint 属性
container None width: 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;

基本用法

使用 Container

容器类将元素的最大宽度(max-width)设置为与当前断点的最小宽度(min-width)相匹配。这对于喜欢设计固定屏幕尺寸而不是试图适应完整流体视口的情况非常有用。

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

要使容器居中,请使用 mx-auto 实用程序:

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

要添加水平填充,请使用 px-{size} 实用程序:

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

如果您想要默认居中容器或包含默认的水平填充,请参阅下面的自定义选项。

有条件地应用

响应式变体

容器类还包括响应式变体,如默认情况下包括 md:container ,允许您在仅在某个断点及更高版本时使某物表现得像一个容器:

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

自定义

默认居中对齐

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

  1. module.exports = {
  2. theme: {
  3. container: {
  4. center: true,
  5. },
  6. },
  7. }

增加水平间距

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

  1. module.exports = {
  2. theme: {
  3. container: {
  4. padding: '2rem',
  5. },
  6. },
  7. }

如果您想为每个断点指定不同的填充量,请使用对象提供默认值并为任何断点特定的覆盖:

  1. module.exports = {
  2. theme: {
  3. container: {
  4. padding: {
  5. DEFAULT: '1rem',
  6. sm: '2rem',
  7. lg: '4rem',
  8. xl: '5rem',
  9. '2xl': '6rem',
  10. },
  11. },
  12. },
  13. };