1

I'm referring to this question: Hover behavior for a dl list in CSS

The accepted answer will only highlight the first <dd> following a <dt>, but when there are multiple <dd>s only the first (adjacent) one is affected.

I've looked at the combinators: https://www.w3schools.com/css/css_combinators.asp but none really cover multiple <dd>s since they are not actually descendant or child elements of the <dt>.

Is there a CSS solution that will affect all <dd>s following a <dt>?

Loren Maxwell
  • 307
  • 3
  • 12

2 Answers2

2

The key point is

/* change color of dd siblings of the hovered dt */
dt:hover ~ dd { 
  color: white;
}

/* set color:initial for dd siblings of dt siblings of the hovered dt */
dt:hover ~ dt ~ dd { 
  color: initial;
}

Please look at this snippet (I've updated the referred answer):

dl {
  overflow: hidden;
}

dt {
  height: 50em;
  margin-bottom: -48.8em;
  background: white;
}

dt:hover {
  background: #333;
  color: white;
}


dd {
  pointer-events: none;
}

dt:hover ~ dd {
  color: white;
}

dt:hover ~ dt ~ dd {
  color: initial;
}
<dl>
  <dt>Term 1</dt>
  <dd>1st Definition of term 1<br>&hellip;<br>&hellip;</dd>
  <dd>2nd Definition of term 1<br>&hellip;<br>&hellip;</dd>
  <dd>3rd Definition of term 1<br>&hellip;<br>&hellip;</dd>
  
  <dt>Term 2</dt>
  <dd>Definition of term 2<br>&hellip;</dd>
  
  <dt>Term3 </dt>
  <dd>Definition of term 3</dd>
</dl>
Kosh
  • 16,966
  • 2
  • 19
  • 34
  • 2
    While technically correct, code-only answers aren't quite as useful. It would be nice if you would explain the [general sibling combinator](https://css-tricks.com/child-and-sibling-selectors/) – random_user_name Jan 06 '18 at 00:45
1

Try using an nth-of-type selector to select the dd's in combination with the generic sibling selector.

dt:hover,
dt:hover ~ dd:nth-of-type(1),
dt:hover ~ dd:nth-of-type(2), 
dt:hover ~ dd:nth-of-type(3) {
    background:#ddd;
}
Jonathan Chaplin
  • 2,442
  • 1
  • 18
  • 21