vue中scoped的使用

Vue的scoped属性用于实现组件间的样式隔离。当组件有scoped时,会在DOM中添加唯一的data-v-标识,使得样式仅对该组件生效。1. 父组件无scoped,子组件有,父组件无法影响子组件样式;2. 父组件有,子组件无,仍无法影响;3. 父子组件都有,样式隔离导致无法直接设置。若组件内嵌组件,最外层组件有scoped,不影响内部组件。可以混合使用全局和局部样式,以及利用深选择器进行特定情况的样式覆盖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vue中scoped的使用

原理:
vue通过在DOM结构以及css样式上加上唯一的标记 data-v-469af010,保证唯一,

[ ] 概点知识

1.父组件无scoped属性,子组件带有scoped,父组件是无法操作子组件的样式的(原因在原理中可知),虽然我们可以在全局中通过该类标签的标签选择器设置样式,但会影响到其他组件
2.父组件有scoped属性,子组件无,父组件也无法设置子组件样式,因为父组件的所有标签都会带有data-v-469af010唯一标志,但子组件不会带有这个唯一标志属性,与1同理,虽然我们可以在全局中通过该类标签的标签选择器设置样式,但会影响到其他组件
3.父子组建都有,同理也无法设置样式,更改起来增加代码量

因此如果组件内部还有组件,只会给最外层的组件里的标签加上唯一属性字段,不影响组件内部引用的组件( 注意 )

  • 使用方法

  • 混合使用全局属性和局部属性

<style>
        .el-button--small {
            font-size: 12px !important;
        }
        .el-message-box__btns .el-button--small{
            width:100px;
            height: 32px;
            background:white !important;
            border-radius: 16px;
            border:1px solid #DCDFE6;
            color:#4c9aff;
            font-weight: bold !important;
            font-size: 20px !important;
            line-height:10px !important;
        }
        .el-message-box__btns .el-button--small:hover{
            border:1px solid #3680e4 !important;
            color:#357FE3 !important;
        }
        .el-message-box__btns .el-button--primary{

            height: 32px !important;
            color: #FFF !important;
            background-color: #409EFF !important;
            border-color: #409EFF !important;
            font-weight: bold;
            font-size: 20px;
        }
        .el-message-box__btns .el-button--primary:hover{
            color: #FFF !important;
            background-color: #409EFF !important;
        }
    </style>
  • 深选择器
<style scoped>
    .el-select {
        width: 160px;
        height: 32px;
        line-height: 32px;
    }
    .el-input__icon {
        line-height: 34px;
        position: relative;
        /*left: 719%;*/
        left:724%;
    }
    .el-select>.el-input {
        display: block;
        width: 160px;
    }
    .el-input {
        font-size: 16px;
        width: 240px;
        height: 32px;
        line-height: 32px;
        border: none;
        border-radius: 16px;
    }
    .el-input__inner {
        height: 32px;
        line-height: 32px;
        border-radius: 16px;
        background: #0B1339;
        color: #FFFFFF;
        border-color: #4A5278;
    }
    .el-table td,
    .el-table th {
        padding: 12px 0;
        text-align: left;
        color: white;
        border: none;
    }
    .el-table__body tr:hover>td {
        background-color:transparent !important;
    }
    .el-table th {
        background-color: #142A59;
    }
    .el-table tr {
        background: #091638;
    }
    .el-table th.is-leaf {
        /* 去除上边框 */
        border-bottom: none;
    }
    .el-table::before {
        /* 去除下边框 */
        height: 0;
    }
    .el-pagination {
        color: #303133;
        font-weight: 700;
        width: 100%;
        text-align: center;
        margin: 30px 0 19px 0;
    }
    .el-pagination.is-background .btn-next,
    .el-pagination.is-background .btn-prev,
    .el-pagination.is-background .el-pager li {
        margin: 0 5px;
        background-color: #10173E;
        color: white;
        min-width: 30px;
        border-radius: 2px;
        border: 1px solid #2E4375;
        /* border-color: #2E4375; */
    }
    .el-pagination__editor.el-input .el-input__inner {
        border-radius: 4px;
    }
    .el-range-editor.el-input__inner {
        display: -webkit-inline-box;
        display: -ms-inline-flexbox;
        display: inline-flex;
        -webkit-box-align: center;
        -ms-flex-align: center;
        align-items: center;
        padding: 0 13px;
        border-color: #3F466C;
    }
    .el-range-editor .el-range-input {
        line-height: 1;
        background: #0D143A;
        color: #3F466C;
    }
    .el-date-editor .el-range-separator {
        padding: 0 5px;
        line-height: 32px;
        width: 5%;
        color: #FDFFFF;
    }
    .el-drawer__body .el-input__inner {
        height: 32px;
        line-height: 32px;
        border-radius: 16px;
        background: white;
        color: #5C5C5C;
        border-color: #E5E5E5;
        width:240px;
        margin-left: -14px;
    }
    .el-drawer__body .el-input {
        width: 160px !important;
    }
    .demo-drawer__footer {
        text-align: center;
    }
    .examine {
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        margin-left: 119px;
    }
    .el-timeline-item__timestamp.is-top {
        /* margin-top: -16px; */
        margin-left: -110px;
        width: 100px;
        color: black;
    }
    .el-timeline-item__content {
        color: black;
        margin-top: -38px;
    }
    .el-date-editor .el-range-separator {
        padding: 0 5px;
        line-height: 32px;
        width: 10%;
        color: #FDFFFF;
    }
    .inputLine .el-input__inner {
        width: 455px;
        margin-left: -14px;
    }
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值