1

I have tag with class="dropdown-button" and want to style its ::after on first LI element hover.

I am trying this:

<ul class="dropdown-button">
   <li> home </li>
   <li> about us </li>
</ul>

style {

   .dropdown-button {
     margin-top:10px;
     border:1px solid #ccc;
    }  

  .dropdown-button::after {
    border-bottom: 6px solid #FFFFFF;
    border-left: 6px solid rgba(0, 0, 0, 0);
    border-right: 6px solid rgba(0, 0, 0, 0);
    content: "";
    display: inline-block;
    left: 7px;
    position: absolute;
    top: -6px;
   }

   // To solve problem, I do this :
   .dropdown-button li:first-child:hover .dropdown-button::after{
     border-bottom: 6px solid #a1a2a3;
     content: "";
    }

    //But this doesn't work.

}

Any suggestions?

KoboldMines
  • 428
  • 2
  • 6
  • 21

4 Answers4

1

This in js:

var el = $('.dropdown-button');
var firstChild = el.children().first();

firstChild.on('mouseover', onMouseOver);
firstChild.on('mouseout', onMouseOut);

function onMouseOver() {
    el.addClass('first-child-hovered');
}

function onMouseOut() {
    el.removeClass('first-child-hovered');
}

And then in css:

.first-child-hovered::after {
    // change style here
}
karaxuna
  • 26,752
  • 13
  • 82
  • 117
0

Simple code :

.dropdown-button li:first-child:hover::after{
     border-bottom: 6px solid #a1a2a3;
     content: "";
}
Gega Gagua
  • 92
  • 9
0

Well, the deep answer is: affecting a parent's pseudo-class through a child's pseudo-class cannot be done by pure CSS. It's in the name: Cascading Style Sheets only supports styling in cascading direction, not upwards to its parent. So here's a better fix. Introduce a new span element in your

    , give it the :after pseudo-element properties and on hover of the list-item's first-child, display your new element. Solution all in CSS. Please follow the snippet below:

        .dropdown-button {
             margin-top:10px;
             border:1px solid #ccc;
             position: relative;
            }  
        
          .dropdown-button:after {
            border-bottom: 6px solid #FFFFFF;
            border-left: 6px solid rgba(0, 0, 0, 0);
            border-right: 6px solid rgba(0, 0, 0, 0);
            content: "";
            display: inline-block;
            left: 7px;
            position: absolute;
            top: -6px;
            transition: border-bottom 0.5s ease;
            z-index: 0;
           }
        
          .dropdown-button1:after {
            
            border-bottom: 6px solid red;
            border-left: 6px solid rgba(0, 0, 0, 0);
            border-right: 6px solid rgba(0, 0, 0, 0);
            content: "";
            display: none;
            left: 7px;
            position: absolute;
            top: -6px;
            z-index: 3;
           }
           li:first-child:hover ~ .dropdown-button1:after {
             border-bottom: 6px solid #a1a2a3;
             display: inline-block;
        }
        <ul class="dropdown-button">
           <li> home </li>
           <li> about us </li>
           <span class="dropdown-button1"></span>
        </ul>
    JideLambo
    • 91
    • 3
    -1

    You can do this by using jQuery

    $(".dropdown-button").first('li').hover(function(){
       $(this).parent().addClass('hovered');
    })
    
    //CSS
    .hovered:after {
       border-bottom: 6px solid #a1a2a3;
       content: "";
    }
    
    hussain nayani
    • 317
    • 1
    • 3
    • 14