Prevent image resizing in CSS image transitions on hover within a grid layout with these helpful tips

I am looking to create a smooth image transition effect where one image transitions to another when hovered over. Currently, I have code that achieves this, but there is an issue with the initial scaling of the top image. It automatically resizes to a larger size, causing the transition to appear as if the image has moved due to the different dimensions. Even though both images are supposed to be the same size, the top image appears larger initially.

You can see my code and the issue in action in the preview here: https://jsfiddle.net/k3jLxofv/1/

In the "Project Area 1" section, you will notice two images loaded initially, with one appearing smaller than the other despite them being identical (as indicated by the lines). I am unsure why there is a discrepancy in size between the two images. My goal is to hover over "Project Area 1" and achieve a seamless transition without any visible movement.

"Project Area 2" demonstrates the expected image sizes at both states (hovered on and not).

Thank you for your help!

.projects-grid-setup {
  display: grid;
  grid-template-columns: repeat(2, minmax(320px, 1fr));
  grid-gap: 0.9rem;
  width: 98.3%;
  margin: 0 auto;
  margin-bottom: 1rem;
}

.projects-tile {
  background: black;
}

.projects-tile-picture {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.crossfade {
  position: relative;
}

.crossfade img.fade {
  position: absolute;
  left: 0;
  opacity: 1;
  transition: opacity 0.55s ease-in-out;
}

.crossfade img.top:hover {
  opacity: 0;
}
<div class="projects-grid-setup">
  <!--Project Start-->
  <a class="projects-tile" href="google.com" target="_blank">
    <div class="crossfade">
      <img class="projects-tile-picture top fade" src="https://picsum.photos/300/200?1" />
      <img class="projects-tile-picture" src="https://picsum.photos/300/200?1" />
    </div>
    <p class="projects-tile-title">
      Project Area 1
    </p>
  </a>
</div>

Answer №1

Ensure to keep the height set as 'auto' in the code snippet provided

.projects-tile-picture {
  width: 100%;
  height: auto;
  object-fit: cover;
}

Answer №2

To prevent the additional space at the bottom, change the display type of .projects-tile-picture to block. The extra space does not affect the static image (before hovering), but it does make the absolutely positioned image slightly larger (height: 100%).

.projects-grid-setup {
  display: grid;
  grid-template-columns: repeat(2, minmax(320px, 1fr));
  grid-gap: 0.9rem;
  width: 98.3%;
  margin: 0 auto;
  margin-bottom: 1rem;
}

.projects-tile {
  background: black;
}

.projects-tile-picture {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.crossfade {
  position: relative;
}

.crossfade img.fade {
  position: absolute;
  left: 0;
  opacity: 1;
  transition: opacity 0.55s ease-in-out;
}

.crossfade img.top:hover {
  opacity: 0;
}
<div class="projects-grid-setup">
  <!--Project Start-->
  <a class="projects-tile" href="google.com" target="_blank">
    <div class="crossfade">
      <img class="projects-tile-picture top fade" src="https://picsum.photos/300/200?1" />
      <img class="projects-tile-picture" src="https://picsum.photos/300/200?2" />
    </div>
    <p class="projects-tile-title">
      Project Area 1
    </p>
  </a>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Prevent the jQuery Circle Progress animation from starting at 0 when data is updated

I am utilizing the jquery-circle-progress plugin. Everything is functioning as expected. However, I have added a button to update the percentage value. <button id="add">Add</button> In the example below, when the button is clicked, ...

Challenges related to a hamburger icon menu

Trying to create a functional hamburger menu but facing some difficulty in getting it right. Here's the concept: The page layout on larger screens should resemble this: https://i.sstatic.net/CBqMP.png Smaller screens will display a hamburger menu, ...

Mysterious behavior in JavaScript causes it to skip the second index of an array

Below is a lengthy snippet of html code that I would like to discuss: <!DOCTYPE html> <html> <head> <script type = "text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script> <script> $( ...

What significance do the blank quotes hold in terms of value?

Is it possible to enter multiple values for the "favoriteCities" field since the value is empty? <input name="favoriteCities[]" value=""/> ...

"Troubleshooting a CSS Animation Problem: Button Animation Glitch

I was working on a code snippet with the following structure: shakingErr class from CSS was not being applied correctly when the user clicked submit with empty/wrong fields. How can this be resolved so that the shaking error animation is triggered proper ...

Styled Components do not have the ability to define :hover states

export const WatchVideoButtonWrapper = styled.div` position: absolute; bottom: 80px; right: 33px; background-color: ${(props) => props.bgColor}; color: ${(props) => props.color}; border-radius: 100px; transition: all 0.1s; ...

Unable to define attributes of a non-existent element (specifically 'innerHTML') within a Vanilla JavaScript component

Currently, I'm developing a Vanilla JS Component and encountering a challenge with utilizing the innerHTML() function to modify the text within a specific ID in the render() HTML code. The problem at hand is the Cannot set properties of null (setting ...

What is the reason for xhr.response being set to null whenever there is a warning in my PHP code?

In order to clarify, it's important to note that the .php files belong to my professor and should not be altered. The issue arises from the fact that $VALORES on line 186 is uninitialized, and I have already reported this to my professor. Below is th ...

Items vanish when hovering over them

Creating nested links in the sidebar navigation bar with CSS has been a challenge for me. While hovering over main links, I can see the sub-links being displayed. However, when I try to hover/click on the children of the sub-links, they disappear. Note: M ...

How can we make the text of a link wrap, without wrapping the links themselves?

In my design, I am using links in a list to create tabs for responsiveness. The issue arises when the width is reduced; instead of the last tab wrapping onto the next line, I want the text within the link itself to wrap. Setting the max-width to be 33% wo ...

Divide the screen evenly with a split, where one side is filled with an image

As a newcomer, I'm curious about how to split the screen in half with a form on the left and an image taking up the full height on the right. Is there a simple way to achieve this? For reference, here is a link showing exactly how I envision it: Exam ...

The Photoswipe default user interface cannot be located

While attempting to incorporate PhotoSwipe into my website, I encountered an uncaught reference error related to The PhotoswipeUI_Default is not defined at the openPhotoSwipe function Below is the code snippet that I have been working on: <!doctyp ...

Alter the input background color steadily through javascript transformation

Is there a way to gradually fade in a new background color using JavaScript only? I am currently changing the background color with the following code: // html <input onfocus="onFocus(this)" onblur="onFocus(this)" type="text"> // JS function onFoc ...

When the window is resized, the css('position') method may return undefined

Here is the code I am using to detect the browser resize event: window.onresize = function(){ var style = $('#button-selection').css('position'); if(style == "relative"){ $("#button-section").css("position"," "); }else if(style == ...

Using single and double quotes in combination with brackets in PHP: a guide

My struggle lies in using single and double quotes along with brackets in PHP $nestedData[] = '<a href="JavaScript:newPopup('loghistory.php?logid='.$row['user_id'].'')"> History('.$countqry.')</a>&a ...

Tips for adjusting the transition speed duration in Bootstrap 4

Is there a way to enhance the smoothness of image slides? I can adjust the speed between slides using data-interval, but how do you tweak the speed within each slide? According to the documentation: .carousel-item{ transition: transform 5s ease, opacit ...

Unable to apply border-radius to a specific HTML table

I am facing an issue where the border-radius is not being displayed on a specific HTML table. I am having difficulty in applying rounded corners to the entire table so that the table edges have a 30px curve. Here is an example of what I am looking for: ht ...

Show brief tags all on one line

This image showcases the functionality of the site, specifically in the "Enter a code" column where users can input data using TagsInput. I am seeking to enhance this feature by ensuring that shorter tags are displayed on a single line. While I am aware th ...

Spin (svg, css) in a circular motion from any chosen point, without using the transform-origin property

My goal is to rotate an svg-rect around a specific point without relying on transform-origin, but instead using chained translates and rotation. After conducting some research, I discovered the following method: Translate the rotation point to the origi ...

What could be causing the blurriness in the image?

I'm having trouble displaying an image below my navigation bar. The image I'm trying to display is located at https://i.sstatic.net/8PUa3.png and has dimensions of 234 x 156 px. However, the image appears blurry and only a portion of it can be s ...