Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <header>
  <nav  class="topnav" id="myTopnav">
    <ul>
      <li><h1><a href="index.html">Site Title</a></h1></li>
      <li><a href="retrieval.html">Retrieval Practice</a></li>
      <li><a href="spaced.html">Spaced Practice</a></li>
      <li><a href="elaboration.html">Elaboration</a></li>
      <li><a href="interleaving.html">Interleaving</a></li>
      <li><a href="concrete.html">Concrete Examples</a></li>
      <li><a href="dualcoding.html">Dual Coding</a></li>
      <li><a href="about.html">About</a></li>
      <li class="icon"><a href="javascript:void(0);" onclick="myFunction()">&#9776;</a></li>
    </ul>
  </nav>
</header>
<article>
  <h1>Re-engineered Responsive Top Navigation</h1>
  <p>This page reworks the following example from W3 Schools: <a href="https://www.w3schools.com/howto/howto_js_topnav_responsive.asp">https://www.w3schools.com/howto/howto_js_topnav_responsive.asp</a>
    
  </p>

<p>The example on the current page has a responsive navigation that uses more standard and accessible markup.</p>
  
  <h2>W3 Schools Markup</h2>
  
  <pre><code>
&lt;div class="topnav" id="myTopnav"&gt;
 &lt;a href="#home"&gt;Home&lt;/a&gt;
 &lt;a href="#news"&gt;News&lt;/a&gt;
 &lt;a href="#contact"&gt;Contact&lt;/a&gt;
 &lt;a href="#about"&gt;About&lt;/a&gt;
 &lt;a href="javascript:void(0);" class="icon" onclick="myFunction()"&gt;&amp;#9776;&lt;/a&gt;
&lt;/div&gt;  
  </code></pre>
  
<p>The two big changes to the above HTML that this example fixes are:</p>
  <ol>
    <li>Wrapping the navigation in the HTML nav element.</li>
    <li>Putting the navigation items in a list.</li>
  </ol>
  
  <p>NAV element: is part of HTML5 which is now usable in over 97% of browsers (<a href="https://caniuse.com/#feat=html5semantic">https://caniuse.com/#feat=html5semantic</a>). This helps screen readers know where the navigation on the page is located.</p>
  <p>Using a list makes it easier to know how many items are in the navigation.</p>
  
  <h2>Modified Markup</h2>
  
  <pre><code>
&lt;nav class="topnav" id="myTopnav"&gt;
 &lt;ul&gt;
   &lt;li&gt;&lt;a href="#home"&gt;Home&lt;/a&gt;&lt;/li&gt;
   &lt;li&gt;&lt;a href="#news"&gt;News&lt;/a&gt;&lt;/li&gt;
   &lt;li&gt;&lt;a href="#contact"&gt;Contact&lt;/a&gt;&lt;/li&gt;
   &lt;li&gt;&lt;a href="#about"&gt;About&lt;/a&gt;&lt;/li&gt;
   &lt;li class="icon"&gt;&lt;a href="javascript:void(0);" onclick="myFunction()"&gt;&amp;#9776;&lt;/a&gt;&lt;/li&gt;
 &lt;/ul&gt;
&lt;/nav&gt;
  </code></pre>
  <p>The main changes have been to:</p>
  <ul>
    <li>Use a nav element around everything instead of a div</li>
    <li>Also wrap the nav items in a list</li>
    <li>Move the classes and ids</li>
    <li>Add to and change the CSS to account for the new elements. Mostly this is removing default list styling and update some of the selectors to refer to the new placement of the anchors inside li elements.</li>
  </ul>
  <p>The JavaScript was not changed.</p>
</article>
              
            
!

CSS

              
                

/* Add a dark grey background color to the top navigation */
.topnav {
    background-color: #333;
    overflow: hidden;
}

/* Remove default list styling: remove bullet points from the list and remove default margin */
.topnav ul{
  list-style-type:none;
  margin:0;
  padding: 0; 
}
.topnav ul li{
  margin:0;
  padding: 0;
}

/* style the site title */
.topnav h1{
  /*this keeps in line with other items in nav */
  margin: 0;
  padding: 0;
  
  /* style this however you want your title styled */
  font-family: sans-serif;
}

/* Style the links inside the navigation bar */
.topnav ul li{
  float: left;
  text-align: center;
}
.topnav a {
  color: #f2f2f2;
  display: block;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

/* Change the color of links on hover */
.topnav a:hover {
    background-color: #ddd;
    color: black;
}

/* Hide the link that should open and close the topnav on small screens */
.topnav .icon {
    display: none;
}

/* When the screen is less than 1000 pixels wide, hide all links, except for the first one ("Home"). Show the link that contains should open and close the topnav (.icon) */
@media screen and (max-width: 1000px) {
  .topnav li:not(:first-child) {
    display: none;
  }
  .topnav li.icon {
    float: right;
    display: block;
  }
}

/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens (display the links vertically instead of horizontally) */
@media screen and (max-width: 1000px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive li.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
/*   .topnav.responsive a { */
  .topnav.responsive li {  
    float: none;
    display: block;
    text-align: left;
  }
}
              
            
!

JS

              
                /* Toggle between adding and removing the "responsive" class to topnav when the user clicks on the icon */
function myFunction() {
    var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }
}
              
            
!
999px

Console