0

What I want to achieve:

Center an image horizontally and vertically in the right flexbox item as shown in this picture:

enter image description here

What's the best way to do it?

Here's my Codepen: https://codepen.io/AlexZeitler/pen/JayvNg

I got the image centered horizontally but not vertically using this .grid-right class:

.grid-right {
    width: 67%;
    height: 100vh;
    background-size: cover;
    text-align: center;
} 
Alexander Zeitler
  • 11,919
  • 11
  • 81
  • 124

3 Answers3

6
.grid-right {
  display: flex;            /* new */
  align-items: center;      /* new */
  justify-content: center;  /* new */
  width: 67%;
  height: 100vh;
  background-size: cover;
  text-align: center;
}

revised codepen

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
0

div {
  /* for the demo */
  width: 100%;
  height: 300px;
  background-color: blue;
  
  /* makes the image gets centered */
  display: flex;
  justify-content: space-around; /* horizontal centering */
  align-items: center; /* vertical centering */
}

img {
  /* for the demo */
  height: 100px;
  background-color: white;
}
<div>
  <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/35/Tux.svg/1200px-Tux.svg.png">
</div>
Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
-2

You can use the following to center an image vertically and horizontally in flexbox:

.grid-right {
    width: 67%;
    height: 100vh;
    background-size: cover;
    text-align: center;
    display: flex;            /* new */
    align-items: center;      /* new */
    justify-content: center;  /* new */
}
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
georgedum
  • 491
  • 3
  • 10