当悬停 card-action 时,物化卡片会移动

Materialize cards move when card-action is hovered

我正在尝试在使用 Materialize CSS 的项目中使用 z-index 在 header 之上分层卡片的顶行。由于卡片现在使用相对定位,因此当 card-action 悬停时,它会导致其中一些卡片移动(结果在移动视图中不太明显,因为每行只有一张卡片)。我也不打算让 card-action 文本在悬停时移动到中心,尽管我对这种效果没意见。我试过使用绝对定位以及更改应用定位的元素。如何防止卡片移动并仍然使用 z-index?这是我的代码和 link 到我的 (codepen):

@import url('https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400&display=swap');

:root {
  --primary-color: #c31432;
  --primary-text-color: rgba(0,0,0,0.5);
  
  --primary-text: "Open Sans", sans-serif;
}

* {
  font-family: "Open Sans", sans-serif;
  font-weight: 400;
}

#works-header {
  position: absolute;
  height: 500px;
  width: 100vw;
  z-index: 10;
}

#works-header h1 {
  font-size: 3rem;
  letter-spacing: 1px;
  margin: 0 auto;
}

#cards {
  position: relative;
  top: 400px;
  z-index: 1000;
}

.card-image {
  cursor: pointer;
  transition: all .3s ease;
}

.card-image:hover {
  border: 3px solid var(--primary-color);
  opacity: 0.85;
}

.card-image:hover .card-title {
  display: none;
}

.card-action {
  width: 50%;
  cursor: pointer;
  font-size: 1.2rem;
  transition: all .3s ease;
}

.card-action:hover {
  width: 100%;
  position: absolute; /* this prevents cards from shifting due to relative positioning on the card element */
  opacity: 0.7;
  border-bottom: 2px solid var(--primary-color);
}

.card-tags {
  display: none;
}

  .card-image:hover .card-tags {
    display: block;
    position: absolute;
    left: 1rem;
    bottom: 2rem;
    letter-spacing: 2px;
    font-weight: 300;
  }
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet"/>
<!-- Todos: fix all cards aligned on the right side (card-action does not work properly) -->

<section class="center" id="works">
  <div id="works-header" class="black valign-wrapper">
    <h1 class="white-text center-align">Check out some of my latest projects</h1>
    <p></p>
  </div>
<div class="container row" id="cards">
    <div class="col s12 m6 l4">
      <div class="card z-depth-3">
        <div class="card-image">
          <img src="https://i.imgur.com/vxaE0nK.png">
          <span class="card-tags white-text">HTML, CSS, Javascript</span>
          <span class="card-title white-text">Netflix Clone</span>
        </div>
        <div class="card-content flow-text">
          <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusantium nesciunt repellat consequatur autem! Laboriosam dignissimos rerum eum ea cum quibusdam!</p>
        </div>
        <div class="card-action">
          <a href="#" class="pink-text">View project</a>
        </div>
      </div>
    </div>
  
    <div class="col s12 m6 l4">
      <div class="card z-depth-3">
        <div class="card-image">
          <img src="https://i.imgur.com/vxaE0nK.png">
          <span class="card-tags white-text">HTML, CSS, Javascript</span>
          <span class="card-title white-text">Netflix Clone</span>
        </div>
        <div class="card-content flow-text">
          <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusantium nesciunt repellat consequatur autem! Laboriosam dignissimos rerum eum ea cum quibusdam!</p>
        </div>
        <div class="card-action">
          <a href="#" class="pink-text">View project</a>
        </div>
      </div>
    </div>
  
    <div class="col s12 m6 l4">
      <div class="card z-depth-3">
        <div class="card-image">
          <img src="https://i.imgur.com/vxaE0nK.png">
          <span class="card-tags white-text">HTML, CSS, Javascript</span>
          <span class="card-title white-text">Netflix Clone</span>
        </div>
        <div class="card-content flow-text">
          <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusantium nesciunt repellat consequatur autem! Laboriosam dignissimos rerum eum ea cum quibusdam!</p>
        </div>
        <div class="card-action">
          <a href="#" class="pink-text">View project</a>
        </div>
      </div>
    </div>
  
    <div class="col s12 m6 l4">
      <div class="card z-depth-3">
        <div class="card-image">
          <img src="https://i.imgur.com/vxaE0nK.png">
          <span class="card-tags white-text">HTML, CSS, Javascript</span>
          <span class="card-title white-text">Netflix Clone</span>
        </div>
        <div class="card-content flow-text">
          <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusantium nesciunt repellat consequatur autem! Laboriosam dignissimos rerum eum ea cum quibusdam!</p>
        </div>
        <div class="card-action">
          <a href="#" class="pink-text">View project</a>
        </div>
      </div>
    </div>
  
    <div class="col s12 m6 l4">
      <div class="card z-depth-3">
        <div class="card-image">
          <img src="https://i.imgur.com/vxaE0nK.png">
          <span class="card-tags white-text">HTML, CSS, Javascript</span>
          <span class="card-title white-text">Netflix Clone</span>
        </div>
        <div class="card-content flow-text">
          <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusantium nesciunt repellat consequatur autem! Laboriosam dignissimos rerum eum ea cum quibusdam!</p>
        </div>
        <div class="card-action">
          <a href="#" class="pink-text">View project</a>
        </div>
      </div>
    </div>
  
    <div class="col s12 m6 l4">
      <div class="card z-depth-3">
        <div class="card-image">
          <img src="https://i.imgur.com/vxaE0nK.png">
          <span class="card-tags white-text">HTML, CSS, Javascript</span>
          <span class="card-title white-text">Netflix Clone</span>
        </div>
        <div class="card-content flow-text">
          <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusantium nesciunt repellat consequatur autem! Laboriosam dignissimos rerum eum ea cum quibusdam!</p>
        </div>
        <div class="card-action">
          <a href="#" class="pink-text">View project</a>
        </div>
      </div>
    </div>
  
    
    <div class="col s12 m6 l4">
      <div class="card z-depth-3">
        <div class="card-image">
          <img src="https://i.imgur.com/vxaE0nK.png">
          <span class="card-tags white-text">HTML, CSS, Javascript</span>
          <span class="card-title white-text">Netflix Clone</span>
        </div>
        <div class="card-content flow-text">
          <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusantium nesciunt repellat consequatur autem! Laboriosam dignissimos rerum eum ea cum quibusdam!</p>
        </div>
        <div class="card-action">
          <a href="#" class="pink-text">View project</a>
        </div>
      </div>
    </div>
  
    <div class="col s12 m6 l4">
      <div class="card z-depth-3">
        <div class="card-image">
          <img src="https://i.imgur.com/vxaE0nK.png">
          <span class="card-tags white-text">HTML, CSS, Javascript</span>
          <span class="card-title white-text">Netflix Clone</span>
        </div>
        <div class="card-content flow-text">
          <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusantium nesciunt repellat consequatur autem! Laboriosam dignissimos rerum eum ea cum quibusdam!</p>
        </div>
        <div class="card-action">
          <a href="#" class="pink-text">View project</a>
        </div>
      </div>
    </div>
  </div>
  </div>  
</section>

一种解决方案是为卡片图像添加边框,这样您就可以只过渡不透明度和颜色,而不是边框​​大小(这反过来会重新计算图像的大小并导致闪烁)。浏览器一次处理很多。

.card-image {
  cursor: pointer;
  transition: all .3s ease;
  border: 3px solid black;
}

Codepen.

编辑:

抱歉,我发现有两个问题。

好的,所以您不需要在页眉上进行绝对定位 - 绝对定位会将项目从页面流中移出。无需在此处执行此操作。从卡片中取出亲戚并使用负边距:

#cards {
    top: 400px;
    z-index: 1000;
    margin-top: -100px;
}

我已经从悬停中删除了绝对定位,因为这不是一个好主意。当您需要精确控制元素定位时,绝对定位就派上用场了。这里不需要那个。我还取消了 50% 的宽度并添加了默认边框以防止卡片动作闪烁。

发生这种情况的主要原因是 hover 上的 border。如果您真的需要这种外观 - 在 card-image 中添加空的 div 并为其添加插图 box-shadow

<div class="card-image">
    <img src="https://i.imgur.com/vxaE0nK.png">
    <span class="card-tags white-text">HTML, CSS, Javascript</span>
    <span class="card-title white-text">Netflix Clone</span>
    <div class="hover-shadow"></div>
</div>

和 CSS:

.card-image .hover-shadow {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}
.card-image:hover .hover-shadow {
    box-shadow: inset 0 0 0 2px (--primary-color);
}

完整示例at codepen