skip to Main Content

Whenever I hover over between the text and pizza image in the following code block, the div starts shaking until I hover it over either one of the elements, i.e., the image or the text.

.maincontent img {
  opacity: 0.8;
  overflow: hidden;
  transition: all 0.2s ease-in-out;
  background-size: cover;
}

.maincontent img:hover {
  margin-top: -200px;
  opacity: 0.4;
  transition: all 0.2s ease-in-out;
}

.mainpara:hover~.mainimg {
  margin-top: -200px;
  opacity: 0.4;
  transition: all 0.2s ease-in-out;
}

.maincontent {
  overflow: hidden;
  position: relative;
  margin: 15px;
  background-color: #ff78c54e;
  width: 450px;
  border-radius: 20px;
  padding: 5px;
  border-color: #e8a9a9b4;
  height: 450px;
  transition: all 0.1s ease-in-out;
}
<div class="maincontent">
  <img class="mainimg" src="https://c4.wallpaperflare.com/wallpaper/234/543/684/food-pizza-wallpaper-preview.jpg" alt="">
  <p class="mainpara">Lorem ipsum dolor sit amet consectetur adipisicing elit. Reiciendis praesentium laborum ducimus, debitis quod tenetur quam enim quaerat, sed labore fugit neque veritatis molestiae quasi nihil asperiores, nobis quae tempora. Consequuntur neque dignissimos assumenda Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptatibus consequatur quis, accusantium aut sunt optio esse hic nam. Perferendis molestias deserunt nisi quibusdam minus cum sint. Blanditiis officiis ullam architecto mollitia animi vero tempore ipsa iure culpa reiciendis quisquam ratione maxime, consectetur quod, natus pariatur eveniet, accusantium rerum? Dignissimos, voluptas!</p>
</div>

2

Answers


  1. I was working on it in my Arch Linux System however friend since it’s html and css operating system does not matter so anyways I have fixed the shaking problem and you move the mouse in any direction on the content it’ll zoom into the main image

    <style>
        .maincontent img {
        opacity: 0.8;
        overflow: hidden;
    
        background-size: cover;
      }
      
      .maincontent img:hover {
        margin-top: -200px;
        opacity: 0.4;
    
      }
      
      .mainpara:hover~.mainimg {
        margin-top: -200px;
        opacity: 0.4;
    
      }
      
      .maincontent {
        overflow: hidden;
        position: relative;
        margin: 15px;
        background-color: #ff78c54e;
        width: 450px;
        border-radius: 20px;
        padding: 5px;
        border-color: #e8a9a9b4;
        height: 450px;
        transition: all 0.1s ease-in-out;
      }
    </style>
      <div class="maincontent">
        <img class="mainimg" src="https://c4.wallpaperflare.com/wallpaper/234/543/684/food-pizza-wallpaper-preview.jpg"  alt="" height="476px">
        <p class="mainpara" >Lorem ipsum dolor sit amet consectetur adipisicing elit. Reiciendis praesentium laborum ducimus, debitis quod tenetur quam enim quaerat, sed labore fugit neque veritatis molestiae quasi nihil asperiores, nobis quae tempora. Consequuntur neque dignissimos assumenda Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptatibus consequatur quis, accusantium aut sunt optio esse hic nam. Perferendis molestias deserunt nisi quibusdam minus cum sint. Blanditiis officiis ullam architecto mollitia animi vero tempore ipsa iure culpa reiciendis quisquam ratione maxime, consectetur quod, natus pariatur eveniet, accusantium rerum? Dignissimos, voluptas!</p>
      </div>

    I’ve already tested it so you don’t have to worry for bugs.
    Additionally i’ve attached a video of your original and your original (but rebuilt by me) it’s a mkv file and you can’t watch it in the browser so you’ll have to download the mkv if you want to see the comparison (don’t worry it’s only 9MB) and it’s optional Side by side comparison MKV download

    Login or Signup to reply.
  2. The reason this is happening is to do with the margin: -200px you set when the image is hovered. To better explain what is happening I will walk you through the way css sees things. When you first start, the margin is 0, then whenever you hover the image, the margin transitions to -200px. So let’s say that you now move your mouse over the text to act as a guide when reading. Since the image is not hovered anymore it will "move down" back to where it originally was, right? Well, not exactly, you see when the image moves, the cursor will come into contact with the image again. This will make the image move back up to reveal the text, however, it only gets so far because the cursor is no longer on the image, this repeated process creates that shaky effect that you see.

    Now that we understand why this happens, let’s see how we can fix it. With this recreation I will be assuming what you want out of this card because you never stated what the card was meant to do. Having said that, I assume the card is meant to display an image of a pizza, and then slide upwards to reveal a description below it when hovered. Here is the html of that I will be using.

    <div id="mask">
      <div id="content">
        <img src="https://imgmedia.lbb.in/media/2020/12/5fe323e5282cde4610e3face_1608721381076.jpg" id="preview">
        <p id="desciption">This is a yummy pizza.</p>
      </div>
    </div>
    

    Now for the css, we know we can’t use the image hover to move the image as it will move and therefore cause problems, so we need to pick an element that will always stay still, and we don’t need to look further than the #mask element in the html above. So, what I did is I set the #mask element to tell me when a hover has occurred and simply translated the Y of the #content element. You can even adjust how much the image slides. Below is the code and preview.

    #mask { 
      width: 200px;
      height: 200px;
      overflow: hidden;
      border: 1px solid black;
      border-radius: 15px;
    }
    
    #content {
      width: 200px;
      overflow: hidden;
      text-align: center;
      transition: transform .3s;
    }
    
    #mask:hover #content {
      transform: translateY(-65px);
    }
    
    #preview {
      background-position: center;
      height: 200px;
      transition: opacity .3s
    }
    
    #mask:hover #preview {
      opacity: 0.5;
    }
    <div id="mask">
      <div id="content">
        <img src="https://imgmedia.lbb.in/media/2020/12/5fe323e5282cde4610e3face_1608721381076.jpg" id="preview">
        <p>This is a yummy pizza.</p>
      </div>
    </div>

    Hope this helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search