Animate the top image with CSS to shrink and reveal the sub image underneath

I currently have a set up with two images stacked on top of each other. I am looking to make the top image shrink after a timeout, not disappear completely but become a thumbnail size. As this top image shrinks, the bottom image will be revealed.

As someone with no experience in CSS animation, what would be the best approach for me to learn how to achieve the above effect?

<div class="block">
    <div class="block__content">
        <h3 class="title--s">
            This is the title
        </h3>
    </div>
    <div class="block__image product-sale">
        <img src="/topimage" alt="topimage">
    </div>
</div>  

The product-sale class contains the styling for the thumbnail:

.product-sale {
                &:before {
                content: "text";
                height: 53px;
                padding: 10px;
                background: red;
                color: #fff;
                font-weight: bolder;
                position: absolute;
                z-index: 999;
                display: flex;
                align-items: center;
                justify-content: center;
                left: 30px;
                font-size: 11px;
                }
            }

This is the code for the .block__image class:

.block__image {
            min-width: 140px;
            position: relative;

            img {
                width: 400px;
                height: 200px;
                -o-object-fit: cover;
                object-fit: cover;
                padding: 0px 30px 30px 30px;

            }
}

One idea I have is to add a transition to the pseudo element. You can see an example of transitions on W3Schools.

Answer №1

If you want to add animation to an image using CSS, you can utilize @keyframes and the animation property. While the mention of a thumbnail was unclear, here is some code that demonstrates how you can implement animation:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
            .sub img {
                width: 300px;
                position: absolute;
                animation-name: shrink;
                animation-duration: 4s;
                animation-delay: 5s;
            }
            .top img {
                width: 300px;
                position: absolute;
            }
            @keyframes shrink {
                from {width: 300px;}
                to {width: 50px;}
            }
        </style>
    </head>
    <body>
        <div class="block">
            <div class="block__content">
                <h3 class="title--s">
                this is the title
                </h3>
            </div>
            <div class="top">
                <img src="/mm.jpg" alt="topimage">
            </div>
            <div class="sub">
                <img src="/m.png" alt="subimage">
            </div>
        </div>
    </body>
</html>

Feel free to use this as a reference for adding animations to your images!

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

Displaying images in a horizontal row instead of a vertical column when using ng-repeat

I am currently using ng-repeat to showcase a series of images. While I believe this may be related to a CSS matter, I am uncertain. <div ng-repeat="hex in hexRow track by hex.id"> <img ng-src="{{hex.img}}"/> </div> https://i.sstatic ...

Capture any clicks that fall outside of the specified set

I am facing an issue with my navigation drop down menu. Currently, the pure CSS functionality requires users to click the link again to close the sub-menu. What I want is for the sub-menu to close whenever a click occurs outside of it. I attempted a solu ...

Highcharts 3D Pie Chart with Drilldown Feature

How can I create a 3D Pie Chart with Drilldown effect? I am having trouble understanding how it works. Here is a JsFiddle Demo for a 3D Pie Chart: JsFiddle Demo And here is a JsFiddle Demo for a 2D Pie Chart with Drilldown feature: JsFiddle Demo You can ...

In Google Chrome, the :after element on the left is cleared, while other browsers do not

In an effort to create a button using background images in the :before and :after pseudo-selectors for cross-browser compatibility, an issue arose where only Chrome did not display it correctly (Safari results were unknown). HTML: <div class="btn-cont ...

What's the best way to make two buttons appear side by side on a webpage?

I need to have my two buttons, Update and Cancel, displayed next to each other on the same line. Currently, there is a gap between the two buttons as shown in the attached image. Below is the HTML code I am using: <div class="form_block span2"> ...

Display one div and conceal all others

Currently, I am implementing a toggle effect using fadeIn and fadeOut methods upon clicking a button. The JavaScript function I have created for this purpose is as follows: function showHide(divId){ if (document.getElementById(divID).style.display == ...

Broken image path in the Model-View-Controller framework

When using the jQuery DatePicker plugin in my MVC application, I face an issue with displaying a certain image for the Calendar pop-up. Here is the code snippet: $.datepicker.setDefaults({ showOn: "both", buttonImage: "../images/Calendar.png", ...

Guide on incorporating a glyphicon into a dropdown menu link by leveraging the link_to helper in Ruby on Rails

Just getting started here and I have a question... I want to include glyphicons on the links in a dropdown menu using the link_to helper in my Ruby on Rails app. This is the code I currently have: <ul class="nav_item pull-right"> <li cla ...

Personalize the width of the columns

My SharePoint Online HTML code is as follows: <div sortable="" sortdisable="" filterdisable="" filterable="" filterdisablemessage="" name="Responsable" ctxnum="14" displayname="Responsable" fieldtype="User" ...

The table is too wide for its parent mat-nav-list, stretching to 100%

Here is the code snippet I am using to display a table inside a mat-nav-list: <mat-nav-list> <table> <tr>Node Information</tr> <tr> <td>Name</td> <td *ngIf="activeNod ...

The Layout of Bootstrap4 Form is Displaying Incorrectly

I'm attempting to create a basic email form similar to the one shown in the Bootstrap 4 documentation. However, I am facing an issue where the formatting is not displaying correctly. Here is what the example should look like: https://i.sstatic.net/qx ...

Selenium was unable to find the p tag element on the webpage

I apologize for reaching out to you all for assistance with such a basic task, but I have tried everything in my toolkit and still can't seem to figure it out. I've experimented with partial xpath, full xpath, and various CSS selectors. I am see ...

Issues with incorrect sizing in Safari when using rem units alongside animations

UPDATE: After further testing, it appears that the code functions correctly within the SO snippet view. To replicate the issue, please copy the code and save it locally. UPDATE 2: Odd behavior detected. Refer to this gist and video for more information. ...

Having trouble with animating the background color of an input button using jQuery?

I'm completely confused as to why the background color isn't changing despite investing a significant amount of time into it: <input type="button" class="front-consultation-btn" value="Get A Free Consultation"> <script> $(' ...

Maintaining Scene Integrity in THREE.JS: Tips for Handling Window Resizing

My layout includes a div with a scene that looks great initially; however, as soon as I start moving or resizing the window, the scene overflows the boundaries of the div and goes haywire, almost filling the entire window. Are there any techniques or solu ...

CrossBrowser - Obtain CSS color information

I'm attempting to retrieve the background color of an element: var bgcolor = $('.myclass').first().css('background-color') and then convert it to hex function rgbhex(color) { return "#" + $.map(color.match(/\b(\d+ ...

What is the best way to incorporate a background image that complements my content?

I'm currently working on revamping my friend's windsurf club website. The issue I'm facing is with trying to achieve a more elegant appearance by positioning a background image behind the <h1> and <h2> elements. Currently, I have ...

Position to the left with Flexbox

Currently utilizing Flex to showcase checkboxes, I have successfully met the requirements for both large and small resolutions. However, for medium resolution, the checkboxes are not functioning as anticipated. I am attempting to align all the boxes to flo ...

What exceeds the size of the HTML tag?

I noticed that the height of my section with id "the-view-port" is set to 100vh in my page like this: <html> <body> <section id="the-view-port"> However, I am experiencing an issue in Chrome (Version 73.0.3683.86) where there seem ...

Ways to postpone CSS and Script loading once JavaScript has been executed

My issue with my backbone.js app is that I have noticed some slow loading files (a .css and a .js) that are causing the page to block until they are fully loaded. After rendering the backbone view, I am looking for a way to delay the loading of these file ...