CSS tricks: Styling parent elements with :hover

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

Popularity: unranked [?]

share and enjoy:
  • Digg
  • del.icio.us
  • Twitter
  • MySpace
  • Technorati
  • StumbleUpon
  • Fark
  • co.mments
  • Reddit
  • Facebook
  • Live
  • Google Bookmarks
  • Pownce
  • Slashdot
  • FriendFeed
  • Current
  • Netvibes
  • LinkedIn
  • Ping.fm
  • NewsVine
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • email
  • PDF
  • Print
  • RSS

About jeremy

x jeremy jarratt is a blogger, musician, artist, poet, web developer/designer, armchair philosophizer, and teller of tales. He is currently unemployed, although he has plans. BIG plans. Among the things that he has done for a laugh are minor fractures, cuts, scrapes, and various scabs. Though he's quick to point out that he's no imbecile, we're fairly certain that he thinks the word means some kind of medieval pharmacist. This is his latest home on teh intarwebs.
This entry was posted in creative, internets, uncategorized, web design and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>