Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

CSS/Java based drop down menu.

Options
  • 26-11-2009 2:15pm
    #1
    Closed Accounts Posts: 4


    I'm trying to make a drop down menu for my site, and I can get the styling just how I want it. But Im running into major problems trying to get it to center to the screen. Using tips from other guides around the web I can get it to align roughly to center, but its still off by about 100px.

    I'm not really experienced with these menus and I'm wondering if anyone has run into this issue before, would appreciate any help! :)
    <html>
    <head>
    <style type="text/css">
    #sddm
    {    margin: 0;
        padding: 0;
        z-index: 30}
    
    #sddm li
    {    margin: 0;
        padding: 0;
        list-style: none;
        float: left;
        font: bold 11px arial}
    
    #sddm li a
    {    display: block;
        margin: 0 1px 0 0;
        padding: 4px 10px;
        width: 60px;
        background: #5970B2;
        color: #FFF;
        text-align: center;
        text-decoration: none}
    
    #sddm li a:hover
    {    background: #49A3FF}
    
    #sddm div
    {    position: absolute;
        visibility: hidden;
        margin: 0;
        padding: 0;
        background: #EAEBD8;
        border: 1px solid #5970B2}
    
        #sddm div a
        {    position: relative;
            display: block;
            margin: 0;
            padding: 5px 10px;
            width: auto;
            white-space: nowrap;
            text-align: left;
            text-decoration: none;
            background: #EAEBD8;
            color: #2875DE;
            font: 11px arial}
    
        #sddm div a:hover
        {    background: #49A3FF;
            color: #FFF}
    
            </style>
    
    
            <script type="text/javascript">        
    // Copyright 2006-2007 javascript-array.com
    
    var timeout    = 500;
    var closetimer    = 0;
    var ddmenuitem    = 0;
    
    // open hidden layer
    function mopen(id)
    {    
        // cancel close timer
        mcancelclosetime();
    
        // close old layer
        if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
    
        // get new layer and show it
        ddmenuitem = document.getElementById(id);
        ddmenuitem.style.visibility = 'visible';
    
    }
    // close showed layer
    function mclose()
    {
        if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
    }
    
    // go close timer
    function mclosetime()
    {
        closetimer = window.setTimeout(mclose, timeout);
    }
    
    // cancel close timer
    function mcancelclosetime()
    {
        if(closetimer)
        {
            window.clearTimeout(closetimer);
            closetimer = null;
        }
    }
    
    // close layer when click-out
    document.onclick = mclose; 
    
    </script>
    
    </head>
    <body>
    
    
    <ul id="sddm">
        <li><a href="#" 
            onmouseover="mopen('m1')" 
            onmouseout="mclosetime()">Home</a>
            <div id="m1" 
                onmouseover="mcancelclosetime()" 
                onmouseout="mclosetime()">
            <a href="#">HTML Drop Down</a>
            <a href="#">DHTML Menu</a>
            <a href="#">JavaScript DropDown</a>
            <a href="#">Cascading Menu</a>
            <a href="#">CSS Horizontal Menu</a>
            </div>
        </li>
        <li><a href="#" 
            onmouseover="mopen('m2')" 
            onmouseout="mclosetime()">Download</a>
            <div id="m2" 
                onmouseover="mcancelclosetime()" 
                onmouseout="mclosetime()">
            <a href="#">ASP Dropdown</a>
            <a href="#">Pulldown menu</a>
            <a href="#">AJAX Drop Submenu</a>
            <a href="#">DIV Cascading Menu</a>
            </div>
        </li>
        <li><a href="#">Order</a></li>
        <li><a href="#">Help</a></li>
        <li><a href="#">Contact</a></li>
    </ul>
    <div style="clear:both"></div>
    
    
    
    
    
    
    </body>
    </html>
    


Comments

  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    1) Put a div around it that has "text-align:center"
    2) Add "margin-left:auto;margin-right:auto;width:auto" to the #sddm style


  • Closed Accounts Posts: 4 Red IX


    Thanks for the reply.

    I've done as you've suggested but I think I might have gotten it wrong because the result is the same.

    Here is the code I am using with your modifications:
    <html>
    <head>
    <style type="text/css">
    #sddm
    {margin:0;padding:0;z-index: 30;margin-left:auto;margin-right:auto;width:auto}
    
    #sddm li
    {    margin: 0;
        padding: 0;
        list-style: none;
        float: left;
        font: bold 11px arial}
    
    #sddm li a
    {    display: block;
        margin: 0 1px 0 0;
        padding: 4px 10px;
        width: 60px;
        background: #5970B2;
        color: #FFF;
        text-align: center;
        text-decoration: none}
    
    #sddm li a:hover
    {    background: #49A3FF}
    
    #sddm div
    {    position: absolute;
        visibility: hidden;
        margin: 0;
        padding: 0;
        background: #EAEBD8;
        border: 1px solid #5970B2}
    
        #sddm div a
        {    position: relative;
            display: block;
            margin: 0;
            padding: 5px 10px;
            width: auto;
            white-space: nowrap;
            text-align: left;
            text-decoration: none;
            background: #EAEBD8;
            color: #2875DE;
            font: 11px arial}
    
        #sddm div a:hover
        {    background: #49A3FF;
            color: #FFF}
    
            </style>
    
    
            <script type="text/javascript">        
    // Copyright 2006-2007 javascript-array.com
    
    var timeout    = 500;
    var closetimer    = 0;
    var ddmenuitem    = 0;
    
    // open hidden layer
    function mopen(id)
    {    
        // cancel close timer
        mcancelclosetime();
    
        // close old layer
        if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
    
        // get new layer and show it
        ddmenuitem = document.getElementById(id);
        ddmenuitem.style.visibility = 'visible';
    
    }
    // close showed layer
    function mclose()
    {
        if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
    }
    
    // go close timer
    function mclosetime()
    {
        closetimer = window.setTimeout(mclose, timeout);
    }
    
    // cancel close timer
    function mcancelclosetime()
    {
        if(closetimer)
        {
            window.clearTimeout(closetimer);
            closetimer = null;
        }
    }
    
    // close layer when click-out
    document.onclick = mclose; 
    
    </script>
    
    </head>
    </body>
    
    <div style="text-align:center">
    <ul id="sddm">
        <li><a href="#" 
            onmouseover="mopen('m1')" 
            onmouseout="mclosetime()">Home</a>
            <div id="m1" 
                onmouseover="mcancelclosetime()" 
                onmouseout="mclosetime()">
            <a href="#">HTML Drop Down</a>
            <a href="#">DHTML Menu</a>
            <a href="#">JavaScript DropDown</a>
            <a href="#">Cascading Menu</a>
            <a href="#">CSS Horizontal Menu</a>
            </div>
        </li>
        <li><a href="#" 
            onmouseover="mopen('m2')" 
            onmouseout="mclosetime()">Download</a>
            <div id="m2" 
                onmouseover="mcancelclosetime()" 
                onmouseout="mclosetime()">
            <a href="#">ASP Dropdown</a>
            <a href="#">Pulldown menu</a>
            <a href="#">AJAX Drop Submenu</a>
            <a href="#">DIV Cascading Menu</a>
            </div>
        </li>
        <li><a href="#">Order</a></li>
        <li><a href="#">Help</a></li>
        <li><a href="#">Contact</a></li>
    </ul>
    <div style="clear:both"></div>
    </div>
    
    
    
    
    
    </body>
    </html>
    


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    Red IX wrote: »
    Thanks for the reply.

    I've done as you've suggested but I think I might have gotten it wrong because the result is the same.
    </html>

    Try setting the width of #sddm explicitly to 322px (but remember that if you add another option you'll need to change this).

    Also, change your first body tag to an opening tag:
    </script>
    
    </head>
    [s]</body>[/s][b]<body>[/b] <=======
    


  • Registered Users Posts: 7,838 ✭✭✭Nulty


    I'm a total novice at jsp but from a usability point of view I think the mouseout time should be about 100ms (var timeout = 100;).

    It takes too long for the menu to return.

    sorry cant help with your actual problem


  • Moderators, Science, Health & Environment Moderators Posts: 8,956 Mod ✭✭✭✭mewso


    Lets get our terminology right here. It's javascript which has nothing to do with java. This looks like a very old drop down menu considering it has so much horrible inline script. You should look at something like "Son of Suckerfish" for a more up to date implementation.

    In general centering an element is a case of giving the parent element a preferred width and "margin: 0 auto;" in it's style settings. Not setting the width means it will be 100% and will have no appearance of alignment. I don't think the wrapping div is necessary here (is it for earlier versions of IE?). I am setting the top/bottom margin to 0 but that can be whatever value you like.


  • Advertisement
Advertisement