我认为Matthew James Taylor是这段精美代码的英雄:

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

但是,无论我如何砍掉它-而且我已经有多年了-我无法使菜单具有弯曲的角。

所以我将其发布在这里,因为我看不到SO的解决方案,并且我认为社区可以从中获得繁荣。

细节:

悬停时,最左边和最右边的菜单项都还原为方形角。我曾尝试添加一个css类来更正此问题,但是当用户滚动到子菜单区域时,它们仍恢复为正方形。

这是我的原位菜单:http://hrmpowerwash.pro

这是我的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 */
}


如您所见,请参阅“我已经对主菜单的最左和最右的选项进行了四舍五入(通过将类应用于第一个和最后一个#div ul li),但是它们在悬停时消失了。

在另一个版本中,我添加了以下代码来修复顶部菜单项的悬停,但是当用户下降到子菜单项时,它仍恢复为方形角:

/* 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;
}


如果可能的话,请告知。以及如何实现我在俯视什么?

预先感谢大家。

最佳答案

您非常接近,您只需要在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;
}


请注意添加的.rightmost:hover > a.leftmost:hover > a

现在,即使将鼠标悬停在下拉列表上,这也将使a保持四舍五入。

我还建议为每个浏览器添加-webkit-border-radius CSS,以提高浏览器的兼容性。

关于css - 水平居中下拉菜单上的 flex Angular ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39298900/

10-12 00:29
查看更多