Vue 子元素button如何继承父元素的字体样式

624 阅读1分钟

father为父组件元素,el-button为子组件元素(我们这里默认子组件元素内部有一个 .el-button 的样式),如果想要子组件 el-button 继承 father 的字体样式,解决方案有三种

<div class="father">
    <el-button>按钮</el-button>
</div>

1、去掉sytle标签上的 scoped 属性,或从新起一个不带 scoped 属性的 style 标签,将字体样式定义为全局样式。

<style>
    .father {
        color: red;
    }
</style>

2、通过 /deep/ 或者 ::v-deep 穿透(/deep/可能有兼容性问题,更多时候建议使用 ::v-deep)

<style scoped>
    .father {
        color: red;
    }
    .father /deep/ .el-button {
        
    }
    .father ::v-deep .el-button {
    
    }
</style>

3、通过 >>> 穿透(不支持 css 预处理器,只能在原生 css 中使用)

<style scoped>
    .father {
        color: red;
    }
    .father >>> .el-button {
        
    }
</style>