【问题标题】::first-child and :nth-child(1) not working:first-child 和 :nth-child(1) 不工作
【发布时间】:2021-12-14 18:06:13
【问题描述】:

我正在尝试使用 nth-child(1) 或 first-child 选择器 (.contacts:first-child) 选择“联系人”地址元素的第一段,但它不起作用:(如果我创建一个class 到第一段并使用 class css 选择器,它工作正常。我错过了什么?

<div class="address-col">
    <p class="footer-heading">Contact us</p>
       <address class="contacts">
         <p>23 Harrison St., 2nd Floor, San Francisco, CA 94107</p>
    
         <p>
            <a class="footer-link" href="tel:415-201-6370">415-201-6370</a><br/>
            <a class="footer-link" href="mailto:hello@example.com">hello@example.com</a>
         </p>
       </address>
</div>

提前致谢!

【问题讨论】:

    标签: html css css-selectors


    【解决方案1】:

    The :first-child CSS pseudo-class represents the first element among a group of sibling elements.

    正确的选择器是.contacts p:first-child

    如果您希望它仅基于容器工作而不知道或关心它包含什么,您可以使用.contacts &gt; *:first-child

    当您在容器上使用它时,您实际上通过 css 请求的是一个具有 .contacts 类的元素,它是其兄弟元素中的第一个子元素。

    【讨论】:

    • 你是对的。我误解了文档。我坚持我正在选择联系人第一个孩子,这是使用 .contacts:first-child 的第一段。谢谢!
    • 不客气,很高兴我能帮助你更好地理解 css!祝你有美好的一天,继续编码!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    相关资源
    最近更新 更多