0

I want to center some items. I used display: flex and justify-content: center but it didn't work so far. How can I achieve horizontal and vertical alignment?

.middle {
  transition: .5s ease;
  opacity: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  text-align: center;
}

` .text {
  display: flex;
  align-items: center;
  justify-content: center;
  color: black;
  font-size: 16px;
  padding: 16px 32px;
}
<div class="main-blog-img">
  <img src="img/images/Asset2.png" class="image">
  <a href="">
    <div class="middle">
      <div class="text"><i class="fa fa-plus"></i></div>
    </div>
  </a>
</div>`
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
pattes
  • 35
  • 5

2 Answers2

0

This question has answered many times. See here, for example. How to horizontally center an element

Your problem is that you need to make the parent div a flex container.

.middle {
  display: flex;
  align-items: center;
  justify-content: center;
}
I Stand With Israel
  • 1,971
  • 16
  • 30
0

Change your style with

   .middle {
    transition: .5s ease;
    opacity: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    text-align: center;  display: flex;
  align-items: center;
  justify-content: center;
}

.text {
color: black;
font-size: 16px;
padding: 16px 32px;
}
PHP Geek
  • 3,949
  • 1
  • 16
  • 32