CSS:父母的最后一个孩子

时间:2017-01-23 18:46:50

标签: html css css-selectors

:last-child在所有&#34;孩子&#34;元素是相同的(即:所有<p>或所有<li>,并且该规则适用于该类型的孩子。

但是我如何使用CSS来选择最后一个孩子&#34;包含不同元素的父元素内的元素?

例如,在此示例中,如何将规则应用于.parent以选择其中的最后一个对象(div)?

&#13;
&#13;
.parent:last-child {
  background-color: red;
 }
&#13;
<div class="parent">
  
  <p>First child</p>
  <input type="text" placeholder="Second child" />
  <div>Third child</div>
  
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:16)

您可以使用resources :homes.parent > *:last-child

  

星号(*)是CSS的通用选择器。它匹配单个   任何类型的元素。用简单的选择器省略星号   同样的效果。

&#13;
&#13;
.parent > :last-child
&#13;
.parent > *:last-child {
  background-color: red;
}
&#13;
&#13;
&#13;

相关问题