tailwindcss(类似内联样式)

前言

​ 使用后的感受是它和内联样式的使用基本相似,除了一些名词的拼写(内联样式:background: skyblue; tailwindcss:bg-sky-200; ),最主要的是它比内联样式更好的特点:可以写鼠标悬停,光标聚焦等样式,可以写响应式布局,直接写在class中就行...虽然对比内联样式有很多好的地方,但是在开发中不推荐使用:在一个项目中,有多个人共同开发,你使用tailwindcss写的样式,别人在没接触过tailwindcss看你的代码时会非常麻烦,特别是由其他人修改、调整你绘制的页面样式时非常的吃力,而且一些相同的样式大量重复的编写也不好,对于后期的维护也不方便,因此在没有统一的沟通使用某种样式时,最好将css抽取出来,方便调整,修改,维护

安装引入

安装 Tailwind CSS

tailwindcss通过 npm安装,并创建您的tailwind.config.js 文件。

npm install -D tailwindcss
npx tailwindcss init

配置您的模板路径

在文件中添加所有模板文件的路径tailwind.config.js

module.exports = {
  content: ["./src/**/*.{html,js}"],
  theme: {
    extend: {},
  },
  plugins: [],
}

将 Tailwind 指令添加到您的 CSS

@tailwind将Tailwind 的每个层的指令添加到您的主 CSS 文件中。

@tailwind base;
@tailwind components;
@tailwind utilities;

启动 Tailwind CLI 构建过程

运行 CLI 工具来扫描您的模板文件中的类并构建您的 CSS。

npx tailwindcss -i ./src/input.css -o ./dist/output.css --watch

开始使用 Tailwind

将已编译的 CSS 文件添加到<head>并开始使用 Tailwind 的实用程序类来设置您的内容样式。

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="/dist/output.css" rel="stylesheet">
</head>
<body>
  <h1 class="text-3xl font-bold underline">
    Hello world!
  </h1>
</body>
</html>

常用配置

  1. 响应式:

    sm640像素@media (min-width: 640px) { ... }
    md768像素@media (min-width: 768px) { ... }
    lg1024像素@media (min-width: 1024px) { ... }
    xl1280像素@media (min-width: 1280px) { ... }
    2xl1536像素@media (min-width: 1536px) { ... }
<img class="w-16 md:w-32 lg:w-48" src="...">
  1. Display:
blockdisplay: block;
inline-blockdisplay: inline-block;
inlinedisplay: inline;
flexdisplay: flex;
hiddendisplay: none;
<div class="flex items-center">
  1. Floats:浮动
float-rightfloat: right;
float-leftfloat: left;
float-nonefloat: none;
  1. Clear:清除
clear-leftclear: left;
clear-rightclear: right;
clear-bothclear: both;
clear-noneclear: none;
  1. Flex Direction:设置主轴的方向
flex-rowflex-direction: row;
flex-row-reverseflex-direction: row-reverse;
flex-colflex-direction: column;
flex-col-reverseflex-direction: column-reverse;
  1. Flex Wrap:设置子元素是否换行
flex-wrapflex-wrap: wrap;
flex-wrap-reverseflex-wrap: wrap-reverse;
flex-nowrapflex-wrap: nowrap;
  1. Justify Content:设置主轴上的子元素排列方式
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;
  1. Justify Items:子元素以默认方式沿适当轴线对齐到每个盒子
justify-items-startjustify-items: start;
justify-items-endjustify-items: end;
justify-items-centerjustify-items: center;
justify-items-stretchjustify-items: stretch;
  1. Align Content:设置侧轴上的子元素的排列方式(多行)
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;
  1. Align Items:设置侧轴上的子元素的排列方式(单行)
items-startalign-items: flex-start;
items-endalign-items: flex-end;
items-centeralign-items: center;
items-baselinealign-items: baseline;
items-stretchalign-items: stretch;
  1. Width:宽
w-0width: 0px;
w-pxwidth: 1px;
w-0.5width: 0.125rem; / 2px /
w-1width: 0.25rem; / 4px /
w-1.5width: 0.375rem; / 6px /
w-2width: 0.5rem; / 8px /
w-2.5width: 0.625rem; / 10px /
w-3width: 0.75rem; / 12px /
w-autowidth: auto;
w-1/2width: 50%;
w-1/3width: 33.333333%;
w-2/3width: 66.666667%;
w-1/4width: 25%;
w-fullwidth:100%
  1. Min-Width:最小宽度
min-w-0min-width: 0px;
min-w-fullmin-width: 100%;
min-w-minmin-width: min-content;
min-w-maxmin-width: max-content;
min-w-fitmin-width: fit-content;
  1. Max-Width:最大宽度
max-w-0max-width: 0rem; / 0px /
max-w-nonemax-width: none;
max-w-xsmax-width: 20rem; / 320px /
max-w-smmax-width: 24rem; / 384px /
max-w-mdmax-width: 28rem; / 448px /
max-w-lgmax-width: 32rem; / 512px /
max-w-xlmax-width: 36rem; / 576px /
max-w-2xlmax-width: 42rem; / 672px /
  1. Height:高

和宽配置相同

  1. Min-Height:最小高度

....

  1. Max-Height:最大高度

....

  1. Padding:内边距
p-0padding: 0px;
px-0padding-left: 0px; padding-right: 0px;
py-0padding-top: 0px; padding-bottom: 0px;
pt-0padding-top: 0px;
pb-0padding-bottom: 0px;
pl-0padding-left: 0px;
pr-0padding-right: 0px;
p-[1px]padding: 1px
  1. Margin:外边距
m-0margin: 0px;
mx-0margin-left: 0px; margin-right: 0px;
my-0margin-top: 0px; margin-bottom: 0px;
mt-0margin-top: 0px;
mr-0margin-right: 0px;
mb-0margin-bottom: 0px;
ml-0margin-left: 0px;
m-[1px]margin: 1px;
  1. Space Between:控制子元素之间空间距离
space-x-0 > + margin-left: 0px;
space-y-0 > + margin-top: 0px;
space-x-0.5 > + margin-left: 0.125rem; / 2px /
space-{x\y}-[2px]margin-left: 0.125rem; / 2px /
  1. Font Size:字体大小
text-xsfont-size: 0.75rem; / 12px / line-height: 1rem; / 16px /
text-smfont-size: 0.875rem; / 14px / line-height: 1.25rem; / 20px /
text-basefont-size: 1rem; / 16px / line-height: 1.5rem; / 24px /
text-lgfont-size: 1.125rem; / 18px / line-height: 1.75rem; / 28px /
text-xlfont-size: 1.25rem; / 20px / line-height: 1.75rem; / 28px /
  1. Font Style:字体样式
italicfont-style: italic;
not-italicfont-style: normal;
  1. Font Weight:字体粗细
font-thinfont-weight: 100;
font-mediumfont-weight: 500;
font-semiboldfont-weight: 600;
font-boldfont-weight: 700;
font-extraboldfont-weight: 800;
font-blackfont-weight: 900;
  1. Text Color:文本颜色
text-[#000000]color: #000000
text-inheritcolor: inherit;
text-currentcolor: currentColor;
  1. Background Color:背景颜色
bg-[#000000]background-color: #000000;
bg-inheritbackground-color: inherit;
bg-currentbackground-color: currentColor;
  1. 修饰词
  • 伪类, :hover, :focus,:first-child:required
  • 伪元素, ::before, ::after, ::placeholder, 和::selection
  • 媒体查询,例如响应式断点、暗模式和prefers-reduced-motion
  • 属性选择器,例如[dir="rtl"][open]
<button class="bg-sky-600 hover:bg-sky-700 ...">
  Save changes
</button>

<button class="bg-violet-500 hover:bg-violet-600 active:bg-violet-700 focus:outline-none focus:ring focus:ring-violet-300 ...">
  Save changes
</button>
最后修改:2023 年 06 月 13 日
如果觉得我的文章对你有用,请随意赞赏