css - 无法通过第 nth-child 定位其他元素内的元素

标签 css css-selectors parent

<分区>

我正在尝试通过“nth-child”选择器定位“i”元素,但我不知道该怎么做。你能给我解释一下吗? 在提供的代码中,我尝试使用 SASS

 <ul class="nav-social">
          <li>
            <a href="#"><i class="fab fa-facebook-f"></i></a>
          </li>
          <li>
            <a href="#"><i class="fab fa-twitter"></i></a>
          </li>
          <li>
            <a href="#"><i class="fab fa-youtube"></i></a>
          </li>
          <li>
            <a href="#"><i class="fab fa-instagram"></i></a>
          </li>
        </ul>

我是如何尝试的:

.nav-social {
    li a {
      margin-right: 0.5rem;


      i {
        font-size: 1.5rem;
        // border: 1px solid red;
        border-radius: 50%;
        background: rgba(0, 0, 0, 0);
        color: whitesmoke;
        width: 35px;
        height: 35px;
        text-align: center;
        line-height: 35px;

        &:nth-child(1) {
          color: green;
        }
      }


    }
  }

最佳答案

您需要选择 ul(父级)的 nth-child。所以你需要定位 li 元素:

.nav-social
{
    li
    {
        a
        {
            margin-right: 0.5rem;
            i
            {
                font-size: 1.5rem;
                // border: 1px solid red;
                border-radius: 50%;
                background: rgba(0, 0, 0, 0);
                color: whitesmoke;
                width: 35px;
                height: 35px;
                text-align: center;
                line-height: 35px;
            }
        }
        &:nth-child(1)
        {
            a
            {
                i
                {
                    color: green;
                }
            }
        }
    }
}

关于css - 无法通过第 nth-child 定位其他元素内的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57099809/

上一篇:html - 在列表中显示选择标签选项,但在关闭选择框时隐藏文本

下一篇:css - react , flex 盒子 : I want my div to transitionally rise from the bottom of a flex container

相关文章:

html - 嵌套 div 中的最后一个 child

javascript - 如何为 DOM 元素生成唯一的 css 选择器?

python - 在 Python 中运行独立于父进程的子进程

mysql - 如何在父子关系表中找到无菌节点?

javascript - 将表格格式化为水平布局

html - 我希望 <div> 位于正中心;不知道如何处理底部和右边距

javascript - 如何使用 Javascript 设置 CSS3::selection

css - 通过将鼠标悬停在另一个 div 上来更改一个 div 的不透明度

css - 如何使用 CSS 删除此 <hr>

jQuery -parent() 不在回调函数中工作