Tailwind 02 布局

Huy大约 7 分钟CSSCSS

Tailwind 布局

本节梳理记录一些 Tailwind 常用的布局书写方式。

内容较多,无需一次性全部记忆,但要有所印象。

总结规律

  • box-sizing 盒子模型:盒子模型以 box 开头表示盒子模型,而后跟随盒子模型的边界,如box-border表示以 border 为边界的盒子模型,对应box-sizing: border-box;;
  • display 布局:除 display: none 的类名为 hidden 外,其余类名基本为 display 的取值本身。
  • float 浮动:直接用 - 将 float 和取值进行连接即可,如 float-left 类名表示 float: left;
  • position 定位:position 的取值本身;
  • 定位位置:通用公式为 {top|right|bottom|left|inset}-{number},若是负值,减号在类名前面;
  • visibility 可见性:取值本身,直接写 visibleinvisible
  • z-index 层叠顺序:通用公式为z-{indexNumber},若为负则负号在前;
  • flex 弹性盒子:
    • 整体方向和是否换行,通用公式为 flex-{direction|wrap}
    • content 的整体对齐方向布局,通用公式为 {justify|content}-{value} 分别表示 justify-content 水平方向整体布局和 align-content 垂直方向整体布局;
    • items 的单行沿容器垂直对齐方向,通用公式为 item-{value}
    • align 的单个子项沿容器的垂直对齐方向,通用公式为 align-{value}
    • flex 弹性缩放属性,通用公式为 flex-{initial|0|none|1|auto}

容器

容器同上一小节中介绍的断点是一样的,这里同布局的内容更贴切,因此再做总结。

ClassBreakpointProperties
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;
  • 容器居中:mx-auto

  • 添加水平内边距:px-{size}

  • 响应式变体,如只在某个断点上表现出差异:

    <!-- Full-width fluid until the `md` breakpoint, then lock to container -->
    <div class="md:container md:mx-auto">
      <!-- ... -->
    </div>
    
  • 自定义容器在默认情况下居中。通过配置 theme.container 部分将 center 设置为 true

    // tailwind.config.js
    module.exports = {
      theme: {
        container: {
          center: true,
        },
      },
    }
    

Box Sizing 盒子模型

设置盒子类型会和原生有一点不同,由 box 开头表示盒子模型,而后跟随盒子模型的边界,如box-border表示以 border 为边界的盒子模型:

ClassProperties
box-borderbox-sizing: border-box;
box-contentbox-sizing: content-box;

Display 常用布局

display: nonehidden 外,其余基本为取值本身。

ClassProperties
blockdisplay: block;
inline-blockdisplay: inline-block;
inlinedisplay: inline;
flexdisplay: flex;
inline-flexdisplay: inline-flex;
tabledisplay: table;
inline-tabledisplay: inline-table;
table-captiondisplay: table-caption;
table-celldisplay: table-cell;
table-columndisplay: table-column;
table-column-groupdisplay: table-column-group;
table-footer-groupdisplay: table-footer-group;
table-header-groupdisplay: table-header-group;
table-row-groupdisplay: table-row-group;
table-rowdisplay: table-row;
flow-rootdisplay: flow-root;
griddisplay: grid;
inline-griddisplay: inline-grid;
contentsdisplay: contents;
list-itemdisplay: list-item;
hiddendisplay: none;

float 浮动

浮动在现代布局中用的较少了,但是也有其存在的必要。浮动较为好记,直接用 - 对 float 的取值进行连接即可。

ClassProperties
float-rightfloat: right;
float-leftfloat: left;
float-nonefloat: none;

有浮动,自然也有清除浮动,记忆方法同上,但一般都是用 clear-both

ClassProperties
clear-leftclear: left;
clear-rightclear: right;
clear-bothclear: both;
clear-noneclear: none;

Position 定位

Position 定位非常便捷,直接书写 potion 的值即可:

ClassProperties
staticposition: static;
fixedposition: fixed;
absoluteposition: absolute;
relativeposition: relative;
stickyposition: sticky;

Top / Right / Bottom / Left

控制定位元素的位置,这个类名较多一般是根据元素值去查表,推荐 VScode 插件来进行书写。

通用公式:{top|right|bottom|left|inset}-{number}

若是负值,减号在类名前面:-{top|right|bottom|left|inset}-{number|string}

visibility 可见性

同定位元素一样,直接书写 visibility 的值:

ClassProperties
visiblevisibility: visible;
invisiblevisibility: hidden;

区别于 display: none,DOM 元素依旧会存在但是不显示。

z-index 层叠顺序

层叠顺序也较好记忆:z-{index}

flex 弹性盒子

如上文 Position 所写一样,首先父元素要先设置为 fixed,变为弹性盒子。

弹性盒子共分为俩部分,父元素控制整体布局,子元素控制元素细节。可见 《弹性布局 Flex》

父元素整体布局设置

先回顾一下原先到布局设置:

  1. flex-direction 属性与整体布局方向。

    flex-direction: row | row-reverse | column | column-reverse;
    
  2. flex-wrap 属性与整体布局的换行表现。

    flex-wrap: nowrap | wrap | wrap-reverse;
    
  3. flex-flow 属性是 flex-direction 和 flex-wrap 的缩写。

  • 在 Tailwind 中 flex 控制子项的方向 flex-direction 变化较大,直接省略了 direction 转而拼接值;此外 column 也采用缩写 col。通用公式为flex-{direction}

    ClassProperties
    flex-rowflex-direction: row;
    flex-row-reverseflex-direction: row-reverse;
    flex-colflex-direction: column;
    flex-col-reverseflex-direction: column-reverse;
  • 控制整体是否换行 flex-wrap 同 direction 一样是略去 wrap 关键词直接拼接值。通用公式为flex-{wrap}

    ClassProperties
    flex-wrapflex-wrap: wrap;
    flex-wrap-reverseflex-wrap: wrap-reverse;
    flex-nowrapflex-wrap: nowrap;

对齐特性

实际上,Tailwind 对齐这一部分同 Grid 网格布局是同享的,先看原先对齐的几个属性值:

  1. justify-content 属性与整体布局的水平对齐

    justify-content: normal | flex-start | flex-end | center | space-between |
      space-around | space-evenly;
    
  2. 垂直对齐属性 align-itemsalign-self

    区别: align-self属性是设置在具体的某一个 flex 子项上的,而align-items属性是设置在 flex 容器元素上的,控制所有 flex 子项的垂直对齐方式。

    align-items: stretch | flex-start | flex-end | center | baseline;
    
    align-self: auto | stretch | flex-start | flex-end | center | baseline;
    
    • autoalign-self 属性的默认值,表示 flex 子项的垂直对齐 方式是由 flex 容器的 align-items 属性值决定的。
    • stretch可以看成弹性布局中align-items 属性的默认值,表示 flex 子项在垂直方向上拉伸。
  3. align-content 属性与整体布局的垂直对齐

    区别: align-content 属性和 align-items 属性的区别在于 align-items 属性设置的是每一个 flex 子项的垂直对齐方式,而 align-content 属性将 所有 flex 子项作为一个整体进行垂直对齐设置

    align-content: stretch | flex-start | flex-end | center | space-between |
      space-around | space-evenly;
    
  4. order 属性与单个子项的顺序控制

    order: <integer>; /* 整数值,默认值是 0 */
    

在 Tailwind 中有如下变动:

  • justify-content 用于 flex 和 grid 沿容器整体布局的水平对齐方向:

    ClassProperties
    justify-startjustify-content: flex-start;
    justify-endjustify-content: flex-end;
    justify-centerjustify-content: center;
    justify-betweenjustify-content: space-between;
    justify-aroundjustify-content: space-around;
    justify-evenlyjustify-content: space-evenly;
  • align-content 用于 flex 和 grid 多行沿容器整体布局的垂直对齐方向:

    ClassProperties
    content-centeralign-content: center;
    content-startalign-content: flex-start;
    content-endalign-content: flex-end;
    content-betweenalign-content: space-between;
    content-aroundalign-content: space-around;
    content-evenlyalign-content: space-evenly;
  • align-items 用于 flex 和 grid 各单行沿容器整体布局的垂直对齐方向,通用公式为 item-{value};:

    ClassProperties
    items-startalign-items: flex-start;
    items-endalign-items: flex-end;
    items-centeralign-items: center;
    items-baselinealign-items: baseline;
    items-stretchalign-items: stretch;
  • align-self 用于 flex 和 grid 单个子项沿容器的垂直对齐方向,通用公式为 align-{value}

    ClassProperties
    self-autoalign-self: auto;
    self-startalign-self: flex-start;
    self-endalign-self: flex-end;
    self-centeralign-self: center;
    self-stretchalign-self: stretch;
    self-baselinealign-self: baseline;

flex 属性

flex 属性是 flex-grow、flex-shrink 和 flex-basis 这 3 个缩放属性的缩写。

  • flex:initial 等同于 flex: 0 1 auto;,作用为 flex 初始值;
  • flex:0 等同于 flex: 0 1 0%;,元素尺寸会收缩但不会扩展,作用为元素尺寸表现为最小内容宽度;
  • flex:none 等同于 flex:0 0 auto; , 作用为 flex 子项没有弹性, 设置为固定尺寸元素(无需设置width属性),元素最终尺寸通常表现为最大内容宽度。
  • flex:1 等同于 flex: 1 1 0%;,作用为 flex 子项自动填满剩余空间或自动收缩,在容器尺寸不足时会优先最小化内容的尺寸
  • flex:auto 等同于 flex: 1 1 auto;, 作用为 flex 子项自动填满剩余空间或自动收缩,在容器尺寸不足时会优先最大化内容尺寸;

完整属性缩写介绍对照表,具体使用可见 《弹性布局 Flex》

ClassabbreviationProperties
flex-initialflex: initial;flex: 0 1 auto;
flex-0flex: 0;flex: 0 1 0%;
flex-noneflex: none;flex: 0 0 auto;
flex-1flex: 1;flex: 1 1 0%;
flex-autoflex: auto;flex: 1 1 auto;

flex-grow 扩展属性

ClassProperties
flex-grow-0flex-grow: 0;
flex-growflex-grow: 1;

flex-shrink 收缩属性

ClassProperties
flex-shrink-0flex-shrink: 0;
flex-shrinkflex-shrink: 1;
Loading...