Skip to main content

A sticky navbar for blogger that is not 100% wide

how to fix a blogger navbar without it having to be 100% wide.

I think the first thing I did using JavaScript in Blogger was wa sticky nav. For me this always meant a sticky nav that’s 100% wide. That’s pretty simple to accomplish, but what do we need to do, to make this nav only the width of the content, like we have it in pretty much all of the original blogger templates. With a little plugin a wrote a while ago, this will be done in minutes.

ID, please!

ID, please!

Bevore we can start, we need the ID of the element that we want to have sticky. Easiest way to accomplish that is by right-clicking on the element and inspect it. It’ll give you the code and you can check the ID. If you are using a PageList Widget most probably your ID will be #PageList1.

As soon as you have the ID, you’ll memorize it.

Sticky

Sticky

Getting this sticky is a matter of minutes (maybe even seconds :D) Get to your Code via template edit HTML

Look for </body>

Immediately before that, we’ll add some code. If you already have jQuery added to your code, you do not need to add it again, if you don’t let Google host it for you. Also copy the minified version of my script And then we’ll just write a little bit of jQuery to handle the sizing. Paste above </body> and we are done!

1
<!-- jQuery from Google Hosted Libraries -->
2
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js'/>
3
<script type='text/javascript'>
4
//<![CDATA[
5
6
// jquery.StickyNav.min.js https://gist.github.com/mynimi/d224bbfa487d97102bde
7
!function(e,t,n,r){e.fn.stickyNav=function(n){function r(){return i.prev("div").css("height",i.height()),windowTop=e(t).scrollTop(),i.css({position:windowTop>o?"fixed":"absolute",top:0,left:0})}n=e.extend({wrapperClass:"sticky-nav-wrapper",placeholderClass:"sticky-nav-placeholder",elementClass:"sticky-nav-element",zIndexValue:10},n),this.addClass(n.elementClass),e("."+n.wrapperClass).length||this.wrap('<div class="'+n.wrapperClass+'" style="position: relative; top: 0; left: 0; z-index: '+n.zIndexValue+';"></div>'),e("."+n.placeholderClass).length||this.before('<div class="'+n.placeholderClass+'"></div>');var i=this,o=this.offset().top;return e(t).on("load resize scroll",function(e){r()}),this}}(jQuery,window,document,this);
8
9
$(document).ready( function() {
10
// get original width of menu
11
var menuWidth = $('#PageList1').outerWidth(); // replace #PageList1 with your ID, if it's different.
12
13
// initiate plugin
14
$('.tabs-outer').stickyNav();
15
16
// add width 100% to wrapper element
17
$('.sticky-nav-element').css('width','100%');
18
19
// menu gets original width and centering
20
$('#PageList1').css({
21
'margin': '0 auto',
22
'max-width': menuWidth
23
});
24
});
25
26
//]]>
27
</script>

Search

Results will appear as you type