简体   繁体   English

CSS3 li菜单包含border-radius和li:hover

[英]CSS3 li Menu with border-radius and li:hover

I'm having a bit of an issue with a CSS3 menu with rounded corners, and I'm not sure if it's the way I'm applying my classes that are causing my issue. 我对带有圆角的CSS3菜单有一点问题,我不确定这是否是我应用导致我的问题的类的方式。 Please note that in my examples below I have omitted the -moz- and -webkit- variants of the code in order to save space, but they are present in my code. 请注意,在下面的示例中,我省略了代码的-moz--webkit-变体以节省空间,但它们存在于我的代码中。

I have a <div> tag containing a customised <ul> menu, to allow drop down functionality and am also utilising li:hover to highlight the menu selected. 我有一个包含自定义<ul>菜单的<div>标签,以允许下拉功能,并且还使用li:hover来突出显示所选的菜单。 And, for the first time ever (in my experience), IE actually worked flawlessly first time whereas Firefox and Chrome required some tweaking. 并且,有史以来第一次(根据我的经验),IE实际上第一次完美无缺,而Firefox和Chrome需要进行一些调整。

I was having an issue with the li:hover state of my far left menu item, as when it was hovered it squared off the corners and then after the 'mouseout' it also affected the border-radius of the containing <ul> tag. 我的左边菜单项的li:hover状态出现了问题,因为当它悬停时它偏离角落然后在'mouseout'之后它也影响了包含<ul>标签的border-radius I was able to fix the issue by applying the border-radius to a div tag first and then hiding the overflow, however as the menu items contained drop-down lists they were not being displayed. 我能够通过首先将border-radius应用于div标签然后隐藏溢出来解决问题,但是当菜单项包含下拉列表时,它们没有被显示。 I therefore set the left menu item with a class of menu_end and set the border-top-left-radius and border-bottom-left-radius of the li and li:hover to match the border-radius of the <ul> tag, which I thought had fixed the problem. 因此,我在左侧菜单项中设置了一个menu_end类,并设置了lili:hoverborder-top-left-radiusborder-bottom-left-radius li:hover以匹配<ul>标签的border-radius ,我认为已经解决了这个问题。

.menu_end {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
}

.menu_end li:hover {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
}

However, I've noticed that the sub-menu that is created in the li:hover state has also inherited these style with rounded left corners, and I can't seem to clear them. 但是,我注意到在li:hover状态下创建的子菜单也继承了左边圆角的这些样式,我似乎无法清除它们。 I've tried adding: 我试过添加:

.menu_end ul li ul li{
border-top-left-radius: 0px;
border-bottom-left-radius: 0px;
}

.menu_end ul li ul li:hover {
border-top-left-radius: 0px;
border-bottom-left-radius: 0px;
}

Which for some reason doesn't work. 这由于某种原因不起作用。 I've also tried assigning the nested <li> elements their own class: 我也尝试将嵌套的<li>元素分配给自己的类:

.menu_end_drop li {
border-top-left-radius: 0px;
border-bottom-left-radius: 0px;
}
.menu_end_drop li:hover {
border-top-left-radius: 0px;
border-bottom-left-radius: 0px;
}

Neither method seems to work, and strangely if I apply a style to just border-radius without specifying a location, it effects the two previously unspecified corners but not the top left and bottom left corners. 这两种方法似乎都不起作用,奇怪的是如果我在没有指定位置的情况下将样式应用于border-radius ,它会影响两个先前未指定的角,但不影响左上角和左下角。

I thought this might be to do with the order of my styles, but my understanding is that and styles applied later in a css file supersedes any previous styles, so I'm at a bit of a loss! 我认为这可能与我的样式顺序有关,但我的理解是,后来在css文件中应用的样式取代以前的任何样式,所以我有点亏本!

I've included everything in this fiddle -> http://jsfiddle.net/Z5qWe/2/ 我把这一切都包括在内 - > http://jsfiddle.net/Z5qWe/2/

Any help is greatly appreciated, 任何帮助是极大的赞赏,

Best Wishes, 最好的祝愿,

Joe

The issue is because of this: 问题是因为:

.menu_end li:hover {
    border-top-left-radius: 5px;
    border-bottom-left-radius: 5px;
}

This should only apply to direct descendants (so we use > ). 这应该只适用于直接后代 (所以我们使用> )。

Replace it with this: 替换为:

.menu_end > li:hover {
    border-top-left-radius: 5px;
    border-bottom-left-radius: 5px;
}

Fiddle: http://jsfiddle.net/Z5qWe/5/ 小提琴: http//jsfiddle.net/Z5qWe/5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM