Why are only some of my CSS Grid boxes expanding when I hover over them?

前端 未结 2 1902
生来不讨喜
生来不讨喜 2021-01-12 17:11

I\'m new to grids in CSS but I want to give it a try. I have a grid with 3x3 boxes. When I hover over a box it should should out the complete row... but that\'s not working.

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-12 17:39

    Here is an idea where you can rely on negative margin to create the overlap effect whithout changing the structure and using display:contents

    .container {
      display: grid;
      grid-gap: 10px;
      grid-template-columns: repeat(3, minmax(100px, auto));
      grid-template-rows: repeat(3, minmax(100px, auto));
    }
    
    [class^="item"] {
      text-align: center;
      box-sizing: border-box;
      padding: 10%;
      font-size: 5em;
      color: #0580d5;
      transition: .2s;
    }
    
    [class^="item"]:hover {
      z-index:2; /*we increase the z-index to cover the other*/
      background:
        /*we this to keep the initial background*/
        linear-gradient(rgba(40, 180, 240, .3),rgba(40, 180, 240, .3)),
        #fff;
    }
    
    [class^="item"]:nth-child(3n + 1):hover {
      margin-right:calc(-200% - 20px); /* we remove (2 x grid items + 2 x gap) */
    }
    [class^="item"]:nth-child(3n + 2):hover {
      margin-left:calc(-100% - 10px);
      margin-right:calc(-100% - 10px);
    }
    [class^="item"]:nth-child(3n + 3):hover {
      margin-left:calc(-200% - 20px);
    }
    
    .container>div {
      border: 2px solid #0580d5;
      background-color: rgba(40, 180, 240, .3);
      border-radius: 5px;
    }
    
    @media only screen and (min-width: 789px) {
      .container {
        grid-template-columns: repeat(3, 1fr);
      }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9

提交回复
热议问题