Categories
creative internets uncategorized web design

CSS tricks: Styling parent elements with :hover

How to style the parent elements in a nested hierarchy of X/HTML elements for use with the :hover pseudoclass, using only a couple of lines of CSS. It’s really simple! See the demo at https://transmothra.com/wp-content/uploads/2008/09/parent-highlighting-with-hover.html

[EDIT: this site no longer supports the features described in this article. Sorry.]

You may have noticed in my sidebar that there are a few elements which are nested in an obvious hierarchical order. For example, at the time of this writing, i have a list of books which i am either reading, planning to read, or have already read, listed under the “now reading” heading.

You may also have noticed that the heading for each of these menu items is highlighted whenever you hover your cursor (pointer) over it.

What you may not have noticed is that the headings for these items’ parent elements is also highlighted when its descendant is hovered over. In other words, when you hover over the “planned books,” “current books,” or “recent books” list, the parent element, “now reading,” is highlighted as well.

(If you’re still not sure just what the hell i’m talking about, check out the demo first, and then come back.)

This is a cool trick that you rarely ever see on the internets, and it’s remarkably simple to do. You don’t need no fancy JavaScripting to do it, either! No server- or client-side scripts are used at all, just good old CSS, and a properly nested hierarchy of elements.

First, create a nested list, with headings. For example:

<ol>
  <li>
    <h1>text</h1>
    <h2>more text</h2>
    <ol>
      <li>
        <h3>subtext1</h3>
      </li>
      <li>
        <h3>subtext2</h3>
        <ol>
          <li>
            <h4>subtext2.1</h4>
          </li>
          <li>
            <h4>subtext2.2</h4>
          </li>
        </ol>
      </li>
      <li>
        <h3>subtext</h3>
      </li>
    </ol>
  </li>
  <li>
    <h2>text again</h2>
  </li>
</ol>

Now create the following style rules:

li:hover>h2, /* matches the H2 element when its parent list-item is hovered over */ li:hover>h3, li:hover>h4, li:hover>h5
	{
	background: #000;
	color: #fed;
	}

(Note that the H1 element is not styled and thus does not actually participate in any fancy hover effects.)

Now save your page and test it out. You can adapt this technique to work with just about any set of nested elements. (Just make sure your nest validates!)

See the demo

By jae

jae lethe (he/she/they) is a blogger, musician, artist, poet, web developer/designer, armchair philosophizer, teller of tales, and gadabout. Also, something he calls a "behavioral artist." (Not sure.) She has plans. BIG plans.

Among the things that he has done for a laugh are minor fractures, cuts, scrapes, and various scabs. Though she's quick to point out that they're no imbecile, we're fairly certain that he thinks the word means some kind of medieval pharmacist.

This is her latest home on teh internets - where jae stores their swear words, when they're not hurling them at the sun in vain.

3 replies on “CSS tricks: Styling parent elements with :hover”

Jeremy – THANK YOU! This is awesome. It never ceases to amaze me how much potential there is with CSS hidden in plain sight. I’ve been looking for this solution for years and it has always evaded me – so thanks again!!

Cool! I’m glad you found it useful. I wish i could implement that on this site again but it really gets crufty trying to insert fairly complicated style rules like that into an existing template.

Comments are closed.