December 6, 201015 yr Hi, http://www.infekted.org/menutest/test2.html I'm creating a menu with rollover buttons, above. ^ This is what it looks like in Firefox and IE8 and every other browser (other than IE7 and IE6): This is what it looks like in IE7 and IE6: Basically what I tried to do was: 1. Create a menubar div. 2. Create one div (per button), containing one roll-over button functioning as a hyperlink. 3. Make each button div relative. 4. Use absolute positioning to add text to the centre of each button (div) to act as the same hyperlink. 5. Repeat steps 2-4 to create four buttons side-by-side, completing the menu-bar. My Body code is as shown below. All hand-coded, with the aid of Dreamweaver for the javascript roll-over button effect [in the Head portion, shown in the spoiler lower down if needed]. <body onload="MM_preloadImages('images/menu-button-hover.png')"> <div id="container"> <div id="menubar"> <ul> <li> <div class="button"> <a href="page1.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('button-1','','images/menu-button-hover.png',1)"> <img src="images/menu-button.png" alt="Page1" name="page1" width="181" height="23" id="button-1" /> <div class="button-text">button 1</div> </a> </div><!-- end button 1 --> </li> <li> <div class="button"> <a href="page2.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Services','','images/menu-button-hover.png',1)"> <img src="images/menu-button.png" alt="Page2" name="page2" width="181" height="23" id="button-2" /> <div class="button-text">button 2</div> </a> </div><!-- end button 2 --> </li> <li> <div class="button"> <a href="page3.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Candidates','','images/menu-button-hover.png',1)"> <img src="images/menu-button.png" alt="Page3" name="page3" width="181" height="23" id="button-3" /> <div class="button-text">button 3</div> </a> </div><!-- end button 3 --> </li> <li> <div class="button"> <a href="page4.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('contact','','images/menu-button-hover.png',1)"> <img src="images/menu-button.png" alt="Page4" name="page4" width="181" height="23" id="button-4" /> <div class="button-text">button 4</div> </a> </div><!-- end button 4 --> </li> </ul> </div><!-- end menubar div --> </div><!-- end container div --> </body> Here's my CSS, fairly straight-forward I hope: * { margin: 0px; padding: 0px; } body { background-image: url(images/bgslice.png); background-repeat: repeat-x; } img { border: 0px; } #container { width: 728px; margin-right: auto; margin-left: auto; margin-top: 57px; height: 27px; background-color: #000000; } #menubar { height: 23px; width: 728px; margin-left: 2px; margin-top: 2px; float: left; } .button { width: 181px; float: left; position: relative; } .button-text { text-align: center; position: absolute; width: 181px; top: 3px; } li { list-style-type: none; } a { color: #FFFFFF; text-decoration: none; } I've tried just using the buttons (no text) to simplify things in trying to narrow it down, but the buttons themselves are still staggered like a staircase: http://www.infekted.org/menutest/test1.html Here's the head and the javascript (if it is needed): [size="1"]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="test1.css" rel="stylesheet" type="text/css" /> <title>Menu Test</title> <script type="text/javascript"> function MM_swapImgRestore() { //v3.0 var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc; } function MM_preloadImages() { //v3.0 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array(); var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++) if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}} } function MM_findObj(n, d) { //v4.01 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); if(!x && d.getElementById) x=d.getElementById(n); return x; } function MM_swapImage() { //v3.0 var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3) if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];} } </script> </head> Any hints or tips would be very welcome. Or if there are better ways to do it. I wanted to keep file-sizes down, hence using text over a default button. Rather than having two images with graphic text on them for each and every button. Thanks, Timo
December 6, 201015 yr your code is a bit all over the place and its not really the best way to construct a menu, here is a better way of doing it: menu html: <ul id="menubar"> <li><a href="#">button 1 </a></li> <li><a href="#">button 2</a></li> <li><a href="#">button 3</a></li> <li><a href="#"> button 4</a></li> </ul> menu css: ul#menubar { height: 23px; width: 728px; margin-left: 2px; margin-top: 2px; float: left; } ul#menubar li { float:left; list-style:none; width: 181px; } ul#menubar li a { height: 23px; width:181px; display:block; text-align:center; line-height:23px; background-image:url(images/menu-button.png); color:#FFFFFF; text-decoration:none; } ul#menubar li a:hover { background-image:url(images/menu-button-hover.png ) } basically the idea is to use elements to create the menu style through css, instead of adding divs and absolute positioning text ontop of images. + no need for javascript to change the hover state of the image Although the above will cause a flicker when going from one image to the other in hover state, but you can fix that by following this tutorial: http://www.webdesignerforum.co.uk/topic/3420-css-change-img-on-hover/page__view__findpost__p__21844 Hope this helps
December 6, 201015 yr Author I love you. Although I eventually managed to fix it (the "li" wasn't using float:left;), my former code looks like taking a sledgehammer to crack a nut, lol. Your code is about a tenth of the size. I'm embarassed. I'm still getting to grips with just how vastly modular (and therefore powerful) CSS can be. Your advice is gratefully received. Haven't noticed any flickering (maybe my computer is too quick), but will test the graphic scroll hack as well. Thanks ever so much.
December 6, 201015 yr No worries, and no need to be embarrassed, you're learning and this forum is here to help teach you new things when you need it to The images are quite small so they probably won't flicker unless on a really slow computer. But the linked technique will come in handy when you start using larger file sizes.
Create an account or sign in to comment