Reputation: 525
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
Upvotes: 2
Views: 156
Reputation: 1569
this elements shift is because your elements has defalut margin. Just add in css
ul {
margin:0;
}
but, you'd better add css-reset in the beginning of your stylesheet, for examle this one
/**
* Eric Meyer's Reset CSS v2.0 (http://meyerweb.com/eric/tools/css/reset/)
* http://cssreset.com
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
you should use css reset as all browsers have default css rules which may cause your page to render differently from browser to browser
Upvotes: 1