[CSS3] Tailwindcss组件

在前面的学习中,我们发现很多时候一些标签所应用的原子类是一样的。例如:

<div class="float-left text-center flex items-center bg-blue-100 block"></div>
<div class="float-left text-center flex items-center bg-blue-100 block"></div>
<div class="float-left text-center flex items-center bg-blue-100 block"></div>
<div class="float-left text-center flex items-center bg-blue-100 block"></div>
<div class="float-left text-center flex items-center bg-blue-100 block"></div>

像上面的情况,很多标签所应用的原子类都是一样的,那么我们就可以将其封装为一个组件。

在 tailwind 里面,要封装一个组件可以使用 @apply 指令,该指令后面就可以跟上一组原子类,然后给这个指令取一个名字即可。

.item{
  @apply float-right text-center flex items-center bg-blue-100 block
}

回头在 html 中只需要挂上 item 这个类即可

<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>

通过组件的方式,可以大大减少我们代码的冗余,提高代码的可维护性。

实战案例笔记

在使用深度选择器的时候,我们在 input 上面设置了 peer 这个类,然后在同级的 div 上面设置了 peer:checked

<input type="radio" name="swith" checked class="btn peer"/>
<div class="bg-img-2 bg peer-checked:opacity-100"></div>

上面的代码中表示,当 input 被 checked 的时候,div 会应用 opacity-100 这个样式类。

还需要在配置文件中配置一下:

variants: {
    extend: {
      opacity: ["peer-checked"]
    },
 },

还需要注意 peer-checked 后面要应用的类必须是 tailwind 里面内置的原子类。

posted @   Zhentiw  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
历史上的今天:
2023-03-06 [React] Performance issue - 01 useState with slow function call
2019-03-06 [Algorithm] Count Negative Integers in Row/Column-Wise Sorted Matrix
2018-03-06 [CSS3] The picture element
2018-03-06 [CSS3] Image Width with sizes (srcset & sizes)
2018-03-06 [SCSS] Pure CSS for multiline truncation with ellipsis
2017-03-06 [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types
2017-03-06 [Angular] Using ngTemplateOutlet to create dynamic template
点击右上角即可分享
微信分享提示