提问者:小点点

如何在<div>中完美地居中图标图像


我有一个图标img,并试图将其居中在一个卡片div中,但不知何故不起作用。我已经尝试了text-align:center;display:flex;justify-content:center;和两者都没有帮助。我在下面附上了我的代码。感谢任何帮助。提前道谢。

null

* {
  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>

null


共1个答案

匿名用户

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

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