css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <nav id="main_nav"></nav>


<div class="container">
  <h3>Menu modeled in Kentico Cloud</h3>
  <p>No part of the navigation is hardcoded, it's all dynamically generated based on specialized content items retrieved from your Kentico Cloud project. </p>
</div>
            
          
!
            
              /* UNCOMMENT CSS TO TURN THE RESULT INTO A PROPER DROPDOWN MENU */
/*

* {
  margin: 0;
  padding: 0;
}

body {
  font: 300 15px/1.5 "Helvetica Neue", helvetica, arial, sans-serif;
  background: #333;
  margin: 15px;
}

article {
  width: 600px;
  margin: 0 auto;
  background: #000;
  color: #fff;
  border-radius: 5px;
  box-shadow: 0 0 15px 2px #666;
}

section {
  clear: left;
}

h1 {
  font-size: 45px;
  font-weight: 100;
  letter-spacing: 15px;
  text-align: center;
}

h1, #main_content, #dog_link {
  padding: 15px;
}

p {
  margin: 15px 0;
}

a {
  color: #06c;
}

#main_nav ul {
  background: white;
  float: left;
  -webkit-transition: .5s;
  transition: .5s;
}

#main_nav li {
  float: left;
  position: relative;
  width: 150px;
  list-style: none;
  -webkit-transition: .5s;
  transition: .5s;
}

#main_nav > ul > li > a, h1 {
  text-transform: uppercase;
}

#main_nav a {
  display: block;
  text-decoration: none;
  padding: 5px 15px;
  color: #000;
}

#main_nav ul ul {
  position: absolute;
  left: 0;
  top: 100%;
  visibility: hidden;
  opacity: 0;
}

#main_nav ul ul ul {
  left: 100%;
  top: 0;
}

#main_nav li:hover, #main_nav li:hover li {
  background: #ddd;
}

#main_nav li li:hover, #main_nav li li:hover li {
  background: #bbb;
}

#main_nav li li li:hover {
  background: #999;
}

#main_nav li:hover > ul {
  visibility: visible;
  opacity: 1;
}
*/


            
          
!
            
              // Use jQuery to request the Main menu content item using Kentico Cloud Delivery API.
$.ajax({
  type: "GET",
  url: "https://deliver.kenticocloud.com/24246f25-946d-4ab4-b170-fadef22fe7b2/items/root_navigation_item?depth=5",
  dataType: "json",
  success: processData,
  error: function() {
    alert("failed");
  }
});

// Process the retrieved data using the itemsToList function and display it on the page.
function processData(data) {
  var rootMenuItems = data.item.elements.subitems.value;
  var allLinkedContent = data.modular_content;
  
  var menuHtml = itemsToList(rootMenuItems, allLinkedContent, 0);
  $("#main_nav").append(menuHtml);
}

// Limit recursion depth in case of cyclic references between content items. 
// (Otherwise your menu would be infinitely large and your site would crash.)
const DEPTH_LIMIT = 5;

// Generate menu HTML by recursively transforming content items to list elements.
function itemsToList(itemCodenames, linkedContent, currentDepth) {
    if(currentDepth >= DEPTH_LIMIT) {
      return "";
    }
    var result = "<ul>";
    for(let itemCodename of itemCodenames){
      let title = linkedContent[itemCodename].elements.title.value;
      let url = linkedContent[itemCodename].elements.url.value;
      let subitems = linkedContent[itemCodename].elements.subitems.value;
      // console.log(title, url);
      result += "<li><a href='#/"+ url +"' title='/"+ url +"'>" + title + "</a>"
             + itemsToList(subitems, linkedContent, currentDepth+1) +"</li>";  
    }
    result += "</ul>";
    return result;
  }
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console