1

I am currently trying to change the properties of another object in css but am not sure if I am doing this right. Any help would be appreciated.

ul.drop-menu {
  opacity: 0.1;
}

nav > ul > li:hover ~ ul.drop-menu {
  opacity: 1;
}
<nav>
    <ul>
        <li>Test
            <ul class="drop-menu">
                <li class="1">Test1</li>
                <li class="2">Test2</li>
                <li class="3">Test3</li>
                <li class="4">Test4</li>
                <li class="5">Test5</li>
                <li class="6">Test6</li>
                <li class="7">Test7</li>
            </ul>
        </li>
    </ul>
</nav>
csharpbd
  • 3,786
  • 4
  • 23
  • 32
alexo1001
  • 177
  • 2
  • 2
  • 11

2 Answers2

3

~ is a "general sibling selector", so it will match any element that is a sibling that comes after the element on the left side of the selector.

With your markup, you either just want to remove ~ to target any ul.drop-menu that is a child of li, or if you want the direct descendent, change it to a >. I'm assuming you want the latter, in case you nest multiple .drop-menu's in li's. I added another nested menu and changed your selector a little to demonstrate that.

ul.drop-menu {
  opacity: 0.1;
}

nav > ul li:hover > ul.drop-menu {
  opacity: 1;
}
<nav>
  <ul>
    <li>Test
      <ul class="drop-menu">
        <li class="1">Test1</li>
        <li class="2">Test2
          <ul class="drop-menu">
            <li class="1">Test1</li>
            <li class="2">Test2</li>
            <li class="3">Test3</li>
            <li class="4">Test4</li>
            <li class="5">Test5</li>
            <li class="6">Test6</li>
            <li class="7">Test7</li>
          </ul>
        </li>
        <li class="3">Test3</li>
        <li class="4">Test4</li>
        <li class="5">Test5</li>
        <li class="6">Test6</li>
        <li class="7">Test7</li>
      </ul>
    </li>
  </ul>
</nav>
Michael Coker
  • 52,626
  • 5
  • 64
  • 64
  • Worked just like I wanted it to. I'm new to front-end dev and I'm not very good with css in general since I just jumped right into it. This won't happen if I take some time out of my day to look at things like this though. – alexo1001 Apr 19 '17 at 21:29
  • 1
    @alexo1001 word up. fwiw, `~` is a general sibling selector, and it's partner in crime is `+`, which is an adjacent sibling selector. Doesn't really apply to your answer, but if you're in a learning mood... https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors – Michael Coker Apr 19 '17 at 21:33
  • Thanks, I'll go and check it out @MichealCoker – alexo1001 Apr 19 '17 at 21:34
1

You have to use > instead of ~. Please check this:

ul.drop-menu {
  opacity: 0.1;
}

nav > ul > li:hover > ul.drop-menu {
  opacity: 1;
}
<nav>
    <ul>
        <li>Test
            <ul class="drop-menu">
                <li class="1">Test1</li>
                <li class="2">Test2</li>
                <li class="3">Test3</li>
                <li class="4">Test4</li>
                <li class="5">Test5</li>
                <li class="6">Test6</li>
                <li class="7">Test7</li>
            </ul>
        </li>
    </ul>
</nav>
csharpbd
  • 3,786
  • 4
  • 23
  • 32