HTML CSS image grow

1.1k Views Asked by At

I want to add 6 images in an HTML form. I would like 3 in one row and the other 3 in the next row. the user brings the cursor on any of the images it should enlarge but should not disturb position of another 5 image I would also like the image to enlarge when the cursor is on any of the images. Also, the positions of the other 5 images shouldn't move when one of the images is enlarged.

Does anyone know how to do this with only HTML and CSS?

2

There are 2 best solutions below

0
On BEST ANSWER

Here is a solution. I used CSS Grid to make 3 images in a row and the other 3 for the next row. However, you don't have to use this.

The trick I used to enlarge the image was scale();.

.wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.image-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 0.5rem;
}
img:hover {
  transform: scale(1.2);
}
<div class="wrapper">
  <div class="image-container">
    <img src="https://placeimg.com/200/200/1" alt="1" />
    <img src="https://placeimg.com/200/200/2" alt="2" />
    <img src="https://placeimg.com/200/200/3" alt="3" />
    <img src="https://placeimg.com/200/200/4" alt="4" />
    <img src="https://placeimg.com/200/200/5" alt="5" />
    <img src="https://placeimg.com/200/200/6" alt="6" />
  </div>
</div>

0
On

I made you a solution using javascript and css grid.

window.onload = function () {
    let img_show = document.querySelectorAll(".container_img");

    img_show.forEach(function (img_show_current) {
        img_show_current.onclick = function () {
            this.classList.toggle("img_show");
        };
    });
};
.container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-gap: 30px;
    position: relative;
}

.container_img img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    cursor: pointer;
    transition: transform 0.5s;
}

.container_img img:hover {
    transform: scale(1.1);
}

.container_img.img_show {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}
<div class="container">
    <div class="container_img">
        <img src="https://w-dog.ru/wallpapers/10/7/477636637359519.jpg" />
    </div>
    <div class="container_img">
        <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQcajj6p3VxDPOtGGNOd_7BwzcpO36o6rW34Q&usqp=CAU" />
    </div>
    <div class="container_img">
        <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTpAlH_q4oDlCu94hdmbMb9cmIRzpA-jsW_CQ&usqp=CAU" />
    </div>
    <div class="container_img">
        <img src="https://static9.depositphotos.com/1594308/1110/i/600/depositphotos_11107478-stock-photo-fantasy.jpg" />
    </div>
    <div class="container_img">
        <img src="https://klv-oboi.kz/img/gallery/1/thumbs/thumb_l_11350.jpg" />
    </div>
    <div class="container_img">
        <img src="https://klike.net/uploads/posts/2019-01/1547367999_1.jpg" />
    </div>
</div>