【问题标题】:how to change the color of a row of a table on hover如何在悬停时更改表格行的颜色
【发布时间】:2017-12-27 15:08:19
【问题描述】:

我正在尝试在悬停时更改表格行的颜色。 通过使用行:悬停,但通过这种方式,它只是悬停在一个明显的特定单元格上。 但是当我尝试使用row cell: hover 时,它没有任何效果。这是我的html。

JSfiddle:https://jsfiddle.net/f4ojhxco/2/

 <div id="table">
  <div class="header-row row">
<span class="cell">SI. No.</span>
    <span class="cell">Application Date</span>
    <span class="cell">Customer Name</span>
    <span class="cell">Loan Amount</span>
    <span class="cell">Loan Status</span>
    <span class="cell">Action</span>
  </div>
<div class="row" *ngFor="let item of customerList ; let i= index;">
    <input type="radio" name="expand">
<span class="cell" data-label="SI. No.">{{i+1}}</span>
<span class="cell" data-label="Application Date">{{item.date}}</span>
<span class="cell" data-label="Customer Name">
  {{item.name}}</span>
<span class="cell" data-label="Loan Amount">{{item.amount}}</span>
<span class="cell" data-label="Loan Status">{{item.status}}</span>
<span class="cell" data-label="Action">
  <a href="https://www.w3schools.com">More Details</a>
</span>
  </div>
</div>

【问题讨论】:

标签: css


【解决方案1】:

去掉.row:hover之间的空格,给出如下样式。

.row:hover {
    background: green;
}

【讨论】:

    【解决方案2】:

    只需添加 !important 关键字即可。

      .row :hover {
        background: green !important;
      }
    

    JSfiddle:https://jsfiddle.net/f4ojhxco/3/

    【讨论】:

    • !在这种情况下不需要重要
    【解决方案3】:

    试试这个:

    	body {
    	  background: #cacaca;
    	  margin: 0;
    	  padding: 20px;
    	  font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
    	  font-weight: 300;
    	}
    
    	#table {
    	  display: table;
        width: 100%;
    	  background: #fff;
    	  margin: 0;
    	  box-sizing: border-box;
    	}
    
    	.caption {
    	  display: block;
    	  width: 100%;
    	  background: #64e0ef;
    	  height: 55px;
    	  padding-left: 10px;
    	  color: #fff;
    	  font-size: 20px;
    	  line-height: 55px;
    	  text-shadow: 1px 1px 1px rgba(0, 0, 0, .3);
    	  box-sizing: border-box;
    	}
    
    	.header-row {
    	  background: #8b8b8b;
    	  color: #fff;
    	}
    
    	.row {
    	  display: table-row;
      }
    
      .row :hover {
        background: green;
      }
    
    
    
    	.cell {
    	  display: table-cell;
    	  padding: 23px;
    	  border-bottom: 1px solid #e5e5e5;
    	  text-align: center;
    	}
    
    	.primary {
    	  text-align: left;
    	}
    
    	input[type="radio"],
    	input[type="checkbox"] {
    	  display: none;
    	}
    
    	@media only screen and (max-width: 760px) {
    	  body {
    	    padding: 0;
    	  }
    	  #table {
    	    display: block;
    	    margin: 44px 0 0 0;
    	  }
    	  .caption {
    	    position: fixed;
    	    top: 0;
    	    text-align: center;
    	    height: 44px;
    	    line-height: 44px;
    	    z-index: 5;
    	    border-bottom: 2px solid #999;
    	  }
    	  .row {
    	    position: relative;
    	    display: block;
    	    border-bottom: 1px solid #ccc;
    	  }
    	  .header-row {
    	    display: none;
    	  }
    	  .cell {
    	    display: block;
    	    border: none;
    	    position: relative;
    	    height: 45px;
    	    line-height: 45px;
    	    text-align: left;
    	  }
    	  .primary:after {
    	    content: "";
    	    display: block;
    	    position: absolute;
    	    right: 20px;
    	    top: 18px;
    	    z-index: 2;
    	    width: 0;
    	    height: 0;
    	    border-top: 10px solid transparent;
    	    border-bottom: 10px solid transparent;
    	    border-right: 10px solid #ccc;
    	  }
    	  .cell:nth-of-type(n+2) {
    	    display: none;
    	  }
    	  input[type="radio"],
    	  input[type="checkbox"] {
    	    display: block;
    	    position: absolute;
    	    z-index: 1;
    	    width: 99%;
    	    height: 100%;
    	    opacity: 0;
    	  }
    	  input[type="radio"]:checked,
    	  input[type="checkbox"]:checked {
    	    z-index: -1;
    	  }
    	  input[type="radio"]:checked~.cell,
    	  input[type="checkbox"]:checked~.cell {
    	    display: block;
    	    border-bottom: 1px solid #eee;
    	  }
    	  input[type="radio"]:checked~.cell:nth-of-type(n+2),
    	  input[type="checkbox"]:checked~.cell:nth-of-type(n+2) {
    	    background: #e0e0e0;
    	  }
    	  input[type="radio"]:checked~.cell:nth-of-type(n+2):before,
    	  input[type="checkbox"]:checked~.cell:nth-of-type(n+2):before {
    	    content: attr(data-label);
    	    display: inline-block;
    	    width: 60px;
    	    background: #999;
    	    border-radius: 10px;
    	    height: 20px;
    	    margin-right: 10px;
    	    font-size: 12px;
    	    line-height: 20px;
    	    text-align: center;
    	    color: white;
    	  }
    	  input[type="radio"]:checked~.primary,
    	  input[type="checkbox"]:checked~.primary {
    	    border-bottom: 2px solid #999;
    	  }
    	  input[type="radio"]:checked~.primary:after,
    	  input[type="checkbox"]:checked~.primary:after {
    	    position: absolute;
    	    right: 18px;
    	    top: 22px;
    	    border-right: 10px solid transparent;
    	    border-left: 10px solid transparent;
    	    border-top: 10px solid #ccc;
    	    z-index: 2;
    	  }
    	}
      .cell{
        height:10px;
        background:green;
        
      }
      .cell:hover{
        background:red;
      }
     <div id="table">
      <div class="header-row row">
    <span class="cell">SI. No.</span>
        <span class="cell">Application Date</span>
        <span class="cell">Customer Name</span>
        <span class="cell">Loan Amount</span>
        <span class="cell">Loan Status</span>
        <span class="cell">Action</span>
      </div>
    <div class="row" >
        <input type="radio" name="expand">
    <span class="cell" data-label="SI. No.">1</span>
    <span class="cell" data-label="Application Date">5-jan-2017</span>
    <span class="cell" data-label="Customer Name">
      mr xyz</span>
    <span class="cell" data-label="Loan Amount">60k</span>
    <span class="cell" data-label="Loan Status">open</span>
    <span class="cell" data-label="Action">
      <a href="https://www.w3schools.com">More Details</a>
    </span>
      </div>
    </div>

    【讨论】:

      【解决方案4】:

      只需删除.row:hover 之间的空格

      .row:hover{
          background: green;
      }
      

      【讨论】:

        【解决方案5】:

        试试这个

        #table .row:hover {
         /*add your css here*/
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-01-12
          • 1970-01-01
          • 2013-03-16
          • 2014-11-17
          • 1970-01-01
          • 2014-01-30
          • 1970-01-01
          相关资源
          最近更新 更多