Changing background colour of tr element on mouseover

前端 未结 9 888
一向
一向 2021-02-01 13:00

I want to have my table rows highlighted on mouse over, but I have yet to find a way to do it that isn\'t using Javascript. Is this not possible in CSS?

I\'ve tried this

相关标签:
9条回答
  • 2021-02-01 13:28

    You can give the tr an id and do it.

    tr#element{
        background-color: green;
        cursor: pointer;
        height: 30px;
    
    }
    
    tr#element:hover{
        background-color: blue;
        cursor: pointer;
    
    }
    
    <table width="400px">
    <tr id="element">
    <td></td>
    </tr>
    </table>
    
    0 讨论(0)
  • 2021-02-01 13:31

    You could try:

    tr:hover {
        background-color: #000;
    }
    
    tr:hover td {
        background-color: transparent; /* or #000 */
    }
    

    JS Fiddle demo.

    0 讨论(0)
  • 2021-02-01 13:42

    This will work:

    tr:hover {
        background: #000 !important;
    }
    

    If you want to only apply bg-color on TD then:

    tr:hover td {
        background: #c7d4dd !important;
    }
    

    It will even overwrite your given color and apply this forcefully.

    0 讨论(0)
  • 2021-02-01 13:42

    its easy . Just add !important at the end of your css line :

    tr:hover {    background: #000 !important; }
    
    0 讨论(0)
  • 2021-02-01 13:43

    Try it

    <!- HTML -->
    <tr onmouseover="mOvr(this,'#ffa');" onmouseout="mOut(this,'#FFF');">
    
    <script>
    function mOvr(src,clrOver) { 
      if (!src.contains(event.fromElement)) { 
      src.bgColor = clrOver; 
      } 
    } 
    function mOut(src,clrIn) { 
      if (!src.contains(event.toElement)) { 
      src.bgColor = clrIn; 
      } 
    } 
    </script>
    
    0 讨论(0)
  • 2021-02-01 13:47
    tr:hover td {background-color:#000;}
    
    0 讨论(0)
提交回复
热议问题