cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <header>
  <div id="main" class="duplicate">
    <ul class="nav">
      <li>
        <a href="javascript://" title="Home">
          <span class="icon-stack">
            <i class="icon-circle icon-stack-base"></i>
            <i class="icon-home icon-light"></i>
          </span>
          <span class="text">Home</span>
        </a>
      </li>
      <li>
        <a href="javascript://" title="About">
          <span class="icon-stack">
            <i class="icon-circle icon-stack-base"></i>
            <i class="icon-leaf icon-light"></i>
          </span>
          <span class="text">About</span>
        </a>
      </li>
      <li>
        <a href="javascript://" title="Contact">
          <span class="icon-stack">
            <i class="icon-circle icon-stack-base"></i>
            <i class="icon-phone icon-light"></i>
          </span>
          <span class="text">Contact</span>
        </a>
      </li>
    </ul>
  </div>
  <div id="stickyContent"></div>
</header>

<div class="contentWrap">
<div class="content">
  <h1>Sticky Header Visual Trick</h1>
  <p>Sticky headers seem to be very popular now, but most of them rely on scroll events. One bad thing about relying on the scrolling event is that mobile safari doesn't execute the function that is attached to the event until after the browser is done scrolling. There is also the <code>position:sticky</code>, but it isn't widely supported yet.</p>
  <p>What you see here is a little visual trick (hack) to do something similar to what you might see in a sticky header. This requires two headers in the markup. This is probably really bad for SEO. Anyways, I have the JS duplicate the menu markup so I don't have to write it twice. The main header is <code>position:relative</code> while the sticky header is <code>position:fixed</code>. The main one has a higher z-index that the sticky header.</p>
  <p>I find that the interaction looks better when the main header is much bigger than the sticky one. Also, having more space between the main header and the content is better. Also, the page needs to be long enough so that the main header clears when you scroll all the way.</p>
</div>
</div>
            
          
!
            
              code {
  background-color: #ddd;
}
.contentWrap {
  padding: 2em;
}
.content {
  width: 650px;
  margin: 0 auto;
  min-height: 1250px;
  line-height: 1.5em;
}
h1 {
  font-size: 2em;
  line-height: 2em;
}

/*** Styles for the main header ***/
	#main {
	  background-color: #eee;
	  position: relative;
	  z-index: 1001;
	}
  ul.nav {
    padding-left: 0;
  }
	ul.nav {
	  width: 650px;
	  margin: 0 auto;
	  overflow: hidden;
	}
	ul.nav li {
	  float: left;
	  list-style: none;
	  margin: .5em .5em .5em 0;
	  padding: .5em .5em .5em 0;
	  font-size: 1.5em;
	}
	ul.nav li a {
	  text-decoration: none;
	  color: #ccc;
	}
	ul.nav li a:hover {
	  color: #E0461B;
	  transition: color 400ms ease-out;
	}
	
/*** Styles for the sticky header ***/
  #stickyContent {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 1000;
    background-color: #E0461B;
  }
  #stickyContent ul.nav li {
    font-size: 1em;
  }
  #stickyContent ul.nav a {
    color: #612717;
  }
  #stickyContent ul.nav .text{
    display: none;
  }
            
          
!
            
              document.getElementById('stickyContent').innerHTML = document.getElementById('main').innerHTML;
            
          
!
999px
Loading ..................

Console