1. 尺寸
  2. max-width

尺寸

max-width

用于设置元素最大宽度的工具。

样式
max-w-<number>
max-width: calc(var(--spacing) * <number>);
max-w-<fraction>
max-width: calc(<fraction> * 100%);
max-w-3xs
max-width: var(--container-3xs); /* 16rem (256px) */
max-w-2xs
max-width: var(--container-2xs); /* 18rem (288px) */
max-w-xs
max-width: var(--container-xs); /* 20rem (320px) */
max-w-sm
max-width: var(--container-sm); /* 24rem (384px) */
max-w-md
max-width: var(--container-md); /* 28rem (448px) */
max-w-lg
max-width: var(--container-lg); /* 32rem (512px) */
max-w-xl
max-width: var(--container-xl); /* 36rem (576px) */
max-w-2xl
max-width: var(--container-2xl); /* 42rem (672px) */

示例(Examples)

基本示例(Basic example)

使用像 max-w-24max-w-64 这样的 max-w-<number> 工具,根据间距刻度将元素设置为固定的最大宽度:

🌐 Use max-w-<number> utilities like max-w-24 and max-w-64 to set an element to a fixed maximum width based on the spacing scale:

调整示例大小以查看预期行为

max-w-96
max-w-80
max-w-64
max-w-48
max-w-40
max-w-32
max-w-24
<div class="w-full max-w-96 ...">max-w-96</div><div class="w-full max-w-80 ...">max-w-80</div><div class="w-full max-w-64 ...">max-w-64</div><div class="w-full max-w-48 ...">max-w-48</div><div class="w-full max-w-40 ...">max-w-40</div><div class="w-full max-w-32 ...">max-w-32</div><div class="w-full max-w-24 ...">max-w-24</div>

使用百分比(Using a percentage)

使用 max-w-fullmax-w-<fraction> 工具,如 max-w-1/2max-w-2/5,为元素设置基于百分比的最大宽度:

🌐 Use max-w-full or max-w-<fraction> utilities like max-w-1/2 and max-w-2/5 to give an element a percentage-based maximum width:

调整示例大小以查看预期行为

max-w-9/10
max-w-3/4
max-w-1/2
max-w-1/3
<div class="w-full max-w-9/10 ...">max-w-9/10</div><div class="w-full max-w-3/4 ...">max-w-3/4</div><div class="w-full max-w-1/2 ...">max-w-1/2</div><div class="w-full max-w-1/3 ...">max-w-1/3</div>

使用容器比例(Using the container scale)

使用类似 max-w-smmax-w-xl 的工具,将元素设置为基于容器比例的固定最大宽度:

🌐 Use utilities like max-w-sm and max-w-xl to set an element to a fixed maximum width based on the container scale:

调整示例大小以查看预期行为

Andrew Alfred
Assistant to the Traveling Secretary
<div class="max-w-md ...">  <!-- ... --></div>

使用断点容器(Using breakpoints container)

使用 container 工具将元素的最大宽度设置为与当前断点的 min-width 相匹配。如果你更愿意为固定的屏幕尺寸设计,而不是尝试适应完全流动的视口,这会非常有用:

🌐 Use the container utility to set the maximum 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:

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

请注意,与你在其他框架中可能使用的容器不同,Tailwind 的容器不会自动居中,也没有任何内置的水平内边距。使用 mx-autopx-<number> 工具类来添加这些:

🌐 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. Use mx-auto and the px-<number> utilities to add these:

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

使用自定义值(Using a custom value)

使用 max-w-[<value>] 语法 根据完全自定义的值设置 maximum width

<div class="max-w-[220px] ...">  <!-- ... --></div>

对于 CSS 变量,还可以使用 max-w-(<custom-property>) 语法:

<div class="max-w-(--my-max-width) ...">  <!-- ... --></div>

这只是简写,用于 max-w-[var(<custom-property>)] 它会自动为你添加 var() 函数。

响应式设计(Responsive design)

max-width 工具前面使用断点变体如 md: 仅在 medium 屏幕尺寸及以上时应用工具:

<div class="max-w-sm md:max-w-lg ...">  <!-- ... --></div>

请参阅 变体文档 详细了解如何使用变体。

自定义主题(Customizing your theme)

max-w-<number> 工具由 --spacing 主题变量驱动,可以在你自己的主题中进行自定义:

@theme {  --spacing: 1px; }

主题变量文档中了解有关自定义间距比例的更多信息。

TailwindCSS 中文网 - 粤ICP备13048890号