Image movement outside Div
P粉004287665
P粉004287665 2023-08-13 15:21:15
0
1
434
<p>I'm creating a website that has a black div in the center of the page, and when I hover over it, the image in the center rotates. The problem is that although I put the image inside a div, it goes outside the bounds of the div. How to fix this problem? This is my HTML code: </p> <pre class="brush:js;toolbar:false;">const image = document.getElementById("pic"); image.classList.add("rotate"); const clone = image.cloneNode(true); clone.classList.add("rotate-negative"); clone.classList.add("top-image"); clone.classList.add("shadow-lg"); document.getElementById("container").appendChild(clone);</pre> <pre class="brush:css;toolbar:false;">img { transition: 0.5s; max-height: 600px; } .rotate{ transform: rotate(15deg); position: absolute; top: 0; left: 0; } .rotate-negative{ transform: rotate(-5deg); position: absolute; top: 0; left: 0; } .top-image:hover{ transform: rotate(0deg); transition: 0.5s; max-height: 620px; } #container{ margin-left: auto; margin-right: auto; height: 500px; width: 30%; background-color: black; }</pre> <pre class="brush:html;toolbar:false;"><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css " rel="stylesheet" integrity="sha384-4bw /aepP/YC94hEpVNVgiZdgIC5 VKNBQNGCHeKRQN PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous"> <div id="container"> <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcToJI-yx2dCcsVf3fYDDRF908iRO_dlTsL9H32iWWIkT_1dYuimsdBUZKTIZgYC61z6Vik&usqp=CAU" id="pic"> </div></pre> <p>I want the image to be placed inside the div. However, I don't want to remove the "position" property from the CSS as this would break the design of the image being in the center. What can I do to keep the image inside the div without removing the "position" attribute? </p> <p><em><strong>I saw other questions with the same problem but they didn't help me. </strong></em></p>
P粉004287665
P粉004287665

reply all(1)
P粉054616867

Just add position: relative to your #container selector. This will position the child elements relative to this element.

If desired, you can also add overflow:hidden to truncate content beyond the scope of this element. However, if you don't need it, delete it.

const image = document.getElementById("pic");
image.classList.add("rotate");
const clone = image.cloneNode(true);
clone.classList.add("rotate-negative");
clone.classList.add("top-image");
clone.classList.add("shadow-lg");
document.getElementById("container").appendChild(clone);
img {
  transition: 0.5s;
  max-height: 600px;
}

.rotate{
  transform: rotate(15deg);
  position: absolute;
  top: 0;
  left: 0;
}

.rotate-negative{
  transform: rotate(-5deg);
  position: absolute;
  top: 0;
  left: 0;
}

.top-image:hover{
    transform: rotate(0deg);
    transition: 0.5s;
    max-height: 620px;
}
#container{
    margin-left: auto;
    margin-right: auto;
    height: 500px;
    width: 30%;
    background-color: black;
    
    /*added*/
    position: relative;
    /*overflow:hidden;*/
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">

<div id="container">
  <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcToJI-yx2dCcsVf3fYDDRF908iRO_dlTsL9H32iWWIkT_1dYuimsdBUZKTIZgYC61z6Vik&usqp=CAU" id="pic">
</div>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template