2016-08-16 77 views
3

我已经定义了这个悬停的div元素如何删除悬停风格CSS

div.MyCSSClass:hover 
{ 
    background-color: purple; 
} 

这是我的HTML源代码:

<div class=" 
    <ul class="MyParentCSSClass"> 
    <li> 
     <div> 
      <div> 
      <div class="MyCSSClass"> 
       <!-- I want to remove CSS hover for this div element --> 

我希望在div.MyCSSClass删除悬停是MyParentCSSClass的一个孩子,所以我添加这个来删除CSS中的悬停样式:

.MyParentCSSClass div.MyCSSClass:hover 
{ 
} 

但它没有奏效。我仍然看到相同的悬停风格。 有没有办法在CSS中删除悬停而无需为我的div标签创建新的CSS类?我想保留与其他CSS属性使用'MyCSSClass'相同的名称。

感谢您的建议。我试图

background-color: none !important; 

但是,当我看着Chrome中,CSS是被过度写

.MyGrandParentClass div.MyCSSClass:hover 
{ 
    background-color: purple; 
} 

和html源代码是

<div class="MyGrandParent"> 
    <ul class="MyParentCSSClass"> 
    <li> 
     <div> 
      <div> 
      <div class="MyCSSClass"> 
       <!-- I want to remove CSS hover for this div element --> 

我的问题是怎么样我的“删除悬停'css规则正在被覆盖?我已经把“重要”放在了我的统治之下。

+2

代替使用空括号,将'background-color'设置为'none'或'inherit'。 – Polyov

回答

2
.MyParentCSSClass div.MyCSSClass:hover { 
    background-color: none; 
} 

这将覆盖由div.MyCSSClass:hover给出的背景颜色。如果您将MyParentCSSClass div.MyCSSClass:hover保留为空作为MyParentCSSClass div.MyCSSClass:hover {},它不会覆盖任何内容或实际上什么都不做。

+0

你可能会认为'background-color:initial'实际上是一样的,从语义上讲,你正在清理这个领域。 – Don

+0

是的,这将工作 –

0

您需要将所有以前添加的样式重新写入悬停事件。在您指定的情况下,请执行以下操作:

.MyParentCSSClass div.MyCSSClass:hover 
{ 
    background-color: none; 
} 
0

Background-color:none;不是W3C标准。它将在一些浏览器上工作,但根据W3C标准,这是不正确的方式。

因此,请尝试使用背景颜色:透明,这将在所有浏览器上运行良好,w3c可以验证您的代码。 enter image description here

玩得开心。

+0

初始可能是更好的选择... – Don