4

First of all, I know this question was already asked and answered here: CSS on hover show content

But for some reason, it simply ISN'T working for me! So frustrating... I'll try and keep it brief.

HTML

<ul>
   <li class="servicesfin"><a href=" ">Financial Advising</a></li>
</ul>
<div class="servicesfindesc">
   <p>Offshore Bank accounts, money laundering, hedge funds, tax evasion, investing.</p>
</div>

CSS

.servicesfindesc {
opacity: 0;
visibility: hidden;
}

.servicesfin:hover + .servicesfindesc {
opacity: 1;
visibility: visible;
}
Community
  • 1
  • 1
Willman
  • 438
  • 3
  • 6
  • 12
  • 2
    `+` I don't think will get to `.servicesfindesc` because it is inside the `ul` – Simply Craig Dec 08 '14 at 20:42
  • And by the way, could I change some of these classes to ID's and the selectors/siblings would still work just fine? – Willman Dec 08 '14 at 21:07
  • 1
    Yes, you can interchange classes and IDs all you want. `.servicesfin:hover + .servicesfindesc` is the same as `#servicesfin:hover + #servicesfindesc` or `.servicesfin:hover + #servicesfindesc`. HOWEVER, IDs are for UNIQUE items, do not confuse that. If you need to use it more than once, use a class. – Simply Craig Dec 08 '14 at 21:08

3 Answers3

4

You have to move class to <ul> to make + (adjacent sibling selector) work.

.servicesfindesc {
  opacity: 0;
  visibility: hidden;
}
.servicesfin:hover + .servicesfindesc {
  opacity: 1;
  visibility: visible;
}
<ul class="floatleft servicesfin">
   <li><a href=" ">Financial Advising</a></li>
</ul>
<div class="servicesfindesc">
   <p>Offshore Bank accounts, money laundering, hedge funds, tax evasion, investing.</p>
</div>

If you want to select all next siblings you could use ~ (general sibling selector).

.servicesfindesc {
  opacity: 0;
  visibility: hidden;
}
.servicesfin:hover ~ .servicesfindesc {
  opacity: 1;
  visibility: visible;
}
<ul class="floatleft servicesfin">
   <li><a href=" ">Financial Advising</a></li>
</ul>
<div class="servicesfindesc">
   <p>Offshore Bank accounts, money laundering, hedge funds, tax evasion, investing.</p>
</div>
<div class="servicesfindesc">
   <p>Offshore Bank accounts, money laundering, hedge funds, tax evasion, investing.</p>
</div>
<div class="servicesfindesc">
   <p>Offshore Bank accounts, money laundering, hedge funds, tax evasion, investing.</p>
</div>
<div class="servicesfindesc">
   <p>Offshore Bank accounts, money laundering, hedge funds, tax evasion, investing.</p>
</div>

Reference: Adjacent sibling selectors - General sibling selectors

emmanuel
  • 9,607
  • 10
  • 25
  • 38
– emmanuel Dec 08 '14 at 20:57
  • So instead of using ul's I could just turn the li's into div's and style them the same as li's so everything would share a common parent? – Willman Dec 08 '14 at 21:03
  • But that wouldn't be ideal for accessibility... But you're saying I should do `
  • Financial Services
  • ` – Willman Dec 08 '14 at 21:09
  • You could add: `
  • Financial Advising

    ...

  • `. – emmanuel Dec 08 '14 at 21:11