Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to center a div 1

Status
Not open for further replies.

yesti

MIS
Dec 8, 2000
166
US
Hello,
I found this script on javascript.internet.com and I wanted to know if there is a way to center the cascading menu. The script has an external .css and .js as well as code in the html:

.css:

.menuBar
{
LEFT: 125;
POSITION: relative;
BACKGROUND-COLOR: transparent;
TEXT-ALIGN: center;
COLOR: #FFFFFF;
FONT-WEIGHT: bold;
}
.Bar
{
BORDER-RIGHT: blue 1px outset;
BORDER-TOP: blue 1px outset;
FLOAT: left;
BORDER-LEFT: blue 1px outset;
WIDTH: 100px;
CURSOR: hand;
TEXT-INDENT: 5px;
BORDER-BOTTOM: blue 1px outset;
POSITION: relative;
BACKGROUND-COLOR: #0000FF;
TEXT-ALIGN: center
}
.menu
{
BORDER-RIGHT: buttonhighlight thin outset;
BORDER-TOP: buttonhighlight thin outset;
VISIBILITY: hidden;
BORDER-LEFT: buttonhighlight thin outset;
WIDTH: 150px;
LINE-HEIGHT: 140%;
BORDER-BOTTOM: buttonhighlight thin outset;
POSITION: absolute;
BACKGROUND-COLOR: deepskyblue
}
.menuItem
{
PADDING-RIGHT: 15px;
PADDING-LEFT: 10px;
WIDTH: 100%;
CURSOR: hand;
LINE-HEIGHT: 20px;
BORDER-BOTTOM: white 1px solid
}
.ItemMouseOver
{
PADDING-RIGHT: 15px;
PADDING-LEFT: 10px;
WIDTH: 100%;
CURSOR: hand;
COLOR: highlighttext;
LINE-HEIGHT: 20px;
BORDER-BOTTOM: white 1px solid;
BACKGROUND-COLOR: highlight
}
.ItemMouseOut
{
WIDTH: 100%
}
.Arrow
{
FLOAT: right;
FONT-FAMILY: Webdings;
POSITION: absolute;
TEXT-ALIGN: left
}
.barOver
{
BORDER-RIGHT: blue 1px ridge;
BORDER-TOP: blue 1px ridge;
FLOAT: left;
BORDER-LEFT: blue 1px ridge;
WIDTH: 100px;
CURSOR: hand;
TEXT-INDENT: 5px;
BORDER-BOTTOM: blue 1px ridge;
POSITION: relative;
BACKGROUND-COLOR: deepskyblue;
TEXT-ALIGN: center
}

.js:


function InitMenu()
{
var bar = menuBar.children
for(var i=0;i < bar.length;i++)
{
var menu=eval(bar.menu)
menu.style.visibility = &quot;hidden&quot;
bar.onmouseover = new Function(&quot;ShowMenu(&quot;+bar.id+&quot;)&quot;)
var Items = menu.children
for(var j=0; j<Items.length; j++)
{
var menuItem = eval(Items[j].id)
if(menuItem.menu != null)
{
menuItem.innerHTML += &quot;<Span Id=&quot;+menuItem.id+&quot;_Arrow class='Arrow'>4</Span>&quot;
FindSubMenu(menuItem.menu)}
if(menuItem.cmd != null)
{
menuItem.onclick = new Function(&quot;Do(&quot;+menuItem.id+&quot;)&quot;) }
menuItem.onmouseover = new Function(&quot;highlight(&quot;+Items[j].id+&quot;)&quot;)
}
}
}
function FindSubMenu(subMenu)
{
var menu=eval(subMenu)
var Items = menu.children
for(var j=0; j<Items.length; j++)
{
menu.style.visibility = &quot;hidden&quot;
var menuItem = eval(Items[j].id)
if(menuItem.menu!= null)
{
menuItem.innerHTML += &quot;<Span Id=&quot;+menuItem.id+&quot;_Arrow class='Arrow'>4</Span>&quot;
FindSubMenu(menuItem.menu)
}
if(menuItem.cmd != null)
{
menuItem.onclick = new Function(&quot;Do(&quot;+menuItem.id+&quot;)&quot;) }
menuItem.onmouseover = new Function(&quot;highlight(&quot;+Items[j].id+&quot;)&quot;)
}
}
function ShowMenu(obj)
{
HideMenu(menuBar)
var menu = eval(obj.menu)
var bar = eval(obj.id)
bar.className=&quot;barOver&quot;
menu.style.visibility = &quot;visible&quot;
menu.style.pixelTop = obj.getBoundingClientRect().top + obj.offsetHeight + Bdy.scrollTop
menu.style.pixelLeft = obj.getBoundingClientRect().left + Bdy.scrollLeft
}
function highlight(obj)
{
var PElement = eval(obj.parentElement.id)
if(PElement.hasChildNodes() == true)
{ var Elements = PElement.children
for(var i=0;i<Elements.length;i++)
{
TE = eval(Elements.id)
TE.className = &quot;menuItem&quot;
}
}
obj.className=&quot;ItemMouseOver&quot;
window.defaultStatus = obj.title
ShowSubMenu(obj)
}
function Do(obj)
{
var cmd = eval(obj).cmd
window.navigate(cmd)
}
function HideMenu(obj)
{
if(obj.hasChildNodes()==true)
{
var child = obj.children
for(var j =0;j<child.length;j++)
{
if (child[j].className==&quot;barOver&quot;)
{var bar = eval(child[j].id)
bar.className=&quot;Bar&quot;}
if(child[j].menu != null)
{
var childMenu = eval(child[j].menu)
if(childMenu.hasChildNodes()==true)
HideMenu(childMenu)
childMenu.style.visibility = &quot;hidden&quot;
}
}
}
}
function ShowSubMenu(obj)
{
PMenu = eval(obj.parentElement.id)
HideMenu(PMenu)
if(obj.menu != null)
{
var menu = eval(obj.menu)
menu.style.visibility = &quot;visible&quot;
menu.style.pixelTop = obj.getBoundingClientRect().top + Bdy.scrollTop
menu.style.pixelLeft = obj.getBoundingClientRect().right + Bdy.scrollLeft
if(menu.getBoundingClientRect().right > window.screen.availWidth )
menu.style.pixelLeft = obj.getBoundingClientRect().left - menu.offsetWidth
}
}

html:

<head>
<link href=&quot;../_script/CascadeMenu.css&quot; rel=&quot;stylesheet&quot;>
<script language=&quot;javascript&quot; src=&quot;../_script/CascadeMenu.js&quot;>
</script>
</head>
<body OnLoad=&quot;InitMenu()&quot; Onclick=&quot;HideMenu(menuBar)&quot; ID=&quot;Bdy&quot;>
<DIV Id=&quot;menuBar&quot; class=&quot;menuBar&quot;>
<DIV Id=&quot;Bar1&quot; class=&quot;Bar&quot; menu=&quot;menu1&quot;>Approve/Update/Generate</DIV>
<DIV Id=&quot;Bar2&quot; class=&quot;Bar&quot; menu=&quot;menu2&quot;>Received</DIV>
<DIV Id=&quot;Bar3&quot; class=&quot;Bar&quot; menu=&quot;menu3&quot;>Update</DIV>
<DIV Id=&quot;Bar4&quot; class=&quot;Bar&quot; menu=&quot;menu4&quot;>Reports</DIV>
</DIV>
<div Id=&quot;menu1&quot; class=&quot;menu&quot; >
<div Id=&quot;menuItem1_1&quot; class=&quot;menuItem&quot; title=&quot;Approve PO&quot; cmd=&quot;group1/approval.asp&quot;>Approve PO</div>
<div Id=&quot;menuItem1_2&quot; class=&quot;menuItem&quot; title=&quot;Update PO&quot; cmd=&quot;updatePO.asp&quot;>Update PO</div>
<div Id=&quot;menuItem1_3&quot; class=&quot;menuItem&quot; title=&quot;Generate PO&quot; cmd=&quot;group2/generatepo.asp&quot;>Generate PO</div>
</div>
<div Id=&quot;menu2&quot; class=&quot;menu&quot;>
<div Id=&quot;menuItem2_1&quot; class=&quot;menuItem&quot; title=&quot;Mark as Received&quot; cmd=&quot;group3/recv.asp&quot;>Mark as Recd</div>
<div Id=&quot;menuItem2_2&quot; class=&quot;menuItem&quot; title=&quot;Not Received Yet&quot; cmd=&quot;group1a/notrecd.asp&quot;>Not Recd Yet</div>
<div Id=&quot;menuItem2_3&quot; class=&quot;menuItem&quot; title=&quot;PO Comments&quot; cmd=&quot;group1a/pocomments.asp&quot;>PO Comments</div>
</div>
<div Id=&quot;menu3&quot; class=&quot;menu&quot;>
<div Id=&quot;menuItem3_1&quot; class=&quot;menuItem&quot; title=&quot;Received, not Paid&quot; cmd=&quot;group1a/notpaid.asp&quot;>Recd, Not Paid</div>
<div Id=&quot;menuItem3_2&quot; class=&quot;menuItem&quot; title=&quot;Order Numbers&quot; cmd=&quot;group3/ordernum.asp&quot;>Order Numbers</div>
</div>
<div Id=&quot;menu4&quot; class=&quot;menu&quot;>
<div Id=&quot;menuItem4_1&quot; class=&quot;menuItem&quot; title=&quot;Paid Items&quot; cmd=&quot;group1a/paid.asp&quot;>Paid Items</div>
<div Id=&quot;menuItem4_2&quot; class=&quot;menuItem&quot; title=&quot;Find Item&quot; cmd=&quot;finditem.asp&quot;>Find Item</div>
<div Id=&quot;menuItem4_3&quot; class=&quot;menuItem&quot; title=&quot;View Received Items&quot; cmd=&quot;viewrecd.asp&quot;>View Recd</div>
<div Id=&quot;menuItem4_4&quot; class=&quot;menuItem&quot; title=&quot;Approved, Not Ordered&quot; cmd=&quot;appnotord.asp&quot;>Not Ordered</div>
<div Id=&quot;menuItem4_5&quot; class=&quot;menuItem&quot; title=&quot;Cancelled Items&quot; cmd=&quot;cancelled.asp&quot;>Cancelled</div>
</div>

I'm really new to css and using divs so it's taking me a while to figure this out. Thanks.
 
Just replace the old html part with this one. There were errors in the code when I copied it. Those haven't been fixed.


<head>
<link href=&quot;../_script/CascadeMenu.css&quot; rel=&quot;stylesheet&quot;>
<script language=&quot;javascript&quot; src=&quot;../_script/CascadeMenu.js&quot;>
</script>
</head>
<body OnLoad=&quot;InitMenu()&quot; Onclick=&quot;HideMenu(menuBar)&quot; ID=&quot;Bdy&quot;>
<center>
<div style=&quot;width:170;text-align:left;&quot;>
<DIV Id=&quot;menuBar&quot; class=&quot;menuBar&quot;>
<DIV Id=&quot;Bar1&quot; class=&quot;Bar&quot; menu=&quot;menu1&quot;>Approve/Update/Generate</DIV>
<DIV Id=&quot;Bar2&quot; class=&quot;Bar&quot; menu=&quot;menu2&quot;>Received</DIV>
<DIV Id=&quot;Bar3&quot; class=&quot;Bar&quot; menu=&quot;menu3&quot;>Update</DIV>
<DIV Id=&quot;Bar4&quot; class=&quot;Bar&quot; menu=&quot;menu4&quot;>Reports</DIV>
</DIV>
<div Id=&quot;menu1&quot; class=&quot;menu&quot;>
<div Id=&quot;menuItem1_1&quot; class=&quot;menuItem&quot; title=&quot;Approve PO&quot; cmd=&quot;group1/approval.asp&quot;>Approve PO</div>
<div Id=&quot;menuItem1_2&quot; class=&quot;menuItem&quot; title=&quot;Update PO&quot; cmd=&quot;updatePO.asp&quot;>Update PO</div>
<div Id=&quot;menuItem1_3&quot; class=&quot;menuItem&quot; title=&quot;Generate PO&quot; cmd=&quot;group2/generatepo.asp&quot;>Generate PO</div>
</div>
<div Id=&quot;menu2&quot; class=&quot;menu&quot;>
<div Id=&quot;menuItem2_1&quot; class=&quot;menuItem&quot; title=&quot;Mark as Received&quot; cmd=&quot;group3/recv.asp&quot;>Mark as Recd</div>
<div Id=&quot;menuItem2_2&quot; class=&quot;menuItem&quot; title=&quot;Not Received Yet&quot; cmd=&quot;group1a/notrecd.asp&quot;>Not Recd Yet</div>
<div Id=&quot;menuItem2_3&quot; class=&quot;menuItem&quot; title=&quot;PO Comments&quot; cmd=&quot;group1a/pocomments.asp&quot;>PO Comments</div>
</div>
<div Id=&quot;menu3&quot; class=&quot;menu&quot;>
<div Id=&quot;menuItem3_1&quot; class=&quot;menuItem&quot; title=&quot;Received, not Paid&quot; cmd=&quot;group1a/notpaid.asp&quot;>Recd, Not Paid</div>
<div Id=&quot;menuItem3_2&quot; class=&quot;menuItem&quot; title=&quot;Order Numbers&quot; cmd=&quot;group3/ordernum.asp&quot;>Order Numbers</div>
</div>
<div Id=&quot;menu4&quot; class=&quot;menu&quot;>
<div Id=&quot;menuItem4_1&quot; class=&quot;menuItem&quot; title=&quot;Paid Items&quot; cmd=&quot;group1a/paid.asp&quot;>Paid Items</div>
<div Id=&quot;menuItem4_2&quot; class=&quot;menuItem&quot; title=&quot;Find Item&quot; cmd=&quot;finditem.asp&quot;>Find Item</div>
<div Id=&quot;menuItem4_3&quot; class=&quot;menuItem&quot; title=&quot;View Received Items&quot; cmd=&quot;viewrecd.asp&quot;>View Recd</div>
<div Id=&quot;menuItem4_4&quot; class=&quot;menuItem&quot; title=&quot;Approved, Not Ordered&quot; cmd=&quot;appnotord.asp&quot;>Not Ordered</div>
<div Id=&quot;menuItem4_5&quot; class=&quot;menuItem&quot; title=&quot;Cancelled Items&quot; cmd=&quot;cancelled.asp&quot;>Cancelled</div>
</div>
</div>
</center>

Rick It's a pleasure to know that I've helped you. If I have,
please click the link below to let me know. :)
 
Hello,
Thanks for the reply. I tried this before and it doesn't quite do what I had wanted. This centers each of the topmost menu items on separate lines. I just wanted to align the top row centered on the page. I put that LEFT:125; under .menubar in the .css to &quot;center&quot; the menu on an 800x600 screen, but that is a bad solution.

Is there a way to do this with tables? I'm fine with that if that will work. I tried enclosing all the div's in a table, but then they did not show up. Maybe I have to rtfm a little more... Thanks!
 
What I ended up doing is writing a small javascript to resize the window to a preset width, then using the style sheet to set the left margin of the menu. That seems good for now. Thanks ristmo2001!
 
After some fiddling I have found a better way to center the menu. Calculate the width of the menu (in this case 445) then put this in the <head> tag:

<script language=javascript>
var width=document.documentElement.scrollWidth;
width=Math.abs((width-445)/2);
document.write(&quot;<style type='text/css'>.menuBar{LEFT:&quot;+width+&quot;;POSITION:absolute;BACKGROUND-COLOR:transparent;TEXT-ALIGN:center;COLOR:#FFFFFF;FONT-WEIGHT:bold;}</style>&quot;);
</script>

Take out the .menuBar section from the css file and if you want it to stay centered even if the window is resized put this in the <body> tag:

onResize='location.reload();'

This code works for IE 5.5 SP2. Might have to get the browser window width another way if using a different browser. For examples go here:


and scroll to about 2/3 the way down to see what works in your browser. The page is in japanese, but the table you need is in English. Hope this helps someone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top