代码之家  ›  专栏  ›  技术社区  ›  Albert Gao

如何显示忽略父填充的内容

  •  0
  • Albert Gao  · 技术社区  · 7 年前

    这是一张桌子,

    enter image description here

    这是同一张桌子的设计,当 onHover

    enter image description here

    这是问题所在,原始DIV的父DIV有一个 padding 要对齐表内容,因此当鼠标悬停时, background-color 也遵循相同的约束。所以它变成了:

    enter image description here

    如何解决这个问题,如何使它打破填充约束?

    我创造了一个 codepen

    3 回复  |  直到 7 年前
        1
  •  0
  •   doğukan    7 年前

    是这个吗?

    .box {
      background-color: lightgrey;
    
    }
    
    .row {
      display: flex;
      padding: 16px;
    }
    
    .row:hover {
      background-color:red;
    }
    
    .cell {
      width: 33%;
    }
    <div class='box'>
      <div class='row'>
        <div class='cell'>
          1
        </div>
        <div class='cell'>
          2
        </div>
        <div class='cell'>
          3
        </div>
      </div>
    </div>
        2
  •  0
  •   John    7 年前

    加上左/右填充。单元格如下:

        .cell {
          width: 33%;
          padding: 0 1rem;
        }
    

    https://codepen.io/anon/pen/wNmmxa

        3
  •  0
  •   newbiedude    7 年前

    把这个添加到你的CSS中怎么样?

    .box:hover{background-color: red;}
    

    https://codepen.io/vensa-albertgao/pen/yZKKXa