首页 > 解决方案 > 如何将图标图像完美居中

问题描述

我有一个图标 img 并试图将其置于卡片 div 中,但不知何故无法正常工作。我已经尝试过text-align: center;display:flex; justify-content: center;但都没有帮助。我在下面附上了我的代码。任何帮助表示赞赏。提前致谢。

* {
  box-sizing: border-box;
}

.card {
  background-color: #fff;
  border-radius: 4px;
  max-width: 0px;
  height: 0px;
  position: absolute;
  padding: 20px;
  color: #444;
  cursor: pointer;
  top: 3%;
  right: 0.5%;
}

.card:before {
  content: '';
  display: block;
  position: absolute;
  background-color: #ccc;
  left: 20px;
  right: 20px;
  bottom: 0;
  top: 50%;
  z-index: -1;
  box-shadow: 0 0 40px lighten(#000, 60%);
  transition: box-shadow .2s ease-in-out;
}

.card.level-1:hover:before {
  box-shadow: 0 0 80px lighten(#000, 60%);
}

.card level-1 img {
  display: flex;
  justify-content: center;
}
<div class="card level-1">
  <img src="https://img.icons8.com/windows/32/000000/microphone--v2.png" />
</div>

标签: htmlcss

解决方案


您应该像这样将 flex 放在图像的容器上:

.card {
  display: flex;
  justify-content: center;
  align-items: center;
}

推荐阅读