溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

vue中element-ui組件默認(rèn)css樣式修改的方式是什么

發(fā)布時(shí)間:2022-10-21 16:55:18 來源:億速云 閱讀:189 作者:iii 欄目:編程語(yǔ)言

這篇文章主要介紹“vue中element-ui組件默認(rèn)css樣式修改的方式是什么”,在日常操作中,相信很多人在vue中element-ui組件默認(rèn)css樣式修改的方式是什么問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”vue中element-ui組件默認(rèn)css樣式修改的方式是什么”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

1.使用全局統(tǒng)一覆蓋

針對(duì)一些通用的、樣式固定的組件,可以全局處理,其方法是新建一個(gè)css或者scss文件,覆蓋element原有的class

你可以在src/styles目錄下新建一個(gè)element-ui-reset.scss,根據(jù)UI的需要,修改原有的class名稱

使用scss的好處是可以使用變量,來應(yīng)對(duì)UI的不同變化

比如我們常用的按鈕、分頁(yè)、復(fù)選框等組件,在UI中基本都是同樣的設(shè)計(jì),那么我就就可以統(tǒng)一修改這些樣式

element-ui-reset.scss

$danger-btn-color: #F25454;
$primary-btn-color:#3d66e4;
$success-btn-color:#12A763;


//修改默認(rèn)按鈕顏色
.el-button--primary{
	background-color: $primary-btn-color;
	border-radius: 4px;
	//border: 1px solid $primary-btn-color;
	font-size: 16px;
	border: 0;
	
}

//修改危險(xiǎn)按鈕的顏色
.el-button--danger{
	background-color: $danger-btn-color;
	border-radius: 4px;
	//border: 1px solid $danger-btn-color;
	font-size: 16px;
	border: 0;
}

//修改成功按鈕的顏色
.el-button--success{
	background-color: $success-btn-color;
	border-radius: 4px;
	//border: 1px solid $success-btn-color;
	font-size: 16px;
	border: 0;
	
}

//修改默認(rèn)按鈕的顏色
.el-button--default{
	font-size: 16px;
	border-radius: 4px;
}


//修改成功按鈕的顏色
.el-button--warning{
	//background-color: $success-btn-color;
	border-radius: 4px;
	//border: 1px solid $success-btn-color;
	font-size: 16px;
	border: 0;
	
}


//修改分頁(yè)顏色
.el-pagination{
	position: absolute;
	display: inline-block;
	margin: 0 auto;
	left:50%;
	transform: translateX(-50%);
	background-color: #fafafa;
	border: solid 1px #dfe8eb;
	padding: 0 !important;
	.el-pager{
		margin: 0 !important;
		.number{
			color: #3d66e4 !important;
			border-left: 1px solid #dfe8eb;
			border-right: 1px solid #dfe8eb;
			background-color: #fafafa !important;
			&.active{
				color: #fff !important;
				//border: 1px  solid  #3d66e4;
				background-color: #3d66e4 !important;
				border: 1px solid #3d66e4 !important;
			}
		}
		
	}
	
}	

.el-pagination.is-background .btn-next, .el-pagination.is-background .btn-prev, .el-pagination.is-background .el-pager li{
	margin: 0 !important;
	background-color: #fafafa !important;
}


//修改checkbox
.el-checkbox__inner{
	  border: 1px solid #C0C0C0 ;
	  width: 16px;
	  height: 16px;
	  border-radius: 0;
}

然后在你的main.js中引入

import './src/styles/element-ui-reset.scss'

這樣

優(yōu)點(diǎn)

  • 全局覆蓋,省事

  • 使用scss更加靈活

  • 可以隨時(shí)刪除樣式的覆蓋

缺點(diǎn)

  • 局部修改時(shí)需要重新覆蓋

  • 所有被修改的組件樣式都是一樣的

2.在.vue文件中修改

這種方法是在vue文件中新加一個(gè)style標(biāo)簽

用于修改一些特定的組件樣式,但不會(huì)全局應(yīng)用

比如,某個(gè).vue文件中需要一個(gè)特別定制的table組件,而其它文件則需要用原來的樣式

這樣,我們最好的處理方式就是在.vue文件中新加一個(gè)style標(biāo)簽

在這里修改table的默認(rèn)樣式

<template>
	<div class="my-class">
            <el-table>
            </el-table>
        </div>
</template>

<script>
</script>

<style scoped="scoped" lang="scss">
</style>

<style>
	
    /* 修改element-ui中table組件的樣式 */

    .my-class__expand-column .cell {
            display: none;
    }

    .my-class .el-table tbody tr:hover>td {
            cursor: pointer;
    }


    .my-class .el-form .el-form-item  .el-input__inner:focus{
             border: 1px solid #3D66E4;
       }

   
</style>

但請(qǐng)注意,一定要加上唯一的作用域 即最外層的class名,比如我上面的my-class 。 它限定了當(dāng)前table的修改樣式只能在該class以及其子元素中生效

否則,table的樣式仍會(huì)全局覆蓋

當(dāng)然,如果你愿意,可以將class換成id,這樣保證了class名不會(huì)沖突

<template>
	<div id="my-class">
            <el-table>
            </el-table>
        </div>
</template>

<style>
	
    /* 修改element-ui中table組件的樣式 */

    #my-class__expand-column .cell {
            display: none;
    }

    #my-class .el-table tbody tr:hover>td {
            cursor: pointer;
    }


    #my-class .el-form .el-form-item  .el-input__inner:focus{
             border: 1px solid #3D66E4;
       }

  
</style>

這種方式的好處在于你可以動(dòng)態(tài)的綁定某些class

 <template>
	<div id="my-class">
            <el-table :class="my-table">
            </el-table>
        </div>
</template>

<style>
	
    /* 修改element-ui中table組件的樣式 */

    #my-class__expand-column .cell {
            display: none;
    }

    #my-class .el-table tbody tr:hover>td {
            cursor: pointer;
    }


    #my-class .el-form .el-form-item  .el-input__inner:focus{
             border: 1px solid #3D66E4;
       }

    #my-class .my-table {
    
    }

</style>

優(yōu)點(diǎn)

  • 靈活自定義,可以動(dòng)態(tài)綁定

  • 不會(huì)全局修改

缺點(diǎn)

  • 需要指定唯一的class作用域

3.修改組件的style樣式

這種方法我不是很推薦,拋開冗余不說,其實(shí)不敢保證其生效(依賴element-ui源碼的支持程度)。

但是,對(duì)于某些使用頻率很低但需要?jiǎng)討B(tài)綁定屬性的組件,你可以使用它

比如這個(gè)<el-backtop>組件,我可能需要給它綁定一些動(dòng)態(tài)的樣式屬性

這樣你就可給它綁定style

	<el-backtop target="" :bottom="100" >
	  <div :style="{
             height: 100%;
             width: _width;
             background-color: #f2f5f6;
             box-shadow: 0 0 6px rgba(0,0,0, .12);
             text-align: center;
             line-height: 40px;
             color: #1989fa;
             border-radius: 50%;
           }">

                        <i class="el-icon-caret-top"></i>
                </div>
        </el-backtop>
        
        
        data() {
           
         return{
           _width: 50%
         }
        }

優(yōu)點(diǎn)

  • 靈活方便

  • 動(dòng)態(tài)綁定

缺點(diǎn)

  • 冗余

  • 耦合性高

4. 參考element-ui官方文檔的api

有些組件官網(wǎng)給了修改樣式的api

你可以按照api來指定組件的樣式

vue中element-ui組件默認(rèn)css樣式修改的方式是什么

優(yōu)點(diǎn)

  • 官方

缺點(diǎn)

  • 支持組件較少

疑問

為何要新加一個(gè)style標(biāo)簽 ?

因?yàn)樵趯?shí)際使用過程中,我發(fā)現(xiàn)寫在帶有scoped屬性的某些class并不對(duì)element-ui的組件生效

這造成有的修改樣式可以用,有的不可以,所有就索性重新寫了了style標(biāo)簽

為何不用!important屬性

這家伙太霸道了,比全局修改還要強(qiáng)制。況且寫起來很麻煩

為何不用::v-deep穿透

首先,拋開寫法惡心來說,其耦合性太高

假如在你不需要樣式覆蓋的時(shí)候,你只需要?jiǎng)h除新的style標(biāo)簽

而用::v-deep 的話,逐行去改誰(shuí)受得了

到此,關(guān)于“vue中element-ui組件默認(rèn)css樣式修改的方式是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI