Tailwind CSS - 容器
Tailwind CSS Container 容器 类用于在不同的断点处固定元素的宽度。此容器实用程序类可避免编写自定义 CSS 代码来调整元素在不同断点处的宽度。
Tailwind CSS 容器可确保响应式设计,并可通过在不同的断点处固定元素的宽度来实现内容水平居中。
语法
<element class="container"></element>
Tailwind CSS 容器类
以下是可以与 Tailwind CSS 容器 类有效配合使用的断点列表。
| 类 | 断点 | 属性 |
|---|---|---|
| Container | sm | max-width: 640px; |
| md | max-width: 768px; | |
| lg | max-width: 1024px; | |
| xl | max-width: 1280px; | |
| 2xl | max-width: 1536px; |
Tailwind CSS 容器类示例
以下示例将在不同断点处演示 Tailwind CSS 容器类。
小屏幕、中屏幕和大屏幕的容器背景变化
在以下示例中,内容背景颜色将在断点为"max-width: 640px;"的小(sm)屏幕尺寸、断点为"max-width: 768px;"的中(md)屏幕尺寸以及断点为"max-width: 1024px;"的大(lg)屏幕尺寸时发生变化。
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<p class="text-2xl font-bold">
Tailwind CSS - Container
</p>
<div class="container p-4
sm:bg-red-500
md:bg-green-500
lg:bg-blue-500">
<p>
This example illustrate the background color
changes of this content depending on the
screen size at sm,md,lg breakpoints.
</p>
</div>
</body>
</html>
超大尺寸和双倍超大尺寸的容器背景变化
以下示例中,内容背景颜色将在屏幕尺寸为"max-width: 1280px;"的超大尺寸(xl)和屏幕尺寸为"max-width: 1536px;"的双倍超大尺寸(2xl)下发生变化。
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<p class="text-2xl font-bold">
Tailwind CSS - Container
</p>
<div class="container p-4
xl:bg-red-500
2xl:bg-green-500">
<p>
This example illustrate the background color
changes of this content depending on the
screen size at xl and 2xl breakpoints.
</p>
</div>
</body>
</html>

