I'm absolutely fascinated by the new opportunities offered by CSS3 and I never stop testing these new features. In this article you'll see how to create a pure CSS folder tree menu. Let's see the details.
We start with the following markup structure:
<ul id="folder"> <li> <a href="#sub-1">1</a><span>Folder 1</span> <ul id="sub-1"> <li>A</li> <li>B</li> <li>C</li> </ul> </li> <li> <a href="#sub-2">1</a><span>Folder 2</span> <ul id="sub-2"> <li>D</li> <li>E</li> <li>F</li> </ul> </li> </ul>
As you can see, each outer link has an hash that points to the innermost list. By doing so, we can use the :target
pseudo-class to create our expanding effect:
#folder { margin: 2em; } #folder > li {margin-bottom: 0.5em;} #folder > li > a { display: inline-block; width: 16px; height: 16px; text-indent: -1000em; background: url(icons.png) no-repeat 0 -32px; margin-right: 5px; } #folder > li > a:active {background-position: 0 -16px;} #folder > li > a + span { display: inline-block; height: 16px; line-height: 16px; padding-left: 20px; font-weight: bold; background: url(icons.png) no-repeat 0 0; } #folder > li ul { margin-left: 2.5em; line-height: 1.4; display: none; } #folder > li ul:target { display: block; }
You can see the demo below.