【问题标题】:Change first child ::before style when .active.active 时更改第一个孩子 ::before 样式
【发布时间】:2014-07-28 14:12:42
【问题描述】:

我有一个如下所示的菜单:http://i.stack.imgur.com/beX6i.png

小三角形是使用 ::before 制作的。当子菜单的第一个子菜单处于活动状态时,我需要将其更改为悬停的颜色(具有从 js 自动添加的 .active 类)

选择器应该是什么样的?类似 .submenu a:first-child.active?

CSS:

.submenu{

        z-index:10;
        position:absolute;
        display:none;
        background:#2b2b2b;         
    }


    .submenu a{

        font-family:'Roboto Slab';
        display:block;
        color:white;
        background:transparent;
        text-decoration:none;
        text-transform:uppercase;
        line-height:58px;
        padding-left:8px;
        padding-right:8px;
        font-weight:bold;
        font-size:14px;
    }

    .submenu a:hover{
        color:#fe4817;
        display:block;
        background-color:#3A3939;
    }

    .submenu a.active{
        color:#fe4817;
        background-color:#3A3939;
    }


    .submenu a:first-child:before,
    .submenu a:first-child::before{
        position:absolute;
        top:-8px;
        left:0;
        content:' ';
        width: 0; 
        height: 0; 
        border-left: 15px solid transparent;
        border-right: 15px solid transparent;
        border-bottom: 15px solid #2b2b2b;
    }

    .submenu a:first-child:hover:before,
    .submenu a:first-child:hover::before{
        border-bottom: 15px solid #3A3939;
    }

【问题讨论】:

  • 你能提供html标记吗?或者更好的是,一个小提琴来展示你的问题?
  • 当子菜单的第一个子项(子菜单 a:first-child)具有 .active 类时,我基本上不知道如何选择 ::before 三角形
  • 您似乎知道应该如何做到这一点(从您的帖子中)...您尝试过吗?

标签: html css menu hover


【解决方案1】:

真的很难理解你的问题。但我认为

.submenu a:first-of-type.active:before {
    border-bottom-color:#3a3939;
}

应该做的工作。在这种情况下,:first-of-type 可能比:first-child 更好,因为:first-child 可以是标题或类似的东西,而:first-of-type 只选择第一个锚点。请参阅Selector Reference 了解更多信息。

如果这不起作用,您可以提供 fiddle 我会再看看您的问题。

【讨论】:

  • 我担心的是(在不知道 HTML 的情况下),每个锚标记都是 first,因为它通常是 li 的唯一孩子。我通常希望看到这个三角形是li:first-child:before,而不是锚点......但无论如何+1。
  • 您的回答完美无缺,该选择器正是我所需要的 :) 抱歉没有提琴,考虑到所有 php、js 的事情,真的很难做到。无论如何,谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-05-23
  • 2016-11-30
  • 2017-03-11
  • 1970-01-01
  • 2015-08-14
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
相关资源
最近更新 更多