search

Home  >  Q&A  >  body text

Image movement outside Div

<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粉004287665P粉004287665495 days ago452

reply all(1)I'll reply

  • P粉054616867

    P粉0546168672023-08-14 00:35:03

    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>

    reply
    0
  • Cancelreply