根据class类名自动生成style样式

        我们前端开发不可避免的要和class类名和style样式打交道,有的时候我们只需要为某个dom设置一个样式,这时候我们也需要给dom添加一个class,然后在style中书写样式。

 

        现在给大家分享一个好用的webpack插件,可以根据你写的class名称,自动生成style样式,比如 font-size:16px 会自动生成.font-size:16px { font-size: 16px; }, color:red会自动生成.color:red { color: red }。这样属性名称全写出来也不是很方便,这个插件还支持属性名称简写比如 font-size:16px 还可以写成 fs:16px ,color:red 可以写成 c:red 。接下来和我一起看看插件怎么使用吧。

1. 安装

npm i style-from-class

2. 配置

2.1 在src文件夹下新建styles.css文件,并在main.js中导入

2.2 在vue.config.js中使用插件, 添加如下代码

const StyleFromClass = require('style-from-class');

configureWebpack: {
    plugins: [
        new StyleFromClass({
            fileType: ["vue"],      // 必填
            dirPath: "",            // 非必填, 默认值 ./src
            cssFilePath: "",        // 非必填, 默认值 ./src/styles.css
        })
    ],
}

参数介绍

fileType:fileType包含的文件才会通过class类名自动生成style样式

dirPath:dirPath这个文件夹下的文件才会通过class类名自动生成style样式

cssFilePath:通过class类名自动生成style样式保存在这个css文件中

3. 使用

<div class="c:red ta:center fs:red">文字</div>

到这里,就可以使用这个插件通过claas类名自动生成style样式了。自己去尝试一下吧。

4. 样式预览

        当鼠标移动到类名上是会hover展示这个类名会添加什么样式。

        4.1 首先给vscode安装一个插件 Preview Style

         

        4.2 鼠标移动到类名上

         

 

注意:

        因为有些属性名简写是一样的,这样就导致有些属性名称不能使用简写的方式。比如border和bottom,简写都是b,此时如果写了b代表了bottom。还有一些见下面的表格,红色加粗的表示简写代表的属性。

简写属性名
aall  animation
bbackground  border  bottom
cclear  clip  color  columns  content  cursor
ddirection  display
ffilter  flex  float  font
oopacity  order  outline  overflow
ppadding  perspective  position
rresize  right
ttop  transform  transition
adanimation-delay  animation-direction  animation-duration
bcbackground-clip  background-color  border-collapse  border-color
bibackground-image  border-image
brbackground-repeat  border-radius  border-right
bsbackground-size  border-spacing  border-style  box-shadow  box-sizing
cscaption-side  column-span
crcolumn-rule  counter-reset
ffflex-flow  font-family
fsflex-shrink  font-size  font-stretch  font-style
fwflex-wrap  font-weight
lsletter-spacing  list-style
mhmax-height  min-height
mwmax-width  min-width
tstab-size  text-shadow  transform-style
tdtext-decoration  transition-delay  transition-duration
totext-overflow  transform-origin
wswhite-space  word-spacing
bisborder-image-slice  border-image-source

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值