Animation of CSS height when image/icon is hovered over

Having an issue with my icon when hovering. I am trying to change the img src on hover. Here is the code I currently have:

#aks {
  width: 0px;
  max-height: 0;
  transition: max-height 0.15s ease-out;
  overflow: hidden;
  background: url("https://img.icons8.com/windows/32/000000/like.png") no-repeat;
  padding: 50px;
}

#aks:hover {
  background: url("https://img.icons8.com/officexs/32/000000/like.png")
    no-repeat;
  max-height: 500px;
  transition: max-height 0.25s ease-in;
}
<img id="aks" src="https://img.icons8.com/windows/32/000000/like.png" />

My goal is to have the outline heart icon replaced by a filled heart icon upon hover. The transition should simulate the filling of the heart from bottom height 0 to full height, creating a visually appealing effect. This example illustrates what I am aiming for: codepen

I would greatly appreciate any alternative solutions or suggestions apart from the one provided. Thank you in advance!

Answer №1

Creating a CSS-only animation for this design can be quite challenging as it involves applying the sliding effect to multiple separate elements - the rotated square and two circles.

To illustrate, here is a demonstration using a font that contains a heart symbol. I have utilized Webdings in this example, but it is recommended to use Font Awesome in a live coding environment.

In essence, the technique involves setting a background gradient that is twice the height of the text, with a split of 50% white and 50% red. Upon hover, the background shifts from displaying the white segment to revealing the red section. However, it's worth noting that certain properties implemented in this method may only function correctly on webkit browsers, such as text-stroke, which outlines the text, and background-clip, responsible for masking the non-text portion of the background within the span element.

span {
  background-size: 100% 200%;
  background-image: linear-gradient(to bottom, white 50%, red 50%);
  color: transparent;
  font-family: webdings;
  font-size: 200px;
  transition: background-position 2s;
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-stroke-width: 5px;
  -webkit-text-stroke-color: red;
  text-stroke-width: 5px;
  text-stroke-color: red;
}
span:hover {
    background-position: 0 100%;
}
<span>Y</span>

Answer №2

If you are considering using css, here is an interesting option for you:

.heart {
  background-color: red;
  height: 30px;
  width: 30px;
  transform: rotate(-45deg);
  transition: background-color 1s;
}

.heart::before,
.heart::after {
  content: '';
  background-color: red;
  height: 30px;
  width: 30px;
  border-radius: 50%;
  position: absolute;
  transition: background-color 1s;
}

.heart::before {
  top: -15px;
}

.heart::after {
  left: 15px;
}

.heart:hover,
.heart:hover::before,
.heart:hover::after {
  background-color: #F5A9AE;
}

body {
  display: flex;
  height: 100vh;
}

.heart {
  margin: auto;
}
<div class="heart"></div>

Answer №3

If you want to add a background animation, you can achieve it with the code snippet below:

.heart {
  width: 50px;
  height:50px;
  padding-top:50px;
  background: 
    url("https://img.icons8.com/officexs/32/000000/like.png") bottom padding-box content-box,
    linear-gradient(#fff,#fff) bottom padding-box content-box,
    url("https://img.icons8.com/windows/32/000000/like.png");
  background-size:100% 100%;
  background-repeat:no-repeat;
  box-sizing:border-box;
  transition:0.5s;
}

.heart:hover {
   padding-top:0;
}
<div class="heart"></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

Reduce the value in the database when a button is clicked using PHP

I am currently working on a carpooling website and I could really use some assistance. I have implemented a button that, once clicked, should decrease the number of available free spaces in the car by one (meaning the person who clicks it will reserve a ...

Using Column CSS Property with Google Maps in Chrome: A Potential Bug?

I'm facing an interesting issue and I could use some help understanding or fixing it. It seems like there might be a bug or a solution to this problem. I am currently working on a layout using the CSS3 property columns. Everything looks fine in Firefo ...

What is the best way to notify the user if the percentage entered is not a numeric value?

My role is to notify the user when the entered value exceeds the acceptable range: if (document.myForm.outputPercentage.value <= 0 || document.myForm.outputPercentage.value >= 100) { alert( "Please enter a percentage between 1 and 100 ...

The animation in Material UI does not smoothly transition with the webkit scrollbar

I've been experimenting with CSS animations in Material UI's sx property to achieve a webkit scrollbar that eases in and out. However, instead of the desired effect, the scrollbar appears and disappears instantly. Whether I define the keyframes ...

Tips for maintaining the navigation tab's consistent appearance

Whenever I click a button on the sidebar and the current tab is the second tab, the navigation bar switches to display the first tab. How can I prevent this switch and keep the second tab displayed when clicking a button on the sidebar? The code for this ...

How can I trigger my function in jQuery after inputting into the jQuery text field?

Looking for advice on implementing a function in jQuery for a text field that is not working as expected. The code works fine when creating a text field in HTML, but encounters issues with the jQuery text field. <!DOCTYPE html> <html> <body ...

Gatsby's build process encounters a stumbling block when webpack fails due to issues

Every time I try to run gatsby build, an error pops up: failed Building static HTML for pages - 10.179s ERROR #95313 Building static HTML failed Refer to our documentation page for more details on this issue: https://gatsby.dev/debug-html 343 | ...

Utilizing jQuery to incorporate a radio input function in a POST request

When the Index.php page button is pressed, data is sent to the Resultx.php script page, which then responds with an asynchronous call back on the same Index.php page. index.php <script> $(document).ready(function() { $("#input_form").subm ...

Seeking particular section of online content in an asynchronous manner

Is there a method for asynchronously requesting a specific portion of a web resource (like the initial 100 bytes) using JavaScript? I believed this could be accomplished through XmlHttpRequest by adjusting its Range header. However, if the server utilizes ...

Which particular files should be included such as CSS and JavaScript?

After downloading bootstrap4, I'm unsure which css, js, or other files are necessary to include in order to make my form Bootstrap ready. My current task involves creating a form in CakePHP 3. Click here for more information on how to create a form ...

Reordering sections in a dynamic manner

I'm working on a single-page website with four sections arranged like this: <section id=“4”> <section id=“3”> <section id=“2”> <section id=“1”> Now, I want to change the order of these sections when scrolling ...

Having trouble using jQuery to target the element "(this)"?

Currently, I am delving into jQuery and have stumbled upon a section that perplexes me. Due to my lack of familiarity with the terminology, conducting an effective Google search has proven difficult. Therefore, I decided to craft this question to elucidate ...

Using PHP and an HTML form to insert data into a MySQL database

I'm having an issue with inserting data into a MySQL database using PHP and an HTML form. After hitting submit, I receive a message stating that the item was inserted successfully. However, when I check phpMyAdmin, the table appears to be empty. The ...

Sending form input values to JavaScript

Struggling to design a webpage featuring one text box and a button that, when clicked, sends the value to Javascript for further processing? After spending significant time troubleshooting, I'm unsure of what's causing the issue. Below are both t ...

Ways to prevent an array from being reset

My issue involves the clothes and orders tables, along with an array based on Clothes and Orders models. Whenever I add a clothes element into the Orders array and specifically update the amount and price of the selected item, it also updates the Clothes a ...

material-icons appear differently when letter-spacing is applied in iOS browsers

To display levels using star icons from angular material, I wanted the stars to be shown next to each other. I attempted to achieve this by applying letter-spacing: -0.25rem;, but unfortunately it did not render correctly on iOS platforms (resulting in s ...

Revolutionizing Interaction: Unveiling the Power of Bootstrap 3

Would you be able to assist me in customizing buttons to resemble a typical dropdown link? <div class="dropdown"> <button class="btn dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">Dropdown <span class="caret"&g ...

"Exploring the variations in design between wordpress.com and self-hosted wordpress

I'm currently puzzling over the source of the disparities in style between my Wordpress site hosted elsewhere and my identical theme on Wordpress.com. It's perplexing to see such contrasting appearances while I migrate my blog. After some irrele ...

I'm not sure why, but for some strange reason, Markdown images refuse to display when surrounded by

Recently, I've encountered a strange issue where my markdown image tag doesn't display the image when wrapped in a <div> or a <section>. Instead, it just outputs markdown code. Take a look at this screenshot for reference. <sectio ...

Utilize ng-repeat to iterate over two arrays

Two arrays are at my disposal here. The first array contains option content, while the second array consists of option tags (i.e., A, B, C, D). How can I structure the layout for the 'input type="radio"' using ng-repeat? To start off, I am attem ...