Advertise
  • Home
  • Menus
  • Blog
  • Contact

Cookies are currently necessary for this site to operate properly.

    Categories

    • Development (9)
    • Extras (3)
    • General (6)
    • Tutorials (6)

    Archives

    • April 2010 (3)
    • November 2009 (4)
    • October 2008 (1)
    • September 2008 (8)
    • August 2008 (1)
    • July 2008 (4)

    Most Recent Posts

    • 5 Free CSS Menu Builders
    • CSS Menu Builder Video Tutorial
    • Site quite unstable – FIXED
    • Centering a menu using either CSS, MooTools, jQuery or just plain JavaScript
    • 50.000 downloads since launch

    My Writing

    If you don't like dry sarcastic humour, please don't read any more of this blog, because I have a tendency to use it without thinking about it.

    This blog will mainly be used as a development log, since I am not a big blogger and don't really have anything worth saying, though I might write a few tutorials and suggested usage articles along the way.

    Oh by the way, in this blog, which is currently the only blog I am active on, I go by the original name of admin. I will not be commenting that much, but when I do, this is the name you will see.

Centering a menu using either CSS, MooTools, jQuery or just plain JavaScript

One of the questions people have frequently asked me, is how do I center the menu. My answer to all have been that I do not know how to do this in CSS, as the list element occupies the full width of its container. So the only way would be to manually set the width of the list element.

The other day I started thinking that this could be done dynamically by using javascript. Of course some people will probably not like this method, so you should stop reading now.

I have created 3 examples, for people using either MooTools, jQuery or just plain JavaScript, you can see them below as well as examples.

Please note that all 3 examples require that you set the ID of the containing div element, this is so that a background image can be added to this element.

Update while writing
Of course while writing this tutorial, I found an article which explains how to do this using CSS, but since I had already written the tutorial, you will get all 4 examples.

CSS way

Based on the article by Matthew James Taylor

CSS

#menu{text-align:center; width:100%; position:relative; overflow:hidden; background:url('topMenuImages.png') repeat-x; }

.menu{margin:0; padding:0; height:30px; background:url('topMenuImages.png') repeat-x; left:50%;clear:left; float:left; position:relative;}
.menu li{padding:0; margin:0; list-style:none; display:block; float:left; position:relative; right:50%;}
.menu li a{float:left; padding-left:15px; display:block; color:rgb(255,255,255); text-decoration:none; font:12px Verdana, Arial, Helvetica, sans-serif; cursor:pointer; background:url('topMenuImages.png') 0px -30px no-repeat;}
.menu li a span{line-height:30px; float:left; display:block; padding-right:15px; background:url('topMenuImages.png') 100% -30px no-repeat;}
.menu li a:hover{background-position:0px -60px; color:rgb(255,255,255);}
.menu li a:hover span{background-position:100% -60px;}
.menu li a.active, .menu li a.active:hover{line-height:30px; font:12px Verdana, Arial, Helvetica, sans-serif; background:url('topMenuImages.png') 0px -90px no-repeat; color:rgb(255,255,255);}
.menu li a.active span, .menu li a.active:hover span{background:url('topMenuImages.png') 100% -90px no-repeat;}

HTML

<div id="menu">
    <ul class="menu">
      <li><a href="/home" class="active"><span>Home</span></a></li>
      <li><a href="/products"><span>Products</span></a></li>
      <li><a href="/services"><span>Services</span></a></li>
      <li><a href="/about"><span>About</span></a></li>
      <li><a href="/contact"><span>Contact</span></a></li>
    </ul>
</div>

Example Centering a menu using CSS

MooTools

window.addEvent('domready', function() {
	var menuWrapID = 'menu'; // container element ID
	var menusize = 0;
	// get width of each anchor tag and sum
	$$('#'+menuWrapID+' .menu li a').each(function(el) {
		menusize += el.getSize().x;
	});
	// set unsorted list width and container background image
	var mbg = $$('.menu').getStyle('background-image');
	$(menuWrapID).setStyle('background-image',mbg);
	$$('.menu').setStyle('width',menusize);
});

Example Centering a menu using MooTools

jQuery

$(document).ready(function(){
	var menuWrapID = 'menu'; // container element ID
	var menusize = 0;
	// get width of each anchor tag and sum
	$('#'+menuWrapID+' a').each(function(i, el) {
		menusize += $(el).outerWidth();
	});
	// set unsorted list width and container background image
	var mbg = $('.menu').css('background-image');
	$('#'+menuWrapID).css('background-image',mbg);
	$('.menu').css('width',menusize+'px');
});

Example Centering a menu using jQuery

JavaScript

function menuResize() {
	var menuWrapID = 'menu'; // container element ID
	var menusize = 0;
	// get width of each anchor tag and sum
	var els = document.getElementById(menuWrapID).getElementsByTagName('a');
	for(var i = 0; i < els.length; i++) {
		var size = els[i].clientWidth;
		menusize = menusize + size;
	}
	// set unsorted list width and get its background image
	var uls = document.getElementById(menuWrapID).getElementsByTagName('ul');
	for(var i = 0; i < uls.length; i++) {
		uls[i].style.width = menusize+'px';
		var browserName=navigator.appName;
		// if FireFox, Chrome, Opera, Safari
		if (browserName==="Netscape" || browserName==="Opera") {  var bgi = getComputedStyle(uls[i],'').getPropertyValue('background-image');}
		// if Internet Explorer
		else if (browserName==="Microsoft Internet Explorer") {  var bgi = uls[i].currentStyle.backgroundImage; }
	}
	// set background image to menu div
	document.getElementById(menuWrapID).style.backgroundImage = bgi;
}

window.onload = menuResize;

Example Centering a menu using JavaScript

In the plain JavaScript version, it relies on the onload event, but you might want to add the excellent domready.js script to your page, so that the change is done when the dom is ready.

November 14th, 2009
Comments (2)
Filed under: Tutorials

2 Responses to “Centering a menu using either CSS, MooTools, jQuery or just plain JavaScript”

  • Comments
  • Pings
  1. annamae says:
    November 14, 2009 at 14:10

    Hi,
    nice but there is an error in Mootools.
    you should add +1 to have it working :
    menusize += el.getSize().x+1;

  2. admin says:
    November 14, 2009 at 15:32

    Hi Annamae,

    I had a look at what you said, but when I calculate the width manually, using FF’s developer toolbar’s inspect element function, it gives the same result. The value I get is 379 pixel, which is the same as the one MooTools gets and again the same as the jQuery script and the plain Javascript get.

    So if you would be so kind as to elaborate on the error that you got and explain why it is an error, it would be much appreciated.

    By the way, thank you for reading the article.

    /olaf

Leave a Reply

Click here to cancel reply.

Info

  • Information for advertisers
  • Terms of Service
  • Privacy
  • License

About

This is my small contribution to the world of web designers, programmers, web enthusiasts and others who have helped me or need help or just don't want to take the time to learn how to do this in photoshop etc.

These menus are licensed under the MIT license

Clicky Web Analytics
V:II | XHTML | CSS | Bluehost | GA & Clicky
2008 - 2010 | This is a [Super Simple Services] site.