帝王谷资源网 Design By www.wdxyy.com

css 作用域是全局的,项目越来越大,人越来越多,命名慢慢成为问题,以下是几种解决命名问题的方案

一. BEM

以 .block__element--modifier 形式命名,命名有含义,block 可视为模块,有一定作用域含义

实例

.dropdown-menu__item--active

二. scoped css

参考:vue-loader.vuejs.org/zh/guide/sc…

目标:当前组件样式不会影响其它组件

给组件的 dom 节点添加惟一属性,并转换 style 标签中的 css 匹配该属性,使得 css 作用域有限

实例

<style scoped>
.example {
  color: red;
}
</style>
 
<template>
  <div class="example">hi</div>
</template>

转换结果:

<style>
.example[data-v-f3f3eg9] {
  color: red;
}
</style>
 
<template>
  <div class="example" data-v-f3f3eg9>hi</div>
</template>

三. css modules

参考:vue-loader.vuejs.org/zh/guide/cs…

将 css 的选择器转换成惟一的字符串,运用到 dom。是在用算法命名,记录了人的命名到算法命名的 map 表

实例

<style module>
.red {
  color: red;
}
</style>
<template>
  <p :class="$style.red">
    This should be red
  </p>
</template>

转换结果:

<style module>
._1yZGjg0pYkMbaHPr4wT6P__1 {
  color: red;
}
</style>
<template>
  <p class="_1yZGjg0pYkMbaHPr4wT6P__1">
    This should be red
  </p>
</template>

四. css-in-js

参考:github.com/styled-comp…

将 css 内容用惟一的选择器表示。同 css modules,用算法命名。将 css 视为 js 的字符串,赋予 css 更多能力

实例

<template>
  <css-in-js></css-in-js>
</template>
 
<script>
  import styled from 'vue-styled-components';
  export default {
    components: {
      cssInJs: styled.div `
                color: red;
            `
    }
  }
</script>

转换结果:

<template>
    <div class="gXTzCp"></div>
</template>
<style>
.gXTzCp {
    color: red;
}
</style>

五. 总结

  1. BEM 让命名有规律、有含义,block 可视为模块,有一定作用域含义
  2. scoped css 限定 css 作用域,无关命名。无法适配多套主题
  3. css modules 使用算法命名,没有了命名冲突,也限定了 css 作用域。无法适配多套主题
  4. css-in-js 使用算法命名,拥有 css modules 的优势。同时将 css 视为 js 的字符串,赋予 css 更多能力
标签:
css,命名

帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。