I'm creating a header that will fix and stay in place once scrolled to a certain number of pixels.
Can I do this using just css and html or do I need jquery too?
I created a demo so you can understand. Any help would be great!
http://jsfiddle.net/gxRC9/4/
body{ margin:0px; padding:0px; } .clear{ clear:both; } .container{ height:2000px; } .cover-photo-container{ width:700px; height: 348px; margin-bottom: 20px; background-color:red; } .small-box{ width:163px; height:100px; border:1px solid blue; float:left; } .sticky-header{ width:700px; height:50px; background:orange; postion:fixed; }
I modified Coop's answer. Please check the example FIDDLE Here are my modifications:
You need some JS to handle scroll events. The best way would be to set a new CSS class for a fixed position that will be assigned to the relevant div when scrolling past a certain point.
HTML
CSS
jQuery
Fiddle example: http://jsfiddle.net/gxRC9/501/
Edit: Extended example
If the trigger point is unknown, but should trigger when the sticky element reaches the top of the screen, you can use
offset().top
.Extended example fiddle: http://jsfiddle.net/gxRC9/502/