CSS悬停效果不适用于我的代码

时间:2013-06-29 04:59:52

标签: html css css3

下面的代码在ie 9中工作正常,并且在任何其他浏览器中都不起作用。当我将鼠标悬停在列表背景上时应该改变颜色,但事实并非如此。

.menunews ul {
  margin: 0px;
  padding: 0px;
  list-style-type: none;
}

.menunews a {
  display: block;
  color: #266CAE;
  height: 30px;
  background: #ffffff;
  border-bottom: 1px solid #ccc;
  overflow: hidden;
  width: 100%;
  height: 2.72em;
  line-height: 2.75em;
  text-indent: 2.02em;
  text-decoration: none;
}

.menunews li a:hover {
  background: #ffffff;
  background: -moz-linear-gradient(top, #ffffff 0%, #f6f6f6 47%, #ededed 100%);
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #ffffff), color- stop(47%, #f6f6f6), color-stop(100%, #ededed));
  background: -webkit-linear-gradient(top, #ffffff 0%, #f6f6f6 47%, #ededed 100%);
  background: -o-linear-gradient(top, #ffffff 0%, #f6f6f6 47%, #ededed 100%);
  background: -ms-linear-gradient(top, #ffffff 0%, #f6f6f6 47%, #ededed 100%);
  background: linear-gradient(to bottom, #ffffff 0%, #f6f6f6 47%, #ededed 100%);
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#ededed', GradientType=0);
  color: #266CAE
}
<ul style="font-size:12px;">
  <li class="menunews">
    <a href=""><span style="margin-left:2px;">Hello test</span></a>
  </li>
</ul>

3 个答案:

答案 0 :(得分:6)

实际上你用其他方式制作了 CSS 这就是为什么浏览器不理解你的 css代码所以我在你的 css中做了一些改变并且根据您的要求在所有浏览器上工作正常,所以我希望这对您有帮助.....

ul li.menunews {
    border-bottom: 1px solid #ccc;  
    list-style:none; 
    height:30px;
}
ul li.menunews a {
    display:block;
    color:#266CAE;
    text-decoration:none;
}
    
ul li.menunews:hover {
    background:#ffffff;
    background:-moz-linear-gradient(top,  #ffffff 0%, #f6f6f6 47%, #ededed 100%); 
    background:-webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff), color- stop(47%,#f6f6f6), color-stop(100%,#ededed)); 
    background:-webkit-linear-gradient(top,  #ffffff 0%,#f6f6f6 47%,#ededed 100%); 
    background:-o-linear-gradient(top,  #ffffff 0%,#f6f6f6 47%,#ededed 100%); 
    background:-ms-linear-gradient(top,  #ffffff 0%,#f6f6f6 47%,#ededed 100%);
    background:linear-gradient(to bottom,  #ffffff 0%,#f6f6f6 47%,#ededed 100%); 
    filter:progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#ededed',GradientType=0 );color:#266CAE} 
}
<ul style="font-size:12px;">
    <li class="menunews"><a href="#"><span style="margin-left:2px;">Hello test</span></a></li>
</ul>

答案 1 :(得分:3)

用ul而不是li定义你的类以便生效:

<ul class="menunews" style="font-size:12px;"><li ><a href="#" >

答案 2 :(得分:2)

您已提及menunews课程为li,css应为li.menunews,请使用以下css代码

 ul{
   margin:0px;
   padding:0px;
   list-style-type:none;
 }
 .menunews a{
        display:block;
        color:#266CAE;
        height:30px;
        background:#ffffff;
        border-bottom: 1px solid  #ccc;
        overflow:hidden;
        width:100%;
        height:2.72em;
        line-height:2.75em;
        text-indent:2.02em;
        text- decoration:none;
        }

 li.menunews a:hover{
      background:#ffffff;
      background:-moz-linear-gradient(top,#ffffff 0%,#f6f6f6 47%, #ededed 100%); 
      background:-webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff), color- stop(47%,#f6f6f6), color-stop(100%,#ededed)); 
      background:-webkit-linear-gradient(top,  #ffffff 0%,#f6f6f6 47%,#ededed 100%); 
      background:-o-linear-gradient(top,  #ffffff 0%,#f6f6f6 47%,#ededed 100%); 
      background:-ms-linear-gradient(top,  #ffffff 0%,#f6f6f6 47%,#ededed 100%);
      background:linear-gradient(to bottom,  #ffffff 0%,#f6f6f6 47%,#ededed 100%); 
      filter:progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#ededed',GradientType=0 );
      color:#266CAE;
      }

请参阅 DEMO