0

I am trying to design menu like the one show in figure below enter image description here

This is a responsive menu and should look like the one when on smaller screens.

I am facing few issue right now. - I have to add a border between two menus as show in figure below and keep the submenu about 40px down from the base line of main menu. When I add bottom margin to ul li the sub menu show but disappears when I try to take pointer over it.

  • I want sub menu to show in center of parent menu even if wifth of sub menu is more the width of parent menu, I tried this but failed to make it perfect.

    Responsive version

enter image description here

  /*Menu*/
    .nav-center
    {
        text-align:center;
    }
    .menu-link {
        display: none;
        float: right;
        text-decoration: none;
        padding: 19px 10px;
    }
    .menu { 
        max-width: 60em;
        -webkit-transition: all 0.3s ease-out;  
        -moz-transition: all 0.3s ease-out;
        -ms-transition: all 0.3s ease-out;
        -o-transition: all 0.3s ease-out;
        transition: all 0.3s ease-out; 
    }
    .menu ul { 
        padding: 0px;
        margin: 0px;
        list-style: none;
        position: relative;
        display: inline-table;
    }
    .menu > li > ul.sub-menu {
        min-width: 10em;
        padding: 4px 0;
        background-color: #f4f4f4;
        border: 1px solid #CCC;
        border: 1px solid rgba(0, 0, 0, 0.2);
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        border-radius: 5px;
        -webkit-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
        -moz-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
        box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
        -webkit-background-clip: padding-box;
        -moz-background-clip: padding;
        background-clip: padding-box;
    }
    .menu ul:after {
        content: ""; clear: both; display: block;
    }
    .menu ul li {
        padding: 0px;
        border-right:1px solid #053A7D;
    }
    .menu > ul > li { 
        display: inline-block;

    }
    .menu ul li a { 
        display: block;
        text-decoration: none;
        color: #053A7D;
        font-size: .9em;
    }
    .menu ul li > a { 
        padding: 5px 20px;  
    }
    .menu ul ul { 
        display: none; 
        position: absolute; 
        top:100%;
        min-width: 160px;
        border-top: 2px solid #053A7D;
        /*background-color: #f4f4f4;
    border: 1px solid rgba(0, 0, 0, 0.2);
        -webkit-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
        -moz-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
        box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
    */
    }
    .menu ul li:hover > ul {
        display: block;
    }
    .menu ul ul > li {
        position: relative;
    }
    .menu ul ul > li a {
        padding: 5px 2px 5px 2px;
        height: auto;
        background-color: #fff;
        -webkit-transition: background-color 0.2s ease-out;  
        -moz-transition: background-color 0.2s ease-out;
        -ms-transition: background-color 0.2s ease-out;
        -o-transition: background-color 0.2s ease-out;
        transition: background-color 0.2s ease-out; 
    }
    .menu ul ul > li a:hover {
        /*background-color: #AE5C10;
        background-color: rgba(174, 92,16, 0.9);
        color: #fff;
    */
    }
    .menu ul ul ul {
        position: absolute; 
        left: 100%; 
        top:0;
    }


    @media screen and (max-width: 768px) {

        a.menu-link {
            display: block;
            color: #fff;
            background-color: #AE5C10;
            background-color: rgba(174, 92,16, 0.9);
        }
        .menu {
            clear: both;
            min-width: inherit;
            float: none;
        }
        .menu, .menu > ul ul {
            overflow: hidden;
            max-height: 0;
            background-color: #f1f1f1;
        }
        .menu > li > ul.sub-menu {
        padding: 0px;
        border: none;
        -webkit-border-radius: 0px;
        -moz-border-radius: 0px;
        border-radius: 0px;
        -webkit-box-shadow: none;
        -moz-box-shadow: none;
        box-shadow: none;
        -webkit-background-clip: padding-box;
        -moz-background-clip: padding;
        background-clip: padding-box;
        }
        .menu.active, .menu > ul ul.active {
            max-height: 55em;
        }
        .menu ul {
            display: inline;
        }
        .menu > ul {
            border-top: 1px solid #808080;
        }
        .menu li, .menu > ul > li {
            display: block;
        }
        .menu li a {
            color: #000;
            display: block;
            padding: 0.8em;
            border-bottom: 1px solid #808080;
            position: relative;
        }
        .menu li.has-submenu > a:after {
            content: '+';
            position: absolute;
            top: 0;
            right: 0;
            display: block;
            font-size: 1.5em;
            padding: 0.55em 0.5em;
        }
        .menu li.has-submenu > a.active:after {
            content: "-";
        }
        .menu ul ul > li a {
            background-color: #e4e4e4;
            height:58px;
            padding: 19px 18px 19px 30px;
        }
        .menu ul ul, .menu ul ul ul {
            display: inherit;
            position: relative;
            left: auto; top:auto;
            box-shadow: none; 
            border:none;
        }
    }
    /*Menu End*/

I have set up fiddle example, I would appreciate help in this regard.

http://jsfiddle.net/9BcPd/

UPDATE: To avoid confusion I am uploading another image which show guideline in red for the sub menu position

enter image description here

3
  • The fiddle looks very different Commented Jul 17, 2014 at 19:56
  • @Marcel, one show in image is done in graphics programs and the last image which has red lines is just for guideline as some user got confused about the center alignment of sub menu. Commented Jul 17, 2014 at 19:59
  • Why does your HTML have all the classes if you don't bother using them in the CSS? Commented Jul 17, 2014 at 20:00

2 Answers 2

1

Here's my take on this navigation menu: http://jsfiddle.net/bG46Z/. I've sprinkled some comments in the CSS code that address the vertical line in between the links and the horizontal centering of the submenu.

For this preview, use Firefox. WebKit browsers will also work, but I've experienced an anomaly (perhaps a bug) when changing the viewport size. That is, when screen width is small (below 600px) and you resize it so that the full menu is shown and then you resize it back to smaller width, then hovering of the menu "button" will not display the menu. It works swell in Firefox, so I am thinking that it is just a bug in WebKit browsers. To overcome the latter anomaly, you can just sprinkle some jQuery code.

HTML:

<div id = "menuContainer">
    <p>Menu</p>
    <ul>
        <li><a href = "#">Home</a></li>
        <li><a href = "#">Menu Two</a>
            <ul>
                <li><a href = "#">Sub Menu One</a></li>
                <li><a href = "#">Sub Menu Two</a></li>
                <li><a href = "#">Sub Menu Three</a></li>
                <li><a href = "#">Sub Menu Four</a></li>
            </ul>
        </li>
        <li><a href = "#">Menu Three</a></li>
        <li><a href = "#">Menu Four</a></li>
    </ul>
</div>
<p>Sample paragraph</p>

CSS:

/* 
    a mini-reset to eliminate default formatting 
    applied by the browser
*/

* {
    margin: 0;
    padding: 0;
}

ul {
    list-style-type: none;
}

body {
    padding: 10px;
}

#menuContainer > p {
    display: none;
}

/*
    the main ul is displayed as a table.  This makes
    it shrinkwrap around its children while remaining 
    a block element.  This allows to apply margin: 0 auto 
    to center it.  Additionally, no clearfix is required 
    if elements are floated in the table element.
*/

#menuContainer > ul {
    display: table;
    margin: 0 auto;
}

#menuContainer > ul > li {
    float: left;
    padding: 5px 0;
    position: relative;
}

#menuContainer > ul > li:hover > a {
    color: #aaa;
}

/*
    The links are displayed as blocks, which allows 
    changing their width and height indirectly through 
    paddings, line-heights, etc.
*/

#menuContainer li a {
    display: block;
    font: bold 15px/1 Sans-Serif;
    text-transform: uppercase;
    text-decoration: none;
    color: #002B81;
    text-align: center;
    padding: 0 20px;
    white-space: nowrap;
}

/*
    The border is added on the left for the links that are 
    children of the list item that follows another list 
    item.  In other words, the border is set for every 
    link that is a child of every list item, except the 
    first list item.
*/

#menuContainer > ul > li + li > a {
    border-left: 2px solid #002B81;
}

/*
    This chunk of code does the horizontal centering of the 
    nested list under its list item parent.  The left of 50% 
    moves the left edge of the list to the center of its li 
    parent; and, translateX() of -50% moves the nested list 
    to the left by 50% of its width.
*/

#menuContainer ul ul {
    position: absolute;
    left: 50%;
    top: 100%;
    -webkit-transform: translateX(-50%);
    transform: translateX(-50%);
    padding: 20px 0 30px 0;
    display: none;
}

/*
    This is a "trick" to apply the background to 
    only a part of the nested ul.  In reality the 
    ul is immediately below its li parent, but this 
    makes it look as if it is farther down.
*/

#menuContainer ul ul:before {
    content: "";
    position: absolute;
    top: 20px;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    z-index: -1;
}

#menuContainer ul ul > li:first-child {
    border-top: 2px solid #002B81;
    padding-top: 10px;
}

#menuContainer ul ul a {
    padding: 0 10px;
    color: #fafafa;
    line-height: 2;
}

#menuContainer ul ul a:hover {
    color: #ccc;
    background-color: #fafafa
}

#menuContainer > ul > li:hover > ul {
    display: block;
}

/*
    media queries code that makes the menu "button" 
    visible, reformats the menus for more narrow display,
    and creates the appropriate dropdown effect.
*/

@media screen and (max-width: 600px) {
    #menuContainer > p {
        cursor: pointer;
        display: table;
        margin: 0 auto;
        padding: 5px 10px;
        font: bold 15px/1 Sans-Serif;
        text-transform: uppercase;
        color: #fafafa;
        background-color: #002B81;
    }

    #menuContainer > ul {
        padding: 10px 0 30px 0;
        position: absolute;
        display: none;
        left: 50%;
        -webkit-transform: translateX(-50%);
        transform: translateX(-50%);
    }

    #menuContainer > ul:before {
        content: "";
        position: absolute;
        top: 10px;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: #ccc;
        z-index: -1;
    }

    #menuContainer > ul > li {
        float: none;
    }

    #menuContainer > ul > li:first-child {
        border-top: 2px solid #002b81;
    }

    #menuContainer > ul > li {
        padding: 5px 0;
    }

    #menuContainer > ul > li > a {
        font-size: 14px;
    }

    #menuContainer > ul > li:hover > a {
        color: #585858;
    }

    #menuContainer > ul > li + li > a {
        border: 0;
    }

    #menuContainer ul ul {
        position: static;
        display: block;
        border-top: 0;
        padding: 0;
        -webkit-transform: none;
        transform: none;
        margin-top: 5px;
    }

    #menuContainer ul ul > li:first-child {
        border: 0;
        padding-top: 0;
    }

    #menuContainer ul ul a {
        font-size: 12px;
    }

    #menuContainer:hover > p:hover + ul,
    #menuContainer:hover ul:hover {
        display: block;
    }
}

Cheers.

Sign up to request clarification or add additional context in comments.

3 Comments

I am not sure which version your solution will support for IE, I will try it with IE 8, I am sure it will work for IE9+. Any way s it is good & clean solution. +1
That's a good point. The pseudo-elements :before and :after will not work in older browsers (e.g., IE6, IE7). To use the same technique, you'll need to add markup. Transforms, and translateX(), in particular, will work only in modern browsers. However, the same technique will work with negative margins. For the latter, the width of the submenu will need to be explicitly set. That is, if the menu width is 300px, then margin-left of -150px will do the centering.
Or, you may also consider using polyfills: html5polyfill.com. For my site I wanted to use viewport units and to make it work in older browsers I just used a shim and it worked out pretty well.
1
  1. I want sub menu to show in center of parent menu even if width of sub menu is more the width of parent menu, i tried this but failed to make it perfect.

add

.menu ul ul {

 ---
 --- 
 ---
   margin-left:-20px;

}

.menu ul ul li {
   padding: 0px;
   border-right:none;
}
.menu ul li > a {
    padding: 5px 20px;
    text-align:center;
}

DEMO

6 Comments

It doent make any difference, Even the your demo is same as mine.
@KnowledgeSeeker: is it not centered ?
i think you got it wrong, I mean the all the submeu ul should be in center of the Parent menu, Plz look at the image one i want menu to be exactly the way it. as far as sub menu text alignment i already have that center in my actual code.
@KnowledgeSeeker:check my demo now
What will happen if other submenu are more in width than this menu in this case margin-left:-20px; will work but is their way it can do auto calculation and center it equally.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.