.firstRow div:nth-child(2) {
background: green;
width: 40px;
height: 100px;
border: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div class="firstRow">
<div>
<p>test.</p>
<p>test 2.</p>
</div>
</div>
答案 0 :(得分:2)
您要定位的孩子是p
元素,而不是div
元素:
.firstRow p:nth-child(2) {
background: green;
width: 40px;
height: 100px;
border: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div class="firstRow">
<div>
<p>test.</p>
<p>test 2.</p>
</div>
</div>
答案 1 :(得分:1)
:nth-child(n)
当前元素的父元素的n^th
子元素,因此您可以改用.firstRow p:nth-child(2)
。
答案 2 :(得分:1)
.firstRow div:nth-child(2) {
background: green;
width: 40px;
height: 100px;
border: 1px solid black;
}
<div class="firstRow"> <!-- parent -->
<div> <!-- this is nth-child(1) -->
<p>test.</p>
<p>test 2.</p>
</div>
<div></div> <!-- this is nth-child(2) -->
</div>