Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

2 Steps Simple Pure CSS Collapsible Tree Menu

INTRODUCTION
TRADITIONAL GOODNESS

Welcome to a tutorial on how to create a pure CSS collapsible tree menu. Tree menus are one of the most traditional forms of website navigation, and while there are plenty of plugins out there, there sadly don’t seem to have a lot of tutorials for it.

So here it is, a guide that will walk you through how to create one, with just pure CSS – No more hacks, no more bloated 3rd party frameworks, and take the power back to style it as you please. Read on to find out how!

I have included a zip file with all the source code at the end of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

CONFESSION
AN HONEST DISCLOSURE

Quick, hide your wallets! I am an affiliate partner of Google, eBay, Adobe, Bluehost, Clickbank, and more. There are affiliate links and advertisements throughout this website. Whenever you buy things from the evil links that I recommend, I will make a commission. Nah. These are just things to keep the blog going, and allows me to give more good stuff to you guys - for free. So thank you if you decide to pick up my recommendations!


 

NAVIGATION
TABLE OF CONTENTS

Step 1
List to Menu

Step 2
Collapsible Menu

Extra
PHP File List

Extra
Download & More

Closing
What’s Next?

 

STEP 1
LIST TO MENU

Let us first start with some of the basics, of how to create a “normal” vertical menu with HTML and CSS.

THE HTML

1-basic.html


  
    
      Collapsible CSS Tree Menu Demo
    

Yep, this should be pretty straightforward. We are just using an unordered list

    to create the menu here.

    THE CSS

    1-basic.css
    /* [TURN LIST INTO MENU] */
    #menutree {
      margin: 5px;
      padding: 0;
      max-width: 320px; /* Restrict width if you want */
    }
    #menutree li {
      list-style: none;
      padding: 20px 10px;
      background: #B2CCEA;
      border-bottom: 1px solid #86AAD4;
    }
    #menutree li:last-child {
      border: 0;
    }
    #menutree a {
      display: block;
      width: 100%;
      text-decoration: none;
      color: #333;
    }
    /* [DOES NOT MATTER] */
    html, body {
      font-family: arial, sans-serif;
    }

    There is nothing much to the CSS as well, we are merely changing the list to look more like a menu here with some cosmetics.

    THE RESULT

    STEP 2
    COLLAPSIBLE MENU

    Now that we have established the basic menu, let us add on to it, and create a collapsible menu.

    THE HTML

    2-collapse.html
    
    
      
        
          Collapsible CSS Tree Menu Demo
        

    Not much changes here as well:

    • We now put another unordered list inside the list to create the hierarchy.
    • To create the collapsible magic, we will be using a pair of checkbox and label – The rest of the work will be done by CSS, which will be explained below.

    THE CSS

    2-collapse.css
    /* [COLLAPSIBLE] */
    #menutree label {
      position: relative;
      display: block;
      width: 100%;
      cursor: pointer;
    }
    #menutree input[type=checkbox] {
      display: none; /* Hide ugly checkbox */
    }
    
    /* Hide/collapse by default */
    li.collapse ul {
      visibility: hidden;
      opacity: 0;
      max-height: 0; /* CSS bug. Cannot animate height... */
      transition: all 0.5s;
    }
    label::after {
      content: "\25b6";
      position: absolute;
      top: 0;
      right: 0;
      transition: all 0.5s;
    }
    
    /* Show when checked */
    li.collapse input:checked ~ ul {
      visibility: visible;
      opacity: 1;
      max-height: 999px; /* Just give a big enough height for animation */
    }
    li.collapse input:checked ~ label::after {
      transform: rotate(90deg);
    }

    To complete the collapsible menu:

    • We hide the checkbox with CSS, and it will still function normally if we click on the label.
    • By default, we will collapse the internal sub-menu items with visibility: hiddenopacity: 0, and max-height: 0.
    • Also by using label::after, we create an arrow to indicate that this is a collapsed sub-menu item. 
    • When the user clicks on the sub-menu item, we use li.collapse input:checked ~ ul to open it, by reversing visibility: visibleopacity: 1, and max-height: 999px.
    • Finally, we rotate the arrow transform: rotate(90deg) for cosmetics reason.

    THE RESULT

    EXTRA
    PHP FILE LIST

    Now for a small section of extra for you guys who are trying to build a file navigator – This is a small code fragment of how you can use PHP to read a folder and generate a tree menu with it.

    READ AND LIST

    3-list-dir.php
     $v) {
      $basename = basename($k);
      if ($basename != "." && $basename != "..") {
        $path = str_replace($basepath, "", dirname($k));
        if ($path=="") {
          $target =& $objects;
        } else {
          $path = substr($path, 1);
          $path = explode(DIRECTORY_SEPARATOR, $path);
          $evil = "\$target =& \$objects";
          foreach ($path as $p) { $evil .= "['$p']"; }
          eval ( $evil . ";");
        }
        if (is_file($k)) {
          $target[] = $basename;
        } else {
          if (!is_array($target)) {
            $target[$basename] = [];
          }
        }
      }
    }
    
    /* [HTML] */ ?>
    
    
      
        
          Collapsible CSS Tree Menu Demo
        

    Aye, this seems all complicated, but it is actually just some recursive magic at work here.

    EXTRA
    DOWNLOAD & MORE

    That’s all for the code, and here is the download link as promised – Plus a small extra that may be useful to you.

    HAMBURGER MENU

    Looking to build something a little more modern? Check out my other guide on how to create a responsive hamburger menu:

    2 Steps Simple Responsive Pure CSS Hamburger Menu

    SOURCE CODE DOWNLOAD

    Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
     

    CLOSING
    WHAT’S NEXT?

    Thank you for reading, and we have come to the end of this guide. I hope that it has helped you in your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

    The post 2 Steps Simple Pure CSS Collapsible Tree Menu appeared first on Code Boxx.



    This post first appeared on Xxxxxxxxx, please read the originial post: here

    Share the post

    2 Steps Simple Pure CSS Collapsible Tree Menu

    ×

    Subscribe to Xxxxxxxxx

    Get updates delivered right to your inbox!

    Thank you for your subscription

    ×