anon10023108
anon10023108

Reputation: 25

how to perfectly center an icon image inside a <div>

I had an icon img and was trying to center it inside a card div but is somehow not working. I already tried text-align: center; and display:flex; justify-content: center; and both did not help. I attached my code below. Any help is appreciated. Thanks in advance.

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

Upvotes: 0

Views: 265

Answers (1)

Marko
Marko

Reputation: 68

You should put flex on the container of the image like this:

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

Upvotes: 1

Related Questions