I’ve implemented a sticky header here.
However, when it reaches it’s sticking point the text moves down, out of the container.
I’d like for it to stick within the container.
My HTML & Jq:
<div id="container">
<div id="menu">
<ul>
<li><a href='#'>Line 1</a></li>
<li><a href='#'>Line 2</a></li>
<li><a href='#'>Line 3</a></li>
</ul>
</div>
</div>
My script:
<script type="text/javascript">
$(document).ready(function(){
$(window).scroll(function(){
if($(this).scrollTop()>=575)
{
$('#menu').addClass('fixed');
}else{
$('#menu').removeClass('fixed');
}
});
});
</script>
CSS:
#menu{
height: 35px;
background-color:brown;
z-index:200;
}
#menu ul li{
float: left;
list-style: none;
line-height: 35px;
}
#menu a {
color: #fff;
text-decoration: none;
font-size 16px;
font-weight: bold;
padding: 0 20px;
}
#menu.fixed{
position: fixed;
top:0;
width: 720px;
}
#container{
width: 720px;
margin: 0 auto;
}
Thanks in advance
Michael
this elements shift is because your elements has defalut margin. Just add in css
but, you’d better add css-reset in the beginning of your stylesheet, for examle this one
you should use css reset as all browsers have default css rules which may cause your page to render differently from browser to browser