2017-06-16 132 views
1

我有一個LESS文件,在那裏我已經包裹了一大堆的規則於一體的類:嵌套祖先規則LESS

body.programme { 

    #VideoWrapper { 
     border: 1px solid red; 
     } 

    etc... 

    } 

但是我想改變#VideoWrapper的風格,如果另一個類存在在身體。我想我也許可以做到以下幾點...

body.programme { 

    #VideoWrapper { 
     border: 1px solid red; 

     body.inside & { 
      border: 1px solid yellow; 
      } 

     } 

    etc... 

    } 

基本上我想輸出下面的CSS規則:

body.programme #VideoWrapper { 
    border: 1px solid red; 
} 
body.programme.inside #VideoWrapper { 
    border: 1px solid yellow; 
} 

在不到這甚至可能沒有拉出來的規則父主體的程序包裝器?

+0

我從來沒有見過這樣的解決方案......但我也很感興趣,如果這是可能的話:D –

+1

如果這些是你想要的CSS規則,爲什麼你不這樣寫呢? – 2017-06-16 09:32:23

回答

1

我認爲使用&選擇應該做的伎倆,這樣的事情:

body.programme { 
    #VideoWrapper { 
     // Your styles 
    } 
    &.inside { 
     #VideoWrapper { 
      // Different styles 
     } 
    } 
} 

另一種方法(其中唉意味着將這些樣式出來的body)可能是:

#VideoWrapper { 
    // generic styles 
    body.programme & { 
     // special styles 
    } 
    body.programme.inside & { 
     // further styles 
    } 
} 

但是這可能不會讓你的強迫症得到滿足。

+0

他的觀點是,他會拼命想避免兩次寫'#VideoWrapper'。 – 2017-06-16 09:34:40

+0

正確!強迫症! :) –

+0

我不知道有一種方法可以在不重構的情況下完成您想要的任務 - 請參閱我編輯的答案。 –