2017-07-26 63 views
0

假設您想要有幾個h2,並且它們都是不同的顏色,並且它們都具有相同顏色的:after元素。在SASS中,是否可以將子元素的規則的值與父元素的規則進行匹配?

example of a styled h2

什麼了我想要做的是相互匹配:after元素的background-color其父h2color。由於它們都使用顏色屬性,因此在這種情況下應該是有效的。

我們知道,你可以窩在SASS的子元素,像這樣:

h2{ 
    font-weight: bold; 
    &:after{ 
     height:4px; 
    } 
} 

有沒有一種方法,以配合孩子的規則,以父母的規則的值的值?像這樣?如果有,什麼是正確的語法?我似乎無法找到任何指向答案的東西。

h2{ 
    font-weight: bold; 
    color: #093261; 

    &:after{ 
     height:4px; 
     width:50px; 
     content:''; 
     display: block; 
     position: relative; 
     bottom: 0; 

     /* this is what I'm trying to accomplish: */ 
     background: &:color 
    } 
} 
+1

在手寫筆有一個叫做[屬性查找]特徵(HTTP:/ /stylus-lang.com/docs/variables.html#property-lookup),我認爲這是你想要做的,但在SASS中沒有類似的東西。 – blonfu

+0

將是一個很酷的功能添加到Sass,不是嗎? –

回答

0

我建議使用一個SASS變量或@extend這樣:

$headercolor: #093261; 

h2 { 
    font-weight: bold; 
    color: $headercolor; 

    &:after{ 
     height:4px; 
     width:50px; 
     content:''; 
     display: block; 
     position: relative; 
     bottom: 0; 

     /* this is what I'm trying to accomplish: */ 
     background: &:color 
     /* variable */ 
     background: $headercolor; 
    } 
} 

或:

h2 { 
    font-weight: bold; 
    color: #093261; 

    &:after{ 
     height:4px; 
     width:50px; 
     content:''; 
     display: block; 
     position: relative; 
     bottom: 0; 

     /* this is what I'm trying to accomplish: */ 
     background: &:color 
     /* extend */ 
     @extend h2; 
    } 
} 
+0

我想說的是會有一堆h2,有不同的顏色,並且:如果可能的話,每個元素的背景顏色都應該匹配h2顏色的值。基本上,你可以把一個物業的價值用於另一個物業的跨父母/小孩嗎? –

相關問題