Link to home
Start Free TrialLog in
Avatar of qeng
qeng

asked on

CSS - How to Center Spry Horiz Menu Widget within Div in both IE7 and FF3

How do I center the entire horizontal spry menu bar within the orange div (please see attached code) such that if I add additional items to the nav bar, the menu bar remains centered (in other words, I don't want to use padding, I want it to self-center, regardless of the menu bar's overall width).

I'm looking for a solution which works in IE7 as well as FF3
<!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" />
<title>Untitled Document</title>
<style type="text/css">
<!--
body {
	font: 100% Verdana, Arial, Helvetica, sans-serif;
	background: #000000;
	margin: 0; /* it's good practice to zero the margin and padding of the body element to account for differing browser defaults */
	padding: 0;
	text-align: center; /* this centers the container in IE 5* browsers. The text is then set to the left aligned default in the #container selector */
	color: #FFFFFF;
}
 
/* Tips for Elastic layouts 
1. Since the elastic layouts overall sizing is based on the user's default fonts size, they are more unpredictable. Used correctly, they are also more accessible for those that need larger fonts size since the line length remains proportionate.
2. Sizing of divs in this layout are based on the 100% font size in the body element. If you decrease the text size overall by using a font-size: 80% on the body element or the #container, remember that the entire layout will downsize proportionately. You may want to increase the widths of the various divs to compensate for this.
3. If font sizing is changed in differing amounts on each div instead of on the overall design (ie: #sidebar1 is given a 70% font size and #mainContent is given an 85% font size), this will proportionately change each of the divs overall size. You may want to adjust based on your final font sizing.
*/
#container {
	width: 57em;
	margin: 0 auto; /* the auto margins (in conjunction with a width) center the page */
	padding: 0px;
	border: 1px solid #000000;
	text-align: left; /* this overrides the text-align: center on the body element. */
	background-color: #0000FF;
}
#topnavbar {
	padding: 0;
/*	margin: -21px 0;*/
	height: 35px;
	text-align: center;
	background-color: #FF6600;
} 
#header {
/*	padding: 0 10px 0 20px;*/  /* this padding matches the left alignment of the elements in the divs that appear beneath it. If an image is used in the #header instead of text, you may want to remove the padding. */
	text-align: center;
	background-color: #FF0000;
} 
#header h1 {
	margin: 0; /* zeroing the margin of the last element in the #header div will avoid margin collapse - an unexplainable space between divs. If the div has a border around it, this is not necessary as that also avoids the margin collapse */
	padding: 10px 0; /* using padding instead of margin will allow you to keep the element away from the edges of the div */
}
#mainContent {
/*	padding: 0 20px;*/
/*	padding: 20px 0;*/
/*	margin: -21px 0;*/
	background-color: #00FF00;
	text-align: center;
}
#footer {
	padding: 0 10px;
	text-align: center;
	background-color: #FF00FF;
} 
#footer p {
	margin: 0; /* zeroing the margins of the first element in the footer will avoid the possibility of margin collapse - a space between divs */
	padding: 10px 0; /* padding on this element will create space, just as the the margin would have, without the margin collapse issue */
}
-->
</style>
<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
</head>
 
<body class="oneColElsCtrHdr">
 
<div id="container">
  <div id="header">
    <h1>Header</h1>
  <!-- end #header --></div>
  <div id="topnavbar">
    <ul id="MenuBar1" class="MenuBarHorizontal">
      <li><a class="MenuBarItemSubmenu" href="#">Item 1</a>
          <ul>
            <li><a href="#">Item 1.1</a></li>
            <li><a href="#">Item 1.2</a></li>
            <li><a href="#">Item 1.3</a></li>
          </ul>
      </li>
      <li><a href="#">Item 2</a></li>
      <li><a class="MenuBarItemSubmenu" href="#">Item 3</a>
          <ul>
            <li><a class="MenuBarItemSubmenu" href="#">Item 3.1</a>
                <ul>
                  <li><a href="#">Item 3.1.1</a></li>
                  <li><a href="#">Item 3.1.2</a></li>
                </ul>
            </li>
            <li><a href="#">Item 3.2</a></li>
            <li><a href="#">Item 3.3</a></li>
          </ul>
      </li>
      <li><a href="#">Item 4</a></li>
    </ul>
  <!-- end #topnavbar --></div>
  <div id="mainContent">
  <img src="Photos/Snowfest_Site_Cover_DSC_0116_r1_1000x669.jpg" width="1000" height="669" />
  <!-- end #mainContent -->
  </div>
  <div id="footer">
    <p>Footer</p>
  <!-- end #footer --></div>
<!-- end #container --></div>
<script type="text/javascript">
<!--
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
//-->
</script>
</body>
</html>

Open in new window

SOLUTION
Avatar of LittleOleMeDesigns
LittleOleMeDesigns

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of qeng
qeng

ASKER

Thanks for your comment,
I can't seem to get your suggestion to work (I may not have interpreted it correctly).  I'll answer your comments in the order of your reply and explain what I did.
The unordered list was already a drop down format (the 'Videos' menu item has 4 submenu items associated with it ... which can be seen in lines 83 to 86 of the attached html file in the code snippet window).  I'm guessing that because I haven't sent the full site files (e.g. you don't have the required java script to fully operate the spry drop down menus) that you're not able to see this behaviour.
You mentioned to create a div which wraps the MenuBar1 and apply either "margin: auto" or "margin: auto" and "text-align:center".  I tried that but it's not working for me.  The menu was still not centering.
I already had the entire top menu in two divs specifically created to wrap the horizontal menu.  The innermost wrapping container "MenuBar1" seems to be controlled by several rules including the main "ul.MenuBarHorizontal", which is the first rule in the external css sheet  "SpryMenuBarHorizontal.css" which can be seen pasted immediately following the html sheet in the code window below (sorry I couldn't attach it as a separate file).  This div was created by the Spry Widget in Dreamweaver CS3.  I first tried modifying it to include "margin: auto" and "text-align:center".  This didn't center the menu.
The other div, "id=topnavbar", I had previously created to wrap the entire spry widget and also to be able to color the background extending horizontally at either end of div MenuBar1.  Applying "margin: auto" and "text-align:center" to this div also didn't center the menu.
Doing a bit more research, I found that replacing the "width:auto" with a fixed width (in this case I used "width: 50em")  in rule "ul.MenuBarHorizontal" does the trick.  The menu now centers in both IE7 and FF3.
I don't understand why the menu remains left justified with the "width:auto" in "ul.MenuBarHorizontal".
Thx
Mike

<!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" />
<title>Untitled Document</title>
<style type="text/css">
<!--
body {
	font: 100% Verdana, Arial, Helvetica, sans-serif;
	background: #000000;
	margin: 0; /* it's good practice to zero the margin and padding of the body element to account for differing browser defaults */
	padding: 0;
	text-align: center; /* this centers the container in IE 5* browsers. The text is then set to the left aligned default in the #container selector */
	color: #FFFFFF;
}
 
/* Tips for Elastic layouts 
1. Since the elastic layouts overall sizing is based on the user's default fonts size, they are more unpredictable. Used correctly, they are also more accessible for those that need larger fonts size since the line length remains proportionate.
2. Sizing of divs in this layout are based on the 100% font size in the body element. If you decrease the text size overall by using a font-size: 80% on the body element or the #container, remember that the entire layout will downsize proportionately. You may want to increase the widths of the various divs to compensate for this.
3. If font sizing is changed in differing amounts on each div instead of on the overall design (ie: #sidebar1 is given a 70% font size and #mainContent is given an 85% font size), this will proportionately change each of the divs overall size. You may want to adjust based on your final font sizing.
*/
#container {
	width: 57em;
	margin: 0 auto; /* the auto margins (in conjunction with a width) center the page */
	padding: 0px;
	border: 1px solid #000000;
	text-align: left; /* this overrides the text-align: center on the body element. */
	background-color: #000000;
}
#header {
	/*	padding: 0 10px 0 20px;*/  /* this padding matches the left alignment of the elements in the divs that appear beneath it. If an image is used in the #header instead of text, you may want to remove the padding. */
	margin: 10px 0;
	text-align: center;
	background-color: #000000;
}
#header h1 {
	margin: 0; /* zeroing the margin of the last element in the #header div will avoid margin collapse - an unexplainable space between divs. If the div has a border around it, this is not necessary as that also avoids the margin collapse */
	padding: 10px 0; /* using padding instead of margin will allow you to keep the element away from the edges of the div */
}
#topnavbar {
	padding: 0;
	margin: auto;
	/*	margin: -21px 0;*/
	height: 35px;
	text-align: center;
	background-color: #000000;
} 
#mainContent {
	/*	padding: 0 20px;*/
	padding: 20px 0;
	margin: 10px 0;
	background-color: #000000;
	text-align: center;
}
#footer {
	font-size: 80%;
	padding: 20px 10px;
	text-align: center;
	background-color: #000000;
} 
#footer p {
	margin: 0; /* zeroing the margins of the first element in the footer will avoid the possibility of margin collapse - a space between divs */
	padding: 10px 0; /* padding on this element will create space, just as the the margin would have, without the margin collapse issue */
}
-->
</style>
<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
</head>
 
<body class="oneColElsCtrHdr">
 
<div id="container">
  <div id="header">
    <h1>Snowfest Idol 2009</h1>
  <!-- end #header --></div>
  <div id="topnavbar">
    <ul id="MenuBar1" class="MenuBarHorizontal">
      <li><a href="#">Home</a>      </li>
      <li><a href="#">About</a></li>
    <li><a class="MenuBarItemSubmenu" href="#">Videos</a>
      <ul>
            <li><a href="#">Niki's 1st Song</a>          </li>
          <li><a href="#">Selection of Finalists</a></li>
          <li><a href="#">Niki's 2nd Song</a></li>
          <li><a href="#">Selection of Winner</a></li>
      </ul>
      </li>
      <li><a href="#">Photos</a></li>
      <li><a href="#">Songs</a></li>
      <li><a href="#">Articles</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  <!-- end #topnavbar --></div>
  <div id="mainContent">
  <img src="Photos/Snowfest_Site_Cover_DSC_0116_r2_900x602.jpg" width="900" height="602" />
  <!-- end #mainContent -->
  </div>
  <div id="footer">
    <p>&copy; Niki Quinlan 2009</p>
  <!-- end #footer --></div>
<!-- end #container --></div>
<script type="text/javascript">
<!--
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
//-->
</script>
</body>
</html>
 
 
----------------------------------------------------------------
EXTERNAL STYLE SHEET "SpryMenuBarHorizontal.css" FOLLOWS
----------------------------------------------------------------
 
@charset "UTF-8";
 
/* SpryMenuBarHorizontal.css - version 0.6 - Spry Pre-Release 1.6.1 */
 
/* Copyright (c) 2006. Adobe Systems Incorporated. All rights reserved. */
 
/*******************************************************************************
 
 LAYOUT INFORMATION: describes box model, positioning, z-order
 
 *******************************************************************************/
 
/* The outermost container of the Menu Bar, a set width box (width: auto prevents the browser from centering the menu) with auto margin and zero padding */
ul.MenuBarHorizontal
{
	/*	margin: 0;*/
	margin: auto;
	padding: 0;
	list-style-type: none;
	font-size: 100%;
	cursor: default;
	width: 50em;
	text-align: center;
}
/* Set the active Menu Bar with this class, currently setting z-index to accomodate IE rendering bug: http://therealcrisp.xs4all.nl/meuk/IE-zindexbug.html */
ul.MenuBarActive
{
	z-index: 1000;
}
/* Menu item containers, position children relative to this container and are a fixed width */
ul.MenuBarHorizontal li
{
	margin: 0;
	padding: 0;
	list-style-type: none;
	font-size: 100%;
	position: relative;
	text-align: center;
	cursor: pointer;
	width: 7em;
	float: left;
	white-space: nowrap;
}
/* Submenus should appear below their parent (top: 0) with a higher z-index, but they are initially off the left side of the screen (-1000em) */
ul.MenuBarHorizontal ul
{
	margin: 0;
	padding: 0;
	list-style-type: none;
	font-size: 90%;
	z-index: 1020;
	cursor: default;
	width: 7em;
	position: absolute;
	left: -1000em;
}
/* Submenu that is showing with class designation MenuBarSubmenuVisible, we set left to auto so it comes onto the screen below its parent menu item */
ul.MenuBarHorizontal ul.MenuBarSubmenuVisible
{
	left: auto;
}
/* Menu item containers are same fixed width as parent */
ul.MenuBarHorizontal ul li
{
	width: 13em;
	text-align: left;
	background-color: transparent;
	float: none;
}
/* Submenus should appear slightly overlapping to the right (95%) and up (-5%) */
ul.MenuBarHorizontal ul ul
{
	position: absolute;
	margin: -5% 0 0 95%;
}
/* Submenu that is showing with class designation MenuBarSubmenuVisible, we set left to 0 so it comes onto the screen */
ul.MenuBarHorizontal ul.MenuBarSubmenuVisible ul.MenuBarSubmenuVisible
{
	left: auto;
	top: 0;
}
 
/*******************************************************************************
 
 DESIGN INFORMATION: describes color scheme, borders, fonts
 
 *******************************************************************************/
 
/* Submenu containers have borders on all sides */
ul.MenuBarHorizontal ul
{
	border: 1px solid #666666;
}
/* Menu items are a light gray block with padding and no text decoration */
ul.MenuBarHorizontal a
{
	display: block;
	cursor: pointer;
	background-color: #333333;
	padding: 0.5em 0.75em;
	color: #CCCCCC;
	text-decoration: none;
}
/* Menu items that have mouse over or focus have a blue background and white text */
ul.MenuBarHorizontal a:hover, ul.MenuBarHorizontal a:focus
{
	background-color: #33C;
	color: #FFF;
}
/* Menu items that are open with submenus are set to MenuBarItemHover with a blue background and white text */
ul.MenuBarHorizontal a.MenuBarItemHover, ul.MenuBarHorizontal a.MenuBarItemSubmenuHover, ul.MenuBarHorizontal a.MenuBarSubmenuVisible
{
	background-color: #33C;
	color: #FFF;
}
 
/*******************************************************************************
 
 SUBMENU INDICATION: styles if there is a submenu under a given menu item
 
 *******************************************************************************/
 
/* Menu items that have a submenu have the class designation MenuBarItemSubmenu and are set to use a background image positioned on the far left (95%) and centered vertically (50%) */
ul.MenuBarHorizontal a.MenuBarItemSubmenu
{
	background-image: url(SpryMenuBarDown.gif);
	background-repeat: no-repeat;
	background-position: 95% 50%;
}
/* Menu items that have a submenu have the class designation MenuBarItemSubmenu and are set to use a background image positioned on the far left (95%) and centered vertically (50%) */
ul.MenuBarHorizontal ul a.MenuBarItemSubmenu
{
	background-image: url(SpryMenuBarRight.gif);
	background-repeat: no-repeat;
	background-position: 95% 50%;
}
/* Menu items that are open with submenus have the class designation MenuBarItemSubmenuHover and are set to use a "hover" background image positioned on the far left (95%) and centered vertically (50%) */
ul.MenuBarHorizontal a.MenuBarItemSubmenuHover
{
	background-image: url(SpryMenuBarDownHover.gif);
	background-repeat: no-repeat;
	background-position: 95% 50%;
}
/* Menu items that are open with submenus have the class designation MenuBarItemSubmenuHover and are set to use a "hover" background image positioned on the far left (95%) and centered vertically (50%) */
ul.MenuBarHorizontal ul a.MenuBarItemSubmenuHover
{
	background-image: url(SpryMenuBarRightHover.gif);
	background-repeat: no-repeat;
	background-position: 95% 50%;
}
 
/*******************************************************************************
 
 BROWSER HACKS: the hacks below should not be changed unless you are an expert
 
 *******************************************************************************/
 
/* HACK FOR IE: to make sure the sub menus show above form controls, we underlay each submenu with an iframe */
ul.MenuBarHorizontal iframe
{
	position: absolute;
	z-index: 1010;
	filter:alpha(opacity:0.1);
}
/* HACK FOR IE: to stabilize appearance of menu items; the slash in float is to keep IE 5.0 from parsing */
@media screen, projection
{
	ul.MenuBarHorizontal li.MenuBarItemIE
	{
	display: inline;
	f\loat: left;
	background: #FFF;
	}
}

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I had been typing for a while when I posted that.... didn't know you had posted an update in the meantime...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of qeng

ASKER

Yessirnosir,
Thanks a lot for your detailed response.  I will probably revisit it on my next page build.  Without studying it right now it seems your approach is more generic than the one I used and will provide for more automatic sizing of the menu containers.
Just for clarification, number of menu elements changes from site to site or page to page.  This is why I was looking to do something more generic that didn't always require that several rules be adjusted each time the lable widths or number of menu items changes (the adjustment of the individual rules is stil a bit cumbersome for me as I'm relatively new to the entire html/css game).
I see as I'm typing this that another comment has been posted.  So this comment of mine may be out of sequence in the thread once I post it.
I'll review the last comment and close off the question.
Thanks again for your help.
Mike
Avatar of qeng

ASKER

yessirnosir, I'll use your code on my next page build (or on the present one if I keep running into snags with the positioning of the submenus relative to the main menu items).  Thx.