I tried to center a div with 2 photos exactly in the center. However it didn’t go well. Tried different types of positioning, display flex, align items justify content, etc, nothing allows me to
move image container to the center.
Here is my code:
.grid {
display: grid;
gap: 1.5rem;
}
.section {
padding: 4.5rem 0 2rem;
}
.home_container {
position: relative;
align-items: center;
justify-content: center;
}
.home_images {
position: absolute;
display: flex;
left: 50%;
top: 50%;
}
.home_big-img {
z-index: 12;
}
.home_small-img {
transform: translateX(-9rem);
}
<section class="section home grid">
<div class="home_container container">
<div class="home_images">
<img src="./img/man.png" alt="" class="home_big-img">
<img src="./img/plane.png" alt="" class="home_small-img">
</div>
</div>
</section>
Here is first image:
And here is second one:
3
Answers
Will this help?
In case you want to make it vertically center, use translateY instead of translateX and top: 50% instead of left: 50%
To center the images, you can use flexbox.
In the modified code, I removed the position: absolute from the .home_images class and replaced it with display: flex, align-items: center, and justify-content: center in the .home_container class to center the entire image container on the screen. I also set the height of the container to 100vh to make sure it takes up the full viewport height.
Save the changes, and the images should now be centered on the mobile screen resolution.
To center child elements (img in your case) in a parent element, you should:
Have a good day!