【css】vue项目深度作用选择器(scoped)
scoped 修饰的style只给当前组件内的元素使用
本质:
1.给当前组件中的所有元素,添加一个随机的属性
2.给当前组件中的所有元素的样式添加一个对应的随机属性选择器
描述:
场景:按照设计图,修改select选择器的样式
设计图效果
html
<el-select class="el-select-box" v-model="sortStatus" @change="handleStatus"> <el-option v-for="item in items" :key="item.index" :label="item.text" :value="item.index"></el-option> </el-select>
css:
<style lang="scss" > //select 下拉css .el-select-dropdown{ top:557px; .el-select-dropdown__item.selected:after{ position: absolute; right: 20px; font-family: element-icons; content: ""; font-size: 12px; font-weight: 700; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } .el-select-dropdown__item{ width: 108px; height: 30px; color:#666; } } .ranking-top { width: 100%; height: 50px; padding: 0 20px; border-bottom: 1px solid #f4f3f8; .el-select{ width:98px!important; .el-input__inner{ font-size:14px; font-weight: bold; line-height:28px; border:none; } .el-input__icon{ line-height: 28px; } } } </style>
方案一:不加scoped
效果图(正是想要的效果)
ps: 由于select的下拉框是直接在body中的,为了修改下拉框的css,需要加一个scoped
方案二:加上scoped,select 的css失效啦
效果图(非想要效果)
解决方案:.ranking-top 前面加上一个::v-deep
最终方案:
1、使用深度作用选择器::v-deep
2、使用popper-append-to-body属性,修改弹出层的插入位置 注意,该属性值为boolean类型
html
<el-select class="el-select-box" :popper-append-to-body="false" v-model="sortStatus" @change="handleStatus"> <el-option v-for="item in items" :key="item.index" :label="item.text" :value="item.index"></el-option> </el-select>
css
<style lang="scss" scoped> ::v-deep .ranking-top { width: 100%; height: 50px; padding: 0 20px; border-bottom: 1px solid #f4f3f8; .el-select{ width:98px!important; .el-input__inner{ font-size:14px; font-weight: bold; line-height:28px; border:none; } .el-input__icon{ line-height: 28px; } //select 下拉css .el-select-dropdown{ top:557px; .el-select-dropdown__item.selected:after{ position: absolute; right: 20px; font-family: element-icons; content: ""; font-size: 12px; font-weight: 700; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } .el-select-dropdown__item{ width: 108px; height: 30px; color:#666; } } } } </style>
相关资料:
作者:smile.轉角
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我QQ

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
2020-05-18 【php】phpstorm 编辑器中文包