自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

為什么Tailwindcss在開發(fā)者中如此受歡迎?揭秘背后的原因!

開發(fā) 前端
在原子化 CSS 中,CSS 組件被拆分為更小的部分,這些部分可以獨(dú)立地編輯、測(cè)試和重用。這些原子通常是單個(gè)像素或極其微小的變化,例如顏色、大小、位置等。

1.邂逅 tailwindcss

我們平時(shí)寫 css 樣式是這樣的:

<template>
  <div class="zhifou">
    <p>好好學(xué)習(xí)</p>
    <p>天天向上</p>
  </div>
</template>
<script setup></script>
<style lang="scss" scoped>
.zhifou {
  margin: auto;
  width: 600px;
  height: 300px;
  background-color: blue;
  font-size: 20px;
}
</style>

后來(lái)隨著前端技術(shù)的發(fā)展,原子化 CSS 出現(xiàn)了。原子化 CSS 是一種 CSS 框架。

在原子化 CSS 中,CSS 組件被拆分為更小的部分,這些部分可以獨(dú)立地編輯、測(cè)試和重用。這些原子通常是單個(gè)像素或極其微小的變化,例如顏色、大小、位置等。

原子化 CSS 有助于減少代碼量,提高代碼的可維護(hù)性和可重用性。

原子化 CSS 寫法:

<div class="w-10 h-10 bg-red-100 text-10">
    <p>好好學(xué)習(xí)</p>
    <p>天天向上</p>
  </div>

原子化 CSS 框架更像是一個(gè)已經(jīng)封裝好的 CSS 工具類。

例如:我們?cè)陬愡x擇器中寫了 w-[10px],原子化 CSS 框架經(jīng)過(guò)掃描,將 w-[10px] 掃描成

width:10px;

也就是說(shuō),我們只要按照這個(gè)框架的要求去任意組合,框架最后一掃描,就能生成我們想要的 CSS 樣式。這樣會(huì)大大減少代碼量,提高工作效率。

而本文介紹的 tailwindcss 就是市面上非常熱門的原子化 CSS 框架。

tailwindcss 中文網(wǎng)

https://www.tailwindcss.cn/

圖片圖片

2.Vite 安裝配置 tailwindcss

2.1 安裝 tailwindcss

npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

其中第一行命令會(huì)安裝 tailwindcss 的依賴

第二行命令會(huì)創(chuàng)建 tailwindcss 配置文件,包含 postcss.config.js 和 tailwind.config.js 文件。

postcss.config.js 主要用來(lái)給項(xiàng)目中添加 tailwindcss 的插件。

圖片圖片

tailwind.config.js 主要用來(lái)配置 tailwindcss 的掃描規(guī)則、設(shè)置主題等。

圖片圖片

2.2 配置 tailwind.config.js

圖片圖片

/** @type {import('tailwindcss').Config} */
export default {
  content: [
    "./index.html",
    "./src/**/*.{vue,js,ts,jsx,tsx}",],
  theme: {
    extend: {},
  },
  plugins: [],
}

2.3 添加 tailwindcss 的基本指令

新建樣式文件,在 main.js 中導(dǎo)入該文件

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

圖片圖片

圖片圖片

3. 編輯器安裝 tailwindcss 輔助插件

這里我們使用的編輯器是 VScode。

新手剛開始用 tailwindcss 時(shí),需要不斷從官網(wǎng)查詢相關(guān)原子類的寫法,這樣太繁瑣。

安裝插件之后,編輯器就能智能提示了,非常的方便

圖片圖片

圖片圖片

4. tailwindcss 常用方法

4.1 設(shè)置寬高

1.w-[ ],h-[ ] 設(shè)置任意寬高

<template>
  <div>
    <div class="bg-blue-600 w-[200px] h-[100px]">
      <p>好好學(xué)習(xí)</p>
      <p>天天向上</p>
    </div>
    <div class="bg-red-600 w-[20rem] h-[10rem]">
      <p>好好學(xué)習(xí)</p>
      <p>天天向上</p>
    </div>
  </div>
</template>
<script setup></script>
<style lang="scss" scoped></style>

圖片圖片

2.w-1/2 設(shè)置比例

<div class="bg-red-600 w-1/2">
  <p>好好學(xué)習(xí)</p>
  <p>天天向上</p>
</div>

3.占滿寬度和高度

w-full:占滿父容器的寬度

h-full:占滿父容器的高度

w-screen:占滿整個(gè)屏幕的寬度

h-screen:占滿整個(gè)屏幕的高度

<template>
  <div class="w-[400px]">
    <div class="bg-red-600 w-full">
      <p>好好學(xué)習(xí)</p>
      <p>天天向上</p>
    </div>
    <div class="bg-blue-600 w-screen">
      <p>好好學(xué)習(xí)</p>
      <p>天天向上</p>
    </div>
  </div>
</template>
<script setup></script>
<style lang="scss" scoped></style>

圖片圖片

4.設(shè)置最小和最大寬度、高度

設(shè)置最小最大寬度:min-w-[]、max-w-[]

設(shè)置最小最大高度:min-h-[]、max-h-[]

<div class="w-[400px]">
  <div class="bg-red-600 w-full min-w-[200px] min-h-[500px]">
    <p>好好學(xué)習(xí)</p>
    <p>天天向上</p>
  </div>
  <div class="bg-blue-600 w-screen max-w-[100px] max-h-[300px]">
    <p>好好學(xué)習(xí)</p>
    <p>天天向上</p>
  </div>
</div>

4.2 設(shè)置邊距

1.margin

mt-* : margin-top

mb-* : margin-bottom

ml-* : margin-left

mr-* : margin-right

mx-* : margin-left, margin-right;

my-* : margin-top, margin-bottom;

mx-auto : margin: 0 auto;

<div class="bg-red-600 w-[100px] h-[100px]">
  <p>好好學(xué)習(xí)</p>
  <p>天天向上</p>
</div>
<div class="bg-blue-600 w-[300px] h-[300px] mt-20px mx-auto">
  <p>好好學(xué)習(xí)</p>
  <p>天天向上</p>
</div>

圖片圖片


2.padding

  • pt-* : padding-top
  • pb-* : padding-bottom
  • pl-* : padding-left
  • pr-* : padding-right
  • px-* : padding-left, padding-right;
  • py-* : padding-top, padding-bottom;
<div class="bg-blue-600 w-[300px] h-[300px]">
  <p class="pt-20 px-10 bg-red-300">好好學(xué)習(xí)</p>
  <p class="py-3 px-10 bg-green-400">天天向上</p>
</div>

圖片圖片

4.3 設(shè)置邊框

1.設(shè)置邊框?qū)挾?,如果不寫?shù)值,默認(rèn)是 1px

  • border-t-數(shù)值 :border-top-width;
  • border-r-數(shù)值 :border-right-width;
  • border-b-數(shù)值 :border-bottom-width;
  • border-l-數(shù)值 :border-left-width;
  • border-x-數(shù)值 :border-left-width; border-right-width;
  • border-y-數(shù)值 :border-top-width; border-right-width;

2.設(shè)置邊框顏色

border-顏色-數(shù)值

border-red-500

例:

<div class="bg-blue-600 w-[300px] h-[300px] border-t-2 border-b-2 border-red-500">
  <p class="pt-20 px-10 bg-red-300">好好學(xué)習(xí)</p>
  <p class="py-3 px-10 bg-green-400">天天向上</p>
</div>

3.設(shè)置邊框線條類型

  • border-solid :border-style: solid;
  • border-dotted  :border-style: dotted;
  • border-dashed  :border-style: dashed;
  • border-double  :border-style: double;
<div
  class="bg-blue-600 w-[300px] h-[300px] border-t-2 border-b-2 border-dotted border-red-500"
>
  <p class="pt-20 px-10 bg-red-300">好好學(xué)習(xí)</p>
  <p class="py-3 px-10 bg-green-400">天天向上</p>
</div>

4.4 設(shè)置背景顏色

bg-顏色-數(shù)值

<div class="bg-blue-600 w-[300px] h-[300px]">
  <p>好好學(xué)習(xí)</p>
  <p>天天向上</p>
</div>

圖片圖片

4.5 設(shè)置文本字體

1.設(shè)置字體大小: text-[ ]

<p class="text-[20px]">好好學(xué)習(xí)</p>


2.設(shè)置字體加粗

  • font-thin 表示 font-weight: 100;
  • font-light 表示 font-weight: 300;
  • font-normal 表示 font-weight: 400;
  • font-bold 表示 font-weight: 700;
  • font-black 表示 font-weight: 900;
<div class="bg-blue-600 w-[300px] h-[300px]">
  <p class="text-[20px] font-bold">好好學(xué)習(xí)</p>
  <p>天天向上</p>
</div>

圖片圖片

3.設(shè)置字體顏色:text-顏色-數(shù)值

<div class="bg-blue-600 w-[300px] h-[300px]">
  <p class="text-[20px] font-bold text-yellow-400">好好學(xué)習(xí)</p>
  <p>天天向上</p>
</div>

圖片圖片

4.6 偽類

hover:
  • hover:bg-red-300
  • hover:text-[60px]
  • hover:w-[500px]
  • hover:h-[200px]
<div
  class="bg-blue-600 w-[300px] h-[300px] hover:bg-red-300 hover:text-[60px] hover:w-[500px] hover:h-[200px]"
>
  <p class="font-bold text-yellow-400">好好學(xué)習(xí)</p>
  <p>天天向上</p>
</div>

圖片圖片

4.7 定位

  • relative 表示 position: relative;
  • absolute 表示 position: absolute;
  • fiexed 表示 position: fiexed;
  • z-1 表示 z-index:1;

數(shù)值:1 表示 4px

  • top-1 表示 top: 4px;
  • left-2 表示 left: 8px;
  • right-10 表示 right: 40px;
  • bottom-3 表示 bottom: 12px;

任意值:

  • top-[5px]
  • left-[10rem]
  • right-[20px]
  • bottom-[100px]

例:

<div
  class="bg-blue-600 w-[300px] h-[300px] hover:bg-red-300 fixed bottom-[20px] left-[100px]"
>
  <p class="font-bold text-yellow-400">好好學(xué)習(xí)</p>
  <p>天天向上</p>
</div>

圖片圖片

4.8 flex 布局

  • flex 表示 display: flex;
  • flex-row 表示 flex-direction: row;
  • flex-col 表示 flex-direction: column;
  • justify-center 表示 justify-content: center;
  • items-center 表示 align-items: center;
  • flex-wrap 表示換行
  • flex-nowrap 表示不換行
  • flex-1 表示 flex:1;

例:

<div
  class="bg-blue-600 w-[300px] h-[300px] flex flex-row justify-center items-center "
>
  <p class="bg-yellow-400 w-[100px] h-[100px] text-white-400">好好學(xué)習(xí)</p>
  <p class="bg-red-400 w-[100px] h-[100px] text-white-400">天天向上</p>
</div>

圖片圖片

4.9 樣式復(fù)用

下面的例子中 p 標(biāo)簽有重復(fù)的樣式

<div
  class="bg-blue-600 w-[300px] h-[300px] flex flex-row justify-center items-center flex-wrap"
>
  <p class="bg-red-400 w-[100px] h-[100px] text-white text-[20px]">好好學(xué)習(xí)</p>
  <p class="bg-yellow-400 w-[100px] h-[100px] text-white text-[20px]">天天向上</p>
</div>

如果遇到重復(fù)的樣式,我們可以借助 @layer 和 @apply 指令定義全局復(fù)用的樣式:

1.在 TailwindCSS 的樣式文件中定義復(fù)用樣式

圖片圖片

@layer components {
  .title {
    @apply w-[100px] h-[100px] text-white text-[20px];
  }
}

2.在類選擇器中使用復(fù)用類名

<p class="title">好好學(xué)習(xí)</p>
<p class="title">天天向上</p>


責(zé)任編輯:武曉燕 來(lái)源: 知否技術(shù)
相關(guān)推薦

2017-07-26 10:21:46

DockerLinux容器

2020-06-02 19:14:59

Kubernetes容器開發(fā)

2024-05-07 06:36:59

2020-11-17 11:39:00

JavaScript前端編程語(yǔ)言

2016-05-19 10:31:35

數(shù)據(jù)處理CassandraSpark

2024-08-26 08:16:13

2024-04-28 09:15:22

人工智能人形機(jī)器人

2023-09-17 23:01:39

Python編程語(yǔ)言

2014-09-17 10:16:41

Java 9

2021-03-02 16:25:13

手機(jī)iPhone安卓

2020-06-16 14:13:50

Kubernetes容器Linux

2024-01-15 08:47:22

3D自動(dòng)駕駛

2014-12-10 10:01:31

PHP

2011-05-30 14:07:42

2018-03-06 09:57:17

HackerRankPython編程

2018-02-01 13:42:47

編程語(yǔ)言開發(fā)者行業(yè)

2022-06-16 20:56:53

邊緣計(jì)算

2021-03-19 11:10:51

比特幣外匯加密貨幣

2020-07-01 23:21:11

Python web框架開發(fā)

2013-03-28 19:25:35

騰訊云
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)