:第一胎影响其他子元素
问题描述:
我有这个在我的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;
}
}
答
你的第一个解决方案:
.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
它存在。
.buttons>:first-child {padding-top:5px; }这是导致渲染错误无效 – Chapsterj 2013-05-13 16:50:29
我很抱歉特洛伊我的错我添加了其他导致错误的东西。这工作谢谢你。你能解释一下在sass中我从来没有用过的东西吗?另外我猜这个*只是说任何元素类型。所以我可以使用> div:first-child {}到达第一个div .buttons – Chapsterj 2013-05-13 16:54:00
是的,CSS中的'*'表示“任何元素类型”。 CSS中的'>'意味着“直接后裔”。换句话说,'div> span'会匹配'
this