2013-05-13 95 views
2

我有這個在我的SASS文件::第一胎影響其他子元素

.buttons { 
    :first-child { 
     padding-top:7px; 
    } 
} 

HTML:

<div class="buttons"> 
    <div> 
    <label> 
     <input type="checkbox"> 
     bla blaa 
    </label> 
    </div> 
    <div> 
    <a class="advance">Step2</a> 
    <button class="btn">help <i class="arrow"></i></button> 
    </div> 
</div> 

箭頭孩子受着padding,雖然。我只想要父母.button內的第一個孩子。

我也試過這樣:

.buttons { 
    &:first-child { 
     padding-top:5px; 
    } 
} 

回答

8

你的第一個解決方案:

.buttons { 
    :first-child { 
     padding-top: 7px; 
    } 
} 

本質上變成這條規則:

.buttons *:first-child { padding-top: 7px; } 

這將具有完全相同的效果,你'描述 - 每:first-child.buttons將會受到影響。

你的第二個解決方案:

.buttons { 
    &:first-child { 
     padding-top: 5px; 
    } 
} 

打開這個規則:

.buttons:first-child { padding-top: 5px; } 

這將隻影響.buttons本身,只有當它是:first-child內它的父。

認爲你要找的是:

.buttons > *:first-child { padding-top: 5px; } 

這將影響第一要素,無論哪種類型,內.buttons它存在。

+0

.buttons>:first-child {padding-top:5px; }這是導致渲染錯誤無效 – Chapsterj 2013-05-13 16:50:29

+0

我很抱歉特洛伊我的錯我添加了其他導致錯誤的東西。這工作謝謝你。你能解釋一下在sass中我從來沒有用過的東西嗎?另外我猜這個*只是說任何元素類型。所以我可以使用> div:first-child {}到達第一個div .buttons – Chapsterj 2013-05-13 16:54:00

+0

是的,CSS中的'*'表示「任何元素類型」。 CSS中的'>'意味着「直接後裔」。換句話說,'div> span'會匹配'

this
'而不是'

this

' – 2013-05-13 17:04:26