Tailwind CSS 核心语法篇 一:布局 发表于 2025-09-23 更新于 2025-11-20
字数总计: 14.5k 阅读时长: 74分钟 阅读量: 广东
前端技术 前端三方库 Tailwind CSS 核心语法篇 一:布局 Prorise 2025-09-23 2025-11-20 1. 布局 1.1 基础布局属性 1.1.1 aspect-ratio(宽高比) 类名 说明 CSS 等效 aspect-video16:9 的宽高比(适合视频) aspect-ratio: 16 / 9;aspect-square1:1 的宽高比(正方形) aspect-ratio: 1 / 1;aspect-[4/3]任意比例(此例为 4:3) aspect-ratio: 4 / 3;
1.1.2 columns(分栏布局) 类名 说明 CSS 等效 columns-1 到 columns-12指定列数 columns: 1; 到 columns: 12;columns-xs、columns-sm 等指定列宽(尺寸) columns: 20rem; 等gap-x-{size}控制列间距 column-gap: {size};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > </head > <body > <div class ="columns-3 gap-x-8" > <img class ="aspect-video" src ="https://placehold.co/600x400" /> <img class ="aspect-square" src ="https://placehold.co/400x400" /> <img class ="aspect-square" src ="https://placehold.co/400x400" /> </div > </body > </html >
1.1.3 display(显示方式) 类名 说明 CSS 等效 inline内联元素 display: inline;inline-block内联块元素 display: inline-block;block块级元素 display: block;flex弹性布局容器 display: flex;inline-flex内联弹性布局容器 display: inline-flex;grid网格布局容器 display: grid;inline-grid内联网格布局容器 display: inline-grid;flow-root块级格式化上下文 display: flow-root;contents内容显示 display: contents;hidden不显示元素 display: none;table、table-cell 等表格相关显示模式 display: table; 等
1 2 3 4 5 6 <script src ="https://cdn.tailwindcss.com" > </script > <div class ="flex justify-between" > <div class ="bg-red-500" > 项目1</div > <div class ="bg-blue-500" > 项目2</div > <div class ="bg-green-500" > 项目3</div > </div >
1.2 Flexbox 布局 1.2.1 flex-basis(初始大小) 类名 说明 CSS 等效 basis-0 到 basis-96使用间距比例 flex-basis: 0rem; 到 flex-basis: 24rem;basis-3xs 到 basis-7xl使用容器尺寸 flex-basis: 14rem; 到 flex-basis: 80rem;basis-1/2、basis-1/3 等使用分数 flex-basis: 50%;、flex-basis: 33.333333%; 等basis-[30vw]自定义值 flex-basis: 30vw;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > <style > .box { background-color : #ff0000 ; padding : 10px ; border : 1px solid #ccc ; border-radius : 5px ; } </style > </head > <body > <div class ="flex" > <div class ="box basis-1/3" > 项目1</div > <div class ="box basis-2/3" > 项目2</div > <div class ="box basis-1/3" > 项目3</div > </div > </body > </html >
1.2.2 flex-direction(方向) 类名 说明 CSS 等效 flex-row水平方向,从左到右 flex-direction: row;flex-row-reverse水平方向,从右到左 flex-direction: row-reverse;flex-col垂直方向,从上到下 flex-direction: column;flex-col-reverse垂直方向,从下到上 flex-direction: column-reverse;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > <style > .box { background-color : #ff0000 ; padding : 10px ; border : 1px solid #ccc ; border-radius : 5px ; } </style > </head > <body > <div class ="flex flex-col-reverse" > <div class ="box" > 项目1</div > <div class ="box" > 项目2</div > <div class ="box" > 项目3</div > </div > </body > </html >
1.2.3 flex-wrap(换行) 类名 说明 CSS 等效 flex-nowrap不换行,可能溢出 flex-wrap: nowrap;flex-wrap正常换行 flex-wrap: wrap;flex-wrap-reverse反向换行 flex-wrap: wrap-reverse;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > <style > .box { background-color : #ff0000 ; padding : 10px ; border : 1px solid #ccc ; border-radius : 5px ; } </style > </head > <body > <div class ="flex flex-row flex-wrap" > <div class ="box basis-1/3" > 项目1</div > <div class ="box basis-2/3" > 项目2</div > <div class ="box basis-1/3" > 项目3</div > </div > </body > </html >
1.2.4 flex-grow(扩展) 类名 说明 CSS 等效 grow允许扩展填充空间 flex-grow: 1;grow-0禁止扩展 flex-grow: 0;grow-[2]自定义扩展比例 flex-grow: 2;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > <style > .box { background-color : #ff0000 ; padding : 10px ; border : 1px solid #ccc ; border-radius : 5px ; } </style > </head > <body > <div class ="flex p-4" > <div class ="box grow-0 mr-2" > 不扩展 (grow-0)</div > <div class ="box grow mr-2" > 自动扩展 (grow)</div > <div class ="box grow-[2] mr-2" > 扩展系数为2 (grow-[2])</div > </div > </body > </html >
1.2.5 flex-shrink(收缩) 类名 说明 CSS 等效 shrink允许收缩 flex-shrink: 1;shrink-0禁止收缩 flex-shrink: 0;shrink-[2]自定义收缩比例 flex-shrink: 2;
1.2.6 flex(简写) 类名 说明 CSS 等效 flex-1可扩展、可收缩,初始大小 0 flex: 1 1 0%;flex-auto可扩展、可收缩,使用自身大小 flex: 1 1 auto;flex-initial不扩展、可收缩,使用自身大小 flex: 0 1 auto;flex-none不扩展、不收缩 flex: none;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > <style > .box { background-color : #ff0000 ; padding : 10px ; border : 1px solid #ccc ; min-width : 0 ; } </style > </head > <body > <div class ="container mx-auto p-6" > <h1 class ="text-2xl font-bold mb-6" > Tailwind Flex 演示</h1 > <div class ="mb-8" > <h2 class ="text-xl font-semibold mb-2" > flex-1: 可扩展、可收缩,初始大小0 (flex: 1 1 0%)</h2 > <div class ="flex border border-gray-300 p-2" > <div class ="flex-1 bg-blue-500 text-white p-4 m-1 text-center" > flex-1</div > <div class ="flex-1 bg-blue-500 text-white p-4 m-1 text-center" > flex-1</div > <div class ="flex-1 bg-blue-500 text-white p-4 m-1 text-center" > flex-1</div > </div > </div > <div class ="mb-8" > <h2 class ="text-xl font-semibold mb-2" > flex-auto: 可扩展、可收缩,使用自身大小 (flex: 1 1 auto)</h2 > <div class ="flex border border-gray-300 p-2" > <div class ="flex-auto bg-green-500 text-white p-4 m-1 text-center" > 短内容</div > <div class ="flex-auto bg-green-500 text-white p-4 m-1 text-center" > 较长的内容示例</div > <div class ="flex-auto bg-green-500 text-white p-4 m-1 text-center" > 更长的内容示例文本</div > </div > </div > <div class ="mb-8" > <h2 class ="text-xl font-semibold mb-2" > flex-initial: 不扩展、可收缩,使用自身大小 (flex: 0 1 auto)</h2 > <div class ="flex border border-gray-300 p-2" > <div class ="flex-initial bg-yellow-500 text-white p-4 m-1 text-center" > 短内容</div > <div class ="flex-initial bg-yellow-500 text-white p-4 m-1 text-center" > 较长的内容示例</div > <div class ="flex-initial bg-yellow-500 text-white p-4 m-1 text-center" > 更长的内容示例文本</div > </div > </div > <div class ="mb-8" > <h2 class ="text-xl font-semibold mb-2" > flex-none: 不扩展、不收缩,使用自身大小 (flex: 0 0 auto)</h2 > <div class ="flex border border-gray-300 p-2 overflow-x-auto" > <div class ="flex-none bg-red-500 text-white p-4 m-1 text-center w-64" > 固定宽度 flex-none</div > <div class ="flex-none bg-red-500 text-white p-4 m-1 text-center w-64" > 固定宽度 flex-none</div > <div class ="flex-none bg-red-500 text-white p-4 m-1 text-center w-64" > 固定宽度 flex-none</div > <div class ="flex-none bg-red-500 text-white p-4 m-1 text-center w-64" > 固定宽度 flex-none</div > </div > </div > </div > </body > </html >
1.2.7 order(顺序) 类名 说明 CSS 等效 order-1 到 order-12指定显示顺序 order: 1; 到 order: 12;order-first显示在最前 order: -9999;order-last显示在最后 order: 9999;order-none使用默认顺序 order: 0;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > <style > .box { background-color : #ff0000 ; padding : 10px ; border : 1px solid #ccc ; min-width : 0 ; } </style > </head > <body > <div class ="flex" > <div class ="box order-last" > 01</div > <div class ="box" > 02</div > <div class ="box order-first" > 03</div > </div > </body > </html >
1.3 Grid 布局 1.3.1 grid-template-columns(列模板) 类名 说明 CSS 等效 grid-cols-1 到 grid-cols-12创建 n 个等宽列 grid-template-columns: repeat(1, minmax(0, 1fr)); 等grid-cols-subgrid使用父网格列模板 grid-template-columns: subgrid;grid-cols-[200px_1fr_100px]自定义列模板 grid-template-columns: 200px 1fr 100px;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > <style > .grid-item { height : 8rem ; border-radius : 0.5rem ; display : flex; align-items : center; justify-content : center; } .grid-item span { color : white; font-size : 1.5rem ; font-weight : bold; } </style > </head > <body > <div class ="grid grid-cols-4 gap-4" > <div class ="bg-red-500 grid-item" > <span > 01</span > </div > <div class ="bg-blue-500 grid-item" > <span > 02</span > </div > <div class ="bg-green-500 grid-item" > <span > 03</span > </div > <div class ="bg-yellow-500 grid-item" > <span > 04</span > </div > </div > </body > </html >
1.3.2 grid-column(列位置) 类名 说明 CSS 等效 col-span-1 到 col-span-12横跨 n 列 grid-column: span 1 / span 1; 等col-start-1 到 col-start-13从第 n 条线开始 grid-column-start: 1; 等col-end-1 到 col-end-13到第 n 条线结束 grid-column-end: 1; 等col-auto自动放置 grid-column: auto;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > <style > .grid-item { height : 8rem ; border-radius : 0.5rem ; display : flex; align-items : center; justify-content : center; } .grid-item span { color : white; font-size : 1.5rem ; font-weight : bold; } </style > </head > <body > <div class ="grid grid-cols-3 gap-4" > <div class ="bg-red-500 grid-item col-span-3" > <span > 01</span > </div > <div class ="bg-blue-500 grid-item" > <span > 02</span > </div > <div class ="bg-green-500 grid-item" > <span > 03</span > </div > <div class ="bg-yellow-500 grid-item" > <span > 04</span > </div > <div class ="bg-purple-500 grid-item" > <span > 05</span > </div > <div class ="bg-orange-500 grid-item col-span-2" > <span > 06</span > </div > </div > </body > </html >
1.3.3 grid-template-rows(行模板) 类名 说明 CSS 等效 grid-rows-1 到 grid-rows-6创建 n 个等高行 grid-template-rows: repeat(1, minmax(0, 1fr)); 等grid-rows-subgrid使用父网格行模板(少用) grid-template-rows: subgrid;grid-rows-[200px_1fr_100px]自定义行模板 grid-template-rows: 200px 1fr 100px;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > <style > .grid-item { height : 8rem ; border-radius : 0.5rem ; display : flex; align-items : center; justify-content : center; } .grid-item span { color : white; font-size : 1.5rem ; font-weight : bold; } </style > </head > <body > <div class ="mb-6" > <h2 class ="text-xl font-bold mb-2" > grid-rows-1 (1行)</h2 > <div class ="grid grid-cols-3 grid-rows-1 gap-4 h-32" > <div class ="bg-red-500 grid-item" > <span > 01</span > </div > <div class ="bg-blue-500 grid-item" > <span > 02</span > </div > <div class ="bg-green-500 grid-item" > <span > 03</span > </div > </div > </div > <div class ="mb-6" > <h2 class ="text-xl font-bold mb-2" > grid-rows-2 (2行)</h2 > <div class ="grid grid-cols-3 grid-rows-2 gap-4 h-64" > <div class ="bg-red-500 grid-item" > <span > 01</span > </div > <div class ="bg-blue-500 grid-item" > <span > 02</span > </div > <div class ="bg-green-500 grid-item" > <span > 03</span > </div > <div class ="bg-yellow-500 grid-item" > <span > 04</span > </div > <div class ="bg-purple-500 grid-item" > <span > 05</span > </div > <div class ="bg-orange-500 grid-item" > <span > 06</span > </div > </div > </div > </div > </body > </html >
1.3.4 grid-row(行位置) 类名 说明 CSS 等效 row-span-1 到 row-span-6横跨 n 行 grid-row: span 1 / span 1; 等row-start-1 到 row-start-7从第 n 条线开始 grid-row-start: 1; 等row-end-1 到 row-end-7到第 n 条线结束 grid-row-end: 1; 等row-auto自动放置 grid-row: auto;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > <style > .grid-item { height : 8rem ; border-radius : 0.5rem ; display : flex; align-items : center; justify-content : center; } .grid-item span { color : white; font-size : 1.5rem ; font-weight : bold; } </style > </head > <body > <div class ="mb-6" > <h2 class ="text-xl font-bold mb-2" > Row Span Examples</h2 > <div class ="grid grid-cols-3 grid-rows-4 gap-4 h-96" > <div class ="bg-red-500 grid-item row-span-2" > <span > 01 (span 2)</span > </div > <div class ="bg-blue-500 grid-item" > <span > 02</span > </div > <div class ="bg-green-500 grid-item" > <span > 03</span > </div > <div class ="bg-yellow-500 grid-item" > <span > 04</span > </div > <div class ="bg-purple-500 grid-item row-span-3" > <span > 05 (span 3)</span > </div > <div class ="bg-orange-500 grid-item" > <span > 06</span > </div > </div > </div > </div > </body > </html >
1.4 间距与对齐 1.4.1 gap(间距) 类名 说明 CSS 等效 gap-0 到 gap-96设置行列间距 gap: 0rem; 到 gap: 24rem;gap-x-0 到 gap-x-96设置列间距 column-gap: 0rem; 到 column-gap: 24rem;gap-y-0 到 gap-y-96设置行间距 row-gap: 0rem; 到 row-gap: 24rem;gap-px、gap-x-px、gap-y-px1 像素间距 gap: 1px; 等
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > <style > .box { width : 100px ; height : 100px ; background-color : #000 ; color : #fff ; display : flex; align-items : center; justify-content : center; } </style > </head > <body > <div class ="grid grid-cols-2 gap-x-4 gap-y-4" > <div class ="box" > 01</div > <div class ="box" > 02</div > <div class ="box" > 03</div > <div class ="box" > 04</div > </div > </body > </html >
1.4.2 justify-content(主轴对齐) 类名 说明 CSS 等效 justify-start起始对齐 justify-content: flex-start;justify-center居中对齐 justify-content: center;justify-end末端对齐 justify-content: flex-end;justify-between两端对齐 justify-content: space-between;justify-around环绕对齐 justify-content: space-around;justify-evenly均匀对齐 justify-content: space-evenly;justify-stretch拉伸填充 justify-content: stretch;justify-normal默认对齐 justify-content: normal;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > <style > .box { width : 100px ; height : 100px ; background-color : #000 ; color : #fff ; display : flex; align-items : center; justify-content : center; } </style > </head > <body > <div class ="flex justify-center items-center" > <div class ="box" > 01</div > <div class ="box" > 02</div > <div class ="box" > 03</div > <div class ="box" > 04</div > </div > </body > </html >
1.4.3 align-items(交叉轴对齐) 类名 说明 CSS 等效 items-stretch拉伸填充(默认) align-items: stretch;items-start起始对齐 align-items: flex-start;items-center居中对齐 align-items: center;items-end末端对齐 align-items: flex-end;items-baseline基线对齐 align-items: baseline;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > <style > .box { width : 100px ; height : 100px ; background-color : #000 ; color : #fff ; display : flex; align-items : center; justify-content : center; } </style > </head > <body > <div class ="container h-screen" > <div class ="flex justify-evenly items-end h-full" > <div class ="box" > 01</div > <div class ="box" > 02</div > <div class ="box" > 03</div > <div class ="box" > 04</div > </div > </div > </body > </html >
1.4.4 justify-items(网格项水平对齐) 类名 说明 CSS 等效 justify-items-start左侧对齐 justify-items: start;justify-items-center水平居中 justify-items: center;justify-items-end右侧对齐 justify-items: end;justify-items-stretch水平拉伸(默认) justify-items: stretch;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > <style > .box { width : 100px ; height : 100px ; background-color : #000 ; color : #fff ; display : flex; align-items : center; justify-content : center; } </style > </head > <body > <div class ="container h-screen" > <h3 > justify-items-start (左侧对齐)</h3 > <div class ="grid grid-cols-4 justify-items-start" > <div class ="box grid-item" > 项目 1</div > <div class ="box grid-item" > 项目 2</div > <div class ="box grid-item" > 项目 3</div > <div class ="box grid-item" > 项目 4</div > </div > </div > </body > </html >
1.4.5 align-content(多行对齐) 类名 说明 CSS 等效 content-start顶部对齐 align-content: flex-start;content-center垂直居中 align-content: center;content-end底部对齐 align-content: flex-end;content-between垂直两端对齐 align-content: space-between;content-around垂直环绕对齐 align-content: space-around;content-evenly垂直均匀对齐 align-content: space-evenly;content-stretch垂直拉伸(默认) align-content: stretch;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > <style > .box { width : 100px ; height : 100px ; background-color : #000 ; color : #fff ; display : flex; align-items : center; justify-content : center; } </style > </head > <body > <div class ="container h-screen" > <div class ="mb-8" > <h2 class ="text-lg font-semibold mb-2" > content-start (顶部对齐)</h2 > <div class ="flex flex-wrap content-center bg-gray-200 h-48 w-full" > <div class ="bg-blue-500 text-white p-4 m-2" > 项目 1</div > <div class ="bg-blue-500 text-white p-4 m-2" > 项目 2</div > <div class ="bg-blue-500 text-white p-4 m-2" > 项目 3</div > <div class ="bg-blue-500 text-white p-4 m-2" > 项目 4</div > <div class ="bg-blue-500 text-white p-4 m-2" > 项目 5</div > </div > </div > </div > </body > </html >
1.4.6 justify-self/align-self(单项对齐) 类名 说明 CSS 等效 justify-self-auto继承父容器设置 justify-self: auto;justify-self-start左侧对齐 justify-self: start;justify-self-center水平居中 justify-self: center;justify-self-end右侧对齐 justify-self: end;justify-self-stretch水平拉伸(默认) justify-self: stretch;self-auto继承父容器设置 align-self: auto;self-start顶部对齐 align-self: flex-start;self-center垂直居中 align-self: center;self-end底部对齐 align-self: flex-end;self-stretch垂直拉伸 align-self: stretch;self-baseline基线对齐 align-self: baseline;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > <style > .box { width : 100px ; height : 100px ; background-color : #000 ; color : #fff ; display : flex; align-items : center; justify-content : center; } </style > </head > <body > <div class ="container h-screen" > <div class ="grid grid-cols-3 gap-4 bg-gray-100 p-4 mb-8" > <div class ="bg-blue-200 p-4 justify-self-start" > 左侧对齐</div > <div class ="bg-blue-300 p-4 justify-self-center" > 水平居中</div > <div class ="bg-blue-400 p-4 justify-self-end" > 右侧对齐</div > </div > </div > </body > </html >
1.5 定位与显示 本章主要介绍如何控制元素在页面上的精确定位、内容溢出、层级关系以及媒体元素的显示方式。这些工具是实现复杂布局和精细 UI 的基础。
1.5.1 position (定位) position 用于将元素从正常的文档流中脱离出来,以便对其进行精确的位置控制。它是创建下拉菜单、模态框、悬浮按钮等组件的核心属性。
核心概念:定位上下文
当一个元素被设置为 absolute、fixed 或 sticky 时,它会相对于其最近的 已定位祖先元素 (即 position 值不为 static 的父级或更上级元素)进行定位。如果没有已定位的祖先,absolute 会相对于 <body> 定位,而 fixed 会相对于浏览器视口定位。因此,最常见的实践是将父元素设置为 relative,子元素设置为 absolute。
类名 CSS 等效 说明 staticposition: static;默认值,元素遵循正常的文档流 relativeposition: relative;最常用 。为绝对定位的子元素创建定位上下文absoluteposition: absolute;元素完全脱离文档流,相对于最近的已定位祖先定位 fixedposition: fixed;元素相对于浏览器视口定位,滚动页面时位置不变 stickyposition: sticky;粘性定位,在滚动到特定位置前表现为 relative,之后表现为 fixed
代码范例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > </head > <body > <div class ="relative w-48 h-48 bg-gray-200" > <p > 父元素 (relative)</p > <div class ="absolute bottom-0 right-0 w-24 h-24 bg-blue-400" > 子元素 (absolute) </div > </div > </body > </html >
1.5.2 媒体元素适配与定位 当处理 <img> 或 <video> 等媒体元素时,我们常常需要控制它们如何填充其容器,以避免图像拉伸或变形。object-fit 用于控制适配方式,而 object-position 则用于控制元素在容器中的对齐位置。
类名 说明 object-contain保持宽高比 ,完整显示图像,可能会在容器中留下空白object-cover最常用 。保持宽高比 ,填满整个容器,可能会裁剪掉部分图像object-fill拉伸图像以填满容器,不保持宽高比 ,可能导致图像变形 object-none保持图像原始尺寸,可能会被容器裁剪
代码范例:
例如很常见的卡片,图片部分被包裹在容器内,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > </head > <body > <div class ="max-w-sm mx-auto bg-white rounded-lg shadow-md overflow-hidden" > <img src ="https://picsum.photos/200/300" alt ="Card image" class ="w-full h-48 object-cover" > <div class ="p-6" > <h2 class ="text-xl font-semibold mb-2" > Card Title</h2 > <p class ="text-gray-600" > This is a simple card with an image and some text content.</p > </div > </div > </body > </html > =
1.5.4 z-index (层叠顺序) z-index 用于控制 已定位元素 的堆叠顺序。拥有更高 z-index 值的元素会显示在拥有较低值的元素之上。
核心注意点:
z-index 只对设置了 position 属性(static 除外)的元素生效 。不同的 堆叠上下文(Stacking Context) 会影响 z-index 的表现,父元素的 z-index 会限制其所有子元素的层级。 类名 CSS 等效 说明 z-0z-index: 0;与默认层级一致 z-10, z-20…z-index: 10;正数值,数值越大,层级越高 -z-10z-index: -10;负数值,可用于将元素置于其他元素之后 z-autoz-index: auto;默认值 z-[100]z-index: 100;使用方括号来自定义任意 z-index 值
代码范例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Document</title > <script src ="https://cdn.tailwindcss.com" > </script > </head > <body > <div class ="relative h-24" > <div class ="absolute inset-0 w-32 h-16 bg-red-300 z-10" > z-10</div > <div class ="absolute top-4 left-4 w-32 h-16 bg-blue-300 z-20" > z-20</div > </div > </body > </html >
1.6 间距 间距是构建布局的基石,它定义了元素与其内容之间(padding)以及元素与元素之间(margin)的距离。Tailwind 提供了一套精细且一致的间距工具,帮助我们快速构建和谐、平衡的界面。
1.6.1 Padding (内边距) Padding 用于控制元素 内部 的留白,即元素边框与其内容之间的空间。它常用于增加按钮的点击区域、让文本不紧贴容器边缘,从而提升可读性和美观度。
Tailwind 的 padding 工具遵循一套逻辑命名法:
p-{size}: 设置 所有四个方向 的内边距。px-{size}: 仅设置 水平方向 (左、右)的内边距。py-{size}: 仅设置 垂直方向 (上、下)的内边距。pt-{size}, pr-{size}, pb-{size}, pl-{size}: 分别设置 上、右、下、左 四个单边的内边距。常用类名 说明 CSS 等效示例 p-4在所有方向设置 1rem (16px) 的内边距 padding: 1rem;px-6在水平方向设置 1.5rem (24px) 的内边距 padding-left: 1.5rem; padding-right: 1.5rem;py-2在垂直方向设置 0.5rem (8px) 的内边距 padding-top: 0.5rem; padding-bottom: 0.5rem;pt-8在顶部设置 2rem (32px) 的内边距 padding-top: 2rem;p-[10px]使用方括号自定义一个 10px 的内边距 padding: 10px;
代码范例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Padding Example</title > <script src ="https://cdn.tailwindcss.com" > </script > </head > <body class ="bg-slate-100 p-10" > <button class ="bg-blue-500 text-white font-bold rounded" > 没有 Padding </button > <button class ="bg-blue-500 text-white font-bold py-2 px-4 rounded mt-4" > 有 Padding (py-2 px-4) </button > </body > </html >
1.6.2 Margin (外边距) Margin 用于控制元素 外部 的间距,即元素边框与其他元素之间的距离。它是布局中控制元素位置和间距的主要手段。
Margin 的工具类与 Padding 命名规则完全相同,但增加了 负值 和 自动外边距 的特殊用法。
命名规则 : m-{size}, mx-{size}, my-{size}, mt-{size} 等。水平居中 : mx-auto 是一个非常重要的工具,它可以让一个具有固定宽度的块级元素在其父容器中 水平居中 。负外边距 : 在类名前添加负号 - (例如 -mt-4) 可以创建负外边距,常用于创建元素间的重叠效果。常用类名 说明 CSS 等效示例 m-4在所有方向设置 1rem (16px) 的外边距 margin: 1rem;mx-auto最常用 。自动计算水平外边距,实现块级元素水平居中margin-left: auto; margin-right: auto;my-8在垂直方向设置 2rem (32px) 的外边距 margin-top: 2rem; margin-bottom: 2rem;mb-6在底部设置 1.5rem (24px) 的外边距 margin-bottom: 1.5rem;-mt-4在顶部设置 -1rem (-16px) 的负外边距,使元素向上移动 margin-top: -1rem;
代码范例 1:水平居中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Margin Centering</title > <script src ="https://cdn.tailwindcss.com" > </script > </head > <body class ="bg-slate-100 p-4" > <div class ="bg-gray-300 h-24" > <div class ="w-64 h-24 bg-sky-500 text-white flex items-center justify-center mx-auto" > w-64 mx-auto </div > </div > </body > </html >
代码范例 2:负外边距重叠效果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Negative Margin</title > <script src ="https://cdn.tailwindcss.com" > </script > </head > <body class ="p-10" > <div class ="flex" > <div class ="w-16 h-16 rounded-full bg-blue-500 z-10" > </div > <div class ="w-16 h-16 rounded-full bg-red-500 -ml-4" > </div > </div > </body > </html >
1.7 尺寸 尺寸工具用于精确控制元素的宽度(width)和高度(height)。这是所有布局的基础,从定义一个小按钮的尺寸到构建整个页面的宏观结构,都离不开尺寸的设定。Tailwind 提供了从固定值、百分比到基于视口的多种尺寸单位,能够灵活应对各种响应式设计需求。
1.7.1 宽度 (Width) 宽度(width)用于设定元素的水平尺寸。Tailwind 的宽度工具非常丰富,可以满足固定布局和流式布局的需求。
固定宽度 (w-<number>) : 基于预设的间距比例,适用于尺寸固定的组件,如图标、头像、特定大小的卡片等。百分比宽度 (w-<fraction>) : 基于父容器的百分比,是构建响应式网格和流式布局的核心。w-full (100%) 是最常用的类之一。视口宽度 (w-screen) : 使元素的宽度等于浏览器视口的宽度,常用于创建通栏横幅或全屏背景。常用类名 说明 CSS 等效示例 w-64固定宽度,16rem (256px) width: 16rem;w-1/2百分比宽度,占据父容器的 50% width: 50%;w-full最常用 。占据父容器的 100%width: 100%;w-auto宽度由浏览器根据内容自动计算 width: auto;w-screen宽度等于视口宽度 width: 100vw;w-[300px]使用方括号自定义一个 300px 的宽度 width: 300px;
代码范例:创建一个响应式的两列布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Width Example</title > <script src ="https://cdn.tailwindcss.com" > </script > </head > <body class ="p-4 bg-slate-50" > <div class ="flex bg-white p-4 rounded-lg shadow space-x-4" > <div class ="w-1/3 bg-sky-100 p-4 rounded" > Sidebar (w-1/3)</div > <div class ="w-2/3 bg-sky-200 p-4 rounded" > Main Content (w-2/3)</div > </div > </body > </html >
1.7.2 高度 (Height) 高度(height)用于设定元素的垂直尺寸。需要注意的是,百分比高度 (h-full 或 h-1/2 等) 依赖于其父元素具有明确的高度定义 。如果父元素的高度是由内容撑开的(默认情况),那么子元素的百分比高度将不会生效。
h-screen : 一个极其有用的类,它将元素的高度设置为整个视口的高度,非常适合创建全屏的封面、英雄区或单页应用的主视图。常用类名 说明 CSS 等效示例 h-48固定高度,12rem (192px) height: 12rem;h-full占据父容器的 100% 高度(父元素需有定高 ) height: 100%;h-auto高度由浏览器根据内容自动计算 height: auto;h-screen最常用 。高度等于视口高度height: 100vh;h-[80%]使用方括号自定义一个 80% 的高度 height: 80%;
代码范例:创建一个占满整个屏幕的欢迎页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Height Example</title > <script src ="https://cdn.tailwindcss.com" > </script > </head > <body > <div class ="h-screen bg-gradient-to-r from-cyan-500 to-blue-500 flex items-center justify-center" > <h1 class ="text-white text-4xl font-bold" > Welcome!</h1 > </div > </body > </html >
1.7.3 最小/最大尺寸 (Min/Max Sizing) 最小和最大尺寸类为我们的布局提供了重要的 约束 ,使其在不同屏幕尺寸下表现得更加健壮和可预测。
max-w-{size} (最大宽度) : 极其常用 。用于限制容器的最大宽度,尤其是在大屏幕上,可以防止文本行过长导致难以阅读。常用于文章、博客或任何以内容为主的页面。min-h-screen (最小高度) : 极其常用 。确保一个容器(如整个页面布局)的高度至少占满整个屏幕。这可以有效防止在内容较少时,页脚(footer)悬浮在页面中间的尴尬情况。常用类名 说明 CSS 等效示例 max-w-md限制元素最大宽度为 28rem (448px) max-width: 28rem;max-w-screen-lg限制元素最大宽度与大屏幕断点一致 (1024px) max-width: 1024px;min-h-screen确保元素最小高度为整个视口高度 min-height: 100vh;max-h-96限制元素最大高度为 24rem (384px),常用于可滚动的面板 max-height: 24rem;
代码范例:一个居中且有最大宽度限制的文章容器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Max-Width Example</title > <script src ="https://cdn.tailwindcss.com" > </script > </head > <body class ="bg-gray-100" > <article class ="mx-auto max-w-3xl bg-white p-8 my-8 rounded-lg shadow" > <h1 class ="text-3xl font-bold mb-4" > Article Title</h1 > <p class ="text-gray-700 leading-relaxed" > This container is centered using 'mx-auto' and its maximum width is constrained by 'max-w-3xl'. This prevents the text from becoming too wide on large screens, which significantly improves readability... </p > </article > </body > </html >
1.8 文字排版 排版是用户界面设计的灵魂。良好的排版能显著提升信息的可读性、层次感和整体美感。Tailwind 提供了一整套原子化的排版工具,让我们可以像搭积木一样,精确地构建出任何想要的文本样式。
1.8.1 核心字体样式 (Family, Size & Weight) 这是定义文本外观最基础的三个属性:字体、大小和粗细。
font-family (字体系列) : Tailwind 预设了三种通用字体栈:font-sans (无衬线体,UI首选), font-serif (衬线体), 和 font-mono (等宽字体,代码首选)。font-size (字体大小) : text-{size} 类提供了一套从 xs (特小) 到 9xl (巨大) 的完整尺寸体系。text-base 是默认的基础尺寸,通常对应 16px。font-weight (字体粗细) : font-{weight} 类覆盖了从 thin (100) 到 black (900) 的标准字重。最常用的是 font-normal (400) 和 font-bold (700)。常用类名 说明 CSS 等效示例 font-sans无衬线字体,适用于绝大多数UI文本 font-family: ui-sans-serif, system-ui...text-base基础/默认字体大小 (16px) font-size: 1rem;text-lg较大文本 (18px) font-size: 1.125rem;text-2xl2倍大号文本,常用于标题 (24px) font-size: 1.5rem;font-normal正常粗细 (400) font-weight: 400;font-semibold半粗 (600),常用于次级标题或强调 font-weight: 600;font-bold粗体 (700),常用于主标题 font-weight: 700;
代码范例:一个标准的文章标题和段落
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Typography Basics</title > <script src ="https://cdn.tailwindcss.com" > </script > </head > <body class ="p-8 font-sans" > <h1 class ="text-3xl font-bold text-slate-900" > The Foundation of Great Design </h1 > <p class ="mt-4 text-base font-normal text-slate-700" > Typography is more than just choosing beautiful fonts; it's a critical component of user interface design that ensures readability and accessibility. </p > </body > </html >
1.8.2 文本颜色与对齐 (Color & Alignment) text-color (文本颜色) : 使用 text-{color}-{shade} 格式,例如 text-red-500。Tailwind 的调色板非常丰富。一个重要的技巧是使用斜杠 / 来指定透明度,如 text-blue-500/75 表示 75% 的不透明度。text-align (文本对齐) : 控制文本的水平对齐方式。text-center 是除了默认的 text-left 之外最常用的。常用类名 说明 CSS 等效示例 text-slate-700设置文本为 slate 颜色的 700 色号 color: rgb(51 65 85);text-blue-500/75设置文本为 50% 透明度的蓝色 color: rgb(59 130 246 / 0.75);text-left左对齐 (默认) text-align: left;text-center居中对齐 text-align: center;text-right右对齐 text-align: right;
1.8.3 行高与间距 (Line Height & Spacing) line-height (行高) : leading-{size} 类用于控制文本行与行之间的垂直距离。leading-relaxed (宽松行高) 常用于大段文本,以提高可读性。letter-spacing (字母间距) : tracking-{size} 类用于调整字符之间的水平间距,可用于细微的美学调整。常用类名 说明 CSS 等效示例 leading-normal正常行高 (1.5) line-height: 1.5;leading-relaxed宽松行高 (1.625) line-height: 1.625;tracking-tight紧凑的字母间距 letter-spacing: -0.025em;tracking-wide较宽的字母间距 letter-spacing: 0.025em;
代码范例:一段行高更舒适的居中文本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Line Height & Align</title > <script src ="https://cdn.tailwindcss.com" > </script > </head > <body class ="p-8" > <blockquote class ="text-center max-w-2xl mx-auto" > <p class ="text-xl text-gray-800 leading-relaxed" > "Good design is obvious. Great design is transparent. When it's done well, you don't even notice it; it just feels right." </p > </blockquote > </body > </html >
text-decoration (文本装饰) : 主要用于链接 (<a> 标签)。underline 添加下划线,no-underline 移除下划线。常与 hover: 状态结合使用,创造交互效果。text-transform (文本变换) : 改变文本的大小写。uppercase (全大写) 常用于按钮或标签,capitalize (首字母大写) 常用于标题。常用类名 说明 CSS 等效示例 underline添加下划线 text-decoration-line: underline;no-underline移除下划线 text-decoration-line: none;hover:underline悬停时添加下划线 :hover { ... }uppercase文本全部转为大写 text-transform: uppercase;capitalize每个单词的首字母大写 text-transform: capitalize;
代码范例:一个交互式链接和一个大写按钮
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Decoration & Transform</title > <script src ="https://cdn.tailwindcss.com" > </script > </head > <body class ="p-8 space-y-6" > <a href ="#" class ="text-blue-600 no-underline hover:underline" > Interactive Link </a > <button class ="bg-slate-800 text-white font-semibold py-2 px-4 rounded uppercase" > Submit </button > </body > </html >
1.8.5 文本溢出处理 (Overflow) 当文本内容超出其容器时,我们需要优雅地处理它。
truncate (单行截断) : 这是一个极其常用 的复合工具类。它将超出单行的文本截断,并显示省略号 (…)。它实际上是 overflow-hidden + text-ellipsis + whitespace-nowrap 的组合。line-clamp-{n} (多行截断) : 当你需要限制文本显示为特定行数(如2行或3行)并显示省略号时,使用这个类。类名 说明 truncate将文本限制在单行内,超出部分用省略号表示 line-clamp-2将文本限制在两行内,超出部分用省略号表示 line-clamp-3将文本限制在三行内,超出部分用省略号表示
代码范例:卡片中标题和描述的文本截断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" /> <meta name ="viewport" content ="width=device-width, initial-scale=1.0" /> <title > Text Overflow</title > <script src ="https://cdn.tailwindcss.com" > </script > </head > <body class ="bg-slate-100 p-8" > <div class ="max-w-xs bg-white p-4 rounded-lg shadow" > <h3 class ="font-bold truncate" > This is a Very Long Card Title That Will Be Truncated </h3 > <p class ="mt-2 text-sm text-gray-600 line-clamp-2" > This is a longer description for the card. If the content exceeds two lines of text, it will be elegantly clamped with an ellipsis at the end to maintain a consistent layout. </p > </div > </body > </html >