简体   繁体   English

伪元素不显示在第n个子元素中

[英]Pseudo Element Not displaying in nth-child

I was playing around with a little nav menu and pseudo elements and I was wondering why the tiny light orange square I have created displays on the first child element but not the other elements when it is copied over and put in the nth-child(2). 我当时在玩一些导航菜单和伪元素,并且想知道为什么在复制并放入第n个子元素时,我在第一个子元素上显示的是浅橙色的正方形,而在其他元素上却没有显示(2 )。

 body { margin: 0; } * { margin: 0; padding: 0; } #orangeNavBar { height: 45px; width: 627px; background-color: #E87966; margin-left: auto; margin-right: auto; margin-top: 15px; } ul { list-style: none; } ul li { float: left; font-family: times new roman; font-size: 1.1em; font-weight: bold; color: black; height: 45px; box-sizing: border-box; padding: 10px 20px; border: 1px solid black; } ul li:first-child::after { content: ""; height: 8px; width: 8px; background: #F1BAAF; display: block; float: right; position: relative; left: 20px; top: 7.5px; } ul li:nth-child(2)::after { content: ""; height: 8px; width: 8px; background: #F1BAAF; display: block; float: right; position: relative; left: 20px; top: 7.5px; } 
 <div id="orangeNavBar"> <ul> <li>Home</li> <li>Products</li> <li>Company</li> <li>Contact</li> </ul> </div> 

Because 因为

ul li:nth-child(2)::after

only selects the second item 仅选择第二

You need 你需要

ul li:nth-child(n+2)::after

so each child after the first one is selected. 因此选择了第一个孩子之后每个孩子。

 body { margin: 0; } * { margin: 0; padding: 0; } #orangeNavBar { height: 45px; background-color: #E87966; margin-left: auto; margin-right: auto; margin-top: 15px; } ul { list-style: none; } ul li { float: left; font-family: times new roman; font-size: 1.1em; font-weight: bold; color: black; height: 45px; box-sizing: border-box; padding: 10px 20px; border: 1px solid black; } ul li:first-child::after { content: ""; height: 8px; width: 8px; background: #F1BAAF; display: block; float: right; position: relative; left: 20px; top: 7.5px; } ul li:nth-child(n+2)::after { content: ""; height: 8px; width: 8px; background: #F1BAAF; display: block; float: right; position: relative; left: 20px; top: 7.5px; } 
 <div id="orangeNavBar"> <ul> <li>Home</li> <li>Products</li> <li>Company</li> <li>Contact</li> </ul> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM