Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="menu">
  <!-- if we leave href attribute empty or put hash(#) in <a> tag it won't send us to new URL 
       we use <a> tag just to add click event listener in javascript to be able to control clicks 
       clicks will invoke js function which will hide or show "pages"
       "pages" are just divs temporary visible or hidden and hot real URLs
  -->
  <a href="#" id="home-link">Home</a>
  <a href="#" id="about-link">About</a>
  <a href="#" id="articles-link">Articles</a>
</div>
<div id="home">HOME PAGE</div>
<div id="about">ABOUT PAGE</div>
<div id="articles">ARTICLES LIST</div>
              
            
!

CSS

              
                /* we keep two divs visible at a screen
   menu is always visible as navigation part of the app
   home, about, articles - only one of those is visible and others are hidden
   visibility is made with display property
   switching between 'none' for hidden and 'flex' for visible
*/

body {
  margin: 0;
  /* we use flex to be able to center text in home, about, articles divs */
  display: flex;
  flex-flow: column;
  height: 100vh;
}

a {
  color: blue;
  padding: 0.5em;
  border-radius: 5px;
}

#home-link {
  background-color: gray;
}

/* with only a short text in div we want to grow div element to take the rest of visible screen */
#home, #about, #articles {
  flex-grow: 1;
}

.menu {
  padding: 0.5em;
  /* flex to better position links in menu */
  display: flex;
  justify-content: center; 
  align-items: center;
  gap: 3em;
  background-color: lightgray;
  flex: 0 1 auto;
}

#about, #articles {
  display: none;
}

#home {
  background-color: lightgreen;
  display: flex;
  align-items: center; 
  justify-content: center;
}

#about {
  background-color: lightblue;
  align-items: center; 
  justify-content: center;
}

#articles {
  align-items: center; 
  justify-content: center;
  background-color: lightpink;
}

              
            
!

JS

              
                // to keep all menu link elements in one place we create menu object
var menu = {
  home: document.getElementById('home-link'),
  about: document.getElementById('about-link'),
  articles: document.getElementById('articles-link')
};

// pages are in pages object
var pages = {
  home: document.getElementById('home'),
  about: document.getElementById('about'),
  articles: document.getElementById('articles')
};

function show(page) {
  console.log(page);
  // we loop through pages object and menu object to hide or show required page 
  // and set background color in menu links case
  // both objects have same properties so we can loop only pages object
  // in case that we have more pages than menu items which is real world scenario we would need two loops
  for (const property in pages) {
    if (property == page) {
      pages[property].style.display = 'flex';
      menu[property].style.backgroundColor = 'gray';
    } else {
      pages[property].style.display = 'none';
      menu[property].style.backgroundColor = 'transparent';
    }
  }
}

menu.home.addEventListener('click',
  (e) => {
    show('home');
  }
);

menu.about.addEventListener('click',
  (e) => {
    show('about');
  }
);

menu.articles.addEventListener('click',
  (e) => {
    show('articles');
  }
);
              
            
!
999px

Console