0

I'm not sure why the image is translating when the mouse is hovering over it. Also, let me know if there's anything I can move around or change the structure of in my HTML or CSS to make it more functional as I'm new to web development.

html {
  overflow: hidden;
}

body {
  background-image: url("https://images.unsplash.com/photo-1617538781871-bfe89e84b61f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=976&q=80");
  background-attachment: fixed;
  background-position: center; 
  background-repeat: no-repeat;
  background-size: cover;
  height: 100vh;
  margin: 0;
  backdrop-filter: blur(32px);
}

.image {  
  min-height: 500px;
  border-style: solid;
  border-width: 50px;
  margin: auto;
  display: block;
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  height: 50vh;
  
  box-shadow: 0px 0px 20px #000000;
  transition: all 0.5s;
  transform: translate(-50%, -50%);
}

.image:hover {
  transform: scale(1.05);
  box-shadow: 0px 0px 50px #000000;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/style.css">
    <script src="/script.js" defer></script>
  </head>  
  <body>
    <img class="image" src="https://images.unsplash.com/photo-1617538781871-bfe89e84b61f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=976&q=80" alt="Image">
  </body>
</html>
  • 1
    The `transform:translate` is overridden by `transform:scale` on hover, but you can include both in the hover definition. Does this answer your question? [How to apply multiple transforms in CSS?](https://stackoverflow.com/questions/10765755/how-to-apply-multiple-transforms-in-css) – showdev Apr 07 '21 at 02:37

1 Answers1

1

It's because of the transform: translate(-50%, -50%); of the image. On your image hover, you're overriding the transform attribute.

You would want to implement another way of centering your image like wrapping it on a container or something and use flexbox. Here's an example:

html {
  overflow: hidden;
}

body {
  background-image: url("https://images.unsplash.com/photo-1617538781871-bfe89e84b61f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=976&q=80");
  background-attachment: fixed;
  background-position: center; 
  background-repeat: no-repeat;
  background-size: cover;
  height: 100vh;
  margin: 0;
  backdrop-filter: blur(32px);
}

.container {
/* Make this change */
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
}

.image {  
  min-height: 500px;
  border-style: solid;
  border-width: 50px;
  margin: auto;
  display: block;
  margin: 0;
  height: 50vh;

  box-shadow: 0px 0px 20px #000000;
  transition: all 0.5s;
}

.image:hover {
  transform: scale(1.05);
  box-shadow: 0px 0px 50px #000000;
}
<div class="container">
<img class="image" src="https://images.unsplash.com/photo-1617538781871-bfe89e84b61f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=976&q=80" alt="Image">
</div>
Techuila
  • 1,237
  • 8
  • 12