Parapluie Parapluie - 3 months ago 11
CSS Question

curved corners on Horizontally Centered Drop-Down Menus

I think Matthew James Taylor is a hero for this beautiful bit of code:

http://matthewjamestaylor.com/blog/centered-dropdown-menus

However, no matter how I chop away at it – and I have, FOR YEARS – I can not make the menu work with curved corners.

So I am posting this here, because I don't see a solution on SO, and I think the community could prosper from this.

Details:

When hovered, the left-most and right-most menu items both revert to squared corners. I have tried adding a css class to correct this, but they still revert to squares when the user scrolls into the sub-menu area.

Here is my menu, in situ: http://hrmpowerwash.pro

Here is my css:

/* horizontal navigation bar */

/* Main menu settings */
#centeredmenu {
position:relative;
clear:both;
float:left;
z-index:1000; /* This makes the dropdown menus appear above the page content below — superceded only by alerts (z=99999) */
margin:1em 0 0 0;
padding:0;
width:100%;
font-size:90%; /* Menu text size */
text-transform:uppercase;
}

/* Top menu items */
#centeredmenu ul {
margin:0;
padding:0;
list-style:none;
float:right;
position:relative;
right:50%;
}
#centeredmenu ul li {
padding:0;
float:left;
position:relative;
left:50%;
top:1px;
}
#centeredmenu ul li a {
display:block;
margin:0;
padding:.6em .5em .4em;
font-size:1em;
line-height:1em;
text-decoration:none;
color:#FFF;
font-weight:bold;
}

/* These three classes add the white border to the top menu items. */
.leftmost {
border:#FFF 2px solid;
border-radius: 8px 0 0 8px;
background:#e68f1a;
}
.middle {
border:#FFF solid;
border-width:2px 2px 2px 0;
background:#e68f1a;
}
.rightmost {
border:#FFF solid;
border-width:2px 2px 2px 0;
border-radius:0 8px 8px 0;
background:#e68f1a;
}

#centeredmenu ul li.active a {
color:#fff;
background:#000;
}
#centeredmenu ul li a:hover { /* This is to change if we want a brand colour for menu hover instead of blue */
background:#36f; /* Top menu items background colour */
color:#fff;
border-bottom:1px solid #03f;
}
#centeredmenu ul li:hover a,
#centeredmenu ul li.hover a { /* This line is required for IE 6 and below */
background:#36f; /* Top menu items background colour */
color:#fff;
border-bottom:1px solid #03f;
}

/* Submenu items */
#centeredmenu ul ul {
display:none; /* Sub menus are hidden by default */
position:absolute;
top:2em;
left:0;
float:left;
right:auto; /*resets the right:50% on the parent ul */
width:10em; /* width of the drop-down menus */
}
#centeredmenu ul ul li {
left:auto; /*resets the left:50% on the parent li */
margin:0; /* Reset the 1px margin from the top menu */
clear:left;
float:left;
width:100%;
}
#centeredmenu ul ul li a,
#centeredmenu ul li.active li a,
#centeredmenu ul li:hover ul li a,
#centeredmenu ul li.hover ul li a { /* This line is required for IE 6 and below */
font-size:.8em;
font-weight:normal; /* resets the bold set for the top level menu items */
background:#eee;
color:#444;
line-height:1.4em; /* overwrite line-height value from top menu */
border-bottom:1px solid #ddd; /* sub menu item horizontal lines */
float:left;
width:100%;
}
#centeredmenu ul ul li a:hover,
#centeredmenu ul li.active ul li a:hover,
#centeredmenu ul li:hover ul li a:hover,
#centeredmenu ul li.hover ul li a:hover { /* This line is required for IE 6 and below */
background:#36f; /* Sub menu items background colour */
color:#fff;
float:left;
}

/* Flip the last submenu so it stays within the page */
#centeredmenu ul ul.last {
left:auto; /* reset left:0; value */
right:0; /* Set right value instead */
}
#centeredmenu ul ul.last li {
float:right;
position:relative;
right:.8em;
}

/* Make the sub menus appear on hover */
#centeredmenu ul li:hover ul,
#centeredmenu ul li.hover ul { /* This line is required for IE 6 and below */
display:block; /* Show the sub menus */
}


As you can, see I have rounded the left- and right-most options of the main menu (by applying a class to the first and last #div ul li) but they disappear upon hovering.

In another version, I have added the following code to fix the hover of the top menu items, but it still reverts to square corners when the user descends into the sub-menu items:

/* These classes add rounded corners to the menu headers when they are hovered over */
.rightmost > a:hover {
border-radius:0 8px 8px 0;
}
.leftmost > a:hover {
border-radius:8px 0 0 8px;
}


Please advise, if this is even possible; and how it may be achieved. What am I overlooking?

Thanking all of you in advance.

Answer

You were very close, you just needed these 2 additional calls on your CSS:

.rightmost > a:hover, .rightmost:hover > a {
    border-radius:0 8px 8px 0;
}
.leftmost > a:hover, .leftmost:hover > a {
    border-radius:8px 0 0 8px;
}

Notice the .rightmost:hover > a and the .leftmost:hover > a that were added.

This now will keep the a rounded, even when the dropdown is hovered.

I also suggest adding -webkit-border-radius CSS to each for more browser compatibility.