日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

在LESS中,“Extend”是一種功能,允許我們從一個選擇器繼承樣式到另一個選擇器。當我們在一個選擇器中使用“extend”時,它會將該選擇器的樣式與與之匹配的任何其他選擇器合并。

讓我們通過下面的例子來理解它。這樣你就可以更清楚地了解在LESS中使用”extend”特性的用法。

語法

用戶可以按照以下語法在LESS中使用“extend”功能。

.selector1:extend(.selector2) {}
//The above block does the same thing as the below
.selector1{
   &:extend(.selector2);
}

登錄后復制

在上述語法中,”.selector1″是將繼承樣式的選擇器,而”.selector2″是它將從中繼承的選擇器。在使用”extend”時,我們還可以使用”&”符號來創建嵌套選擇器。

在Less中使用”Extend”特性的不同方法

以下是我們可以使用LESS中的“extend”功能來簡化和優化我們的CSS代碼的一些不同技術:

擴展附加到選擇器

擴展附加到選擇器上,允許我們將其附加到的選擇器與另一個選擇器合并。它類似于帶有選擇器參數的普通偽類。

這里有一些例子 –

    在現有選擇器之后擴展一個選擇器 ?

    pre:hover:extend(div pre) {
       // styles
    }
    

    登錄后復制

      在現有的選擇器和extend之間使用一個空格 –

      pre:hover :extend(div pre) {
         // styles
      }
      

      登錄后復制

        我們還可以在同一個規則集中對多個選擇器使用extend,就像這樣?

        h1,
        h2:extend(.title),
        h3:extend(.title) {
        }
        

        登錄后復制

        擴展內部規則集

        我們還可以在規則集內使用”extend”來將一個選擇器的屬性擴展到另一個選擇器上。例如 ?

        .selector1 {
           color: red;
        } 
        .selector2 {
           &:extend(.selector1);
           background-color: yellow;
        }
        

        登錄后復制

        擴展嵌套選擇器

        在使用 “extend” 時,我們可以使用 “&” 符號來創建嵌套選擇器。

        在下面的示例中,使用”extend”將嵌套選擇器”.selector1.nested”擴展為”.selector2″。這使得我們可以在”.selector2″上繼承”.selector1″和”.nested”的樣式。

        .selector1 {
           color: red;  
           &.nested {
              background-color: yellow;
           }
        } 
        .selector2:extend(.selector1.nested) {
           font-size: 16px;
        }
        

        登錄后復制

        Exact Matching With Extend

        當使用CSS擴展時,重要的是要理解它在選擇器之間尋找完全匹配。換句話說,即使它們具有相同的含義,選擇器也需要具有相同的形式才能匹配。

        例如,在以下的CSS代碼中 –

        .first.second,
        .second.first,
        .second > .first { 
           color: blue; 
        }
        // this will NOT match any of the selectors above
        .my-test:extend(.second) {} *.second { color: blue; }
        // this will NOT match the *.second selector
        .no-star:extend(.second) {}a:hover:visited { color: blue; }
        .my-selector:extend(a:visited:hover) {}
        

        登錄后復制

        擴展“all”

        我們可以在Less中使用all關鍵字作為擴展參數的最后一部分,它告訴Less將選擇器作為另一個選擇器的一部分進行匹配。這將創建一個新的選擇器,其中包含原始選擇器的匹配部分,并用擴展替換它。

        這是一個例子 ?

        .selector1.selector2.test,
        .test.selector1.selector3 {
           color: orange;
        } 
        .selector2.test {
           &:hover {
              color: green;
           }
        } 
        .selector4:extend(.test all) {}
        

        登錄后復制

        示例1

        在下面的示例中,我們定義了一個基本樣式,用于具有類名.button的按鈕,然后使用“extend”功能來定義特定的樣式,通過擴展基本樣式來定義主要按鈕和危險按鈕的樣式。

        .primary-button和.danger-button類繼承了.button類中定義的所有樣式,這有助于減少代碼重復。此外,每個類還添加了自己的自定義樣式,以創建不同的按鈕樣式。

        在輸出中,用戶可以觀察到為.button定義的樣式被.primary-button和.danger-button繼承,并且每個類別定義的自定義樣式被應用。

        // base style for a button
        .button {
           background-color: blue;
           border: none;
           color: white;
           padding: 10px;
        } 
        //  specific style for a primary button by extending the base style
        .primary-button:extend(.button) {
           background-color: green;
        } 
        //  specific style for a danger button by extending the base style
        .danger-button:extend(.button) {
           background-color: red;
        }
        

        登錄后復制

        輸出

        .button {
           background-color: blue;
           border: none;
           color: white;
           padding: 10px;
        } 
        .primary-button {
           background-color: green;
        } 
        .danger-button {
           background-color: red;
        }
        

        登錄后復制

        示例2

        在下面的示例中,我們為具有類名.card的卡片定義了一個基本樣式。然后,我們使用“extend”功能來定義大卡片、帶有標題的卡片、帶有頁腳的卡片以及同時具有標題和頁腳的卡片的特定樣式。

        在輸出中,用戶可以觀察到為 .card 定義的樣式被其他類繼承并根據需要進行自定義。

        //style for a card
        .card {
           background-color: white;
           border-radius: 5px;
           box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
           padding: 20px;
        } 
        //  style for a large card by extending the base style
        .large-card:extend(.card) {
           width: 500px;
        } 
        //style for a card with a header by extending the base style
        .card-with-header:extend(.card) {
           border-top: 2px solid black;
           padding-top: 30px;
        } 
        // style for a card with a footer by extending the base style
        .card-with-footer:extend(.card) {
           border-bottom: 2px solid black;
           padding-bottom: 30px;
        } 
        // style for a card with both a header and footer by extending the appropriate classes
        .card-with-header-and-footer:extend(.card-with-header, .card-with-footer) {
        }
        

        登錄后復制

        輸出

        .card {
           background-color: white;
           border-radius: 5px;
           box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
           padding: 20px;
        } 
        .large-card {
           background-color: white;
           border-radius: 5px;
           box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
           padding: 20px;
           width: 500px;
        } 
        .card-with-header {
           background-color: white;
           border-radius: 5px;
           box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
           padding: 20px;
           border-top: 2px solid black;
           padding-top: 30px;
        } 
        .card-with-footer {
           background-color: white;
           border-radius: 5px;
           box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
           padding: 20px;
           border-bottom: 2px solid black;
           padding-bottom: 30px;
        } 
        .card-with-header-and-footer {
           background-color: white;
           border-radius: 5px;
           box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
           padding: 20px;
           border-top: 2px solid black;
           padding-top: 30px;
           border-bottom: 2px solid black;
           padding-bottom: 30px;
        }
        

        登錄后復制

        用戶學習了在LESS中使用”extend”功能的語法以及使用”extend”簡化和優化CSS代碼的各種技巧。通過利用這個功能并使用優化CSS代碼的最佳實踐,用戶可以避免為相似的樣式編寫重復的代碼,并保持CSS代碼更有組織性。

        以上就是在LESS中,extend有什么用途?的詳細內容,更多請關注www.92cms.cn其它相關文章!

分享到:
標簽:extend 有什么 用途
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定