CSS - Retain z-index until the transition is complete

Looking at this basic grid layout:

<html>
<head>
    <style>
        .grid {
            display: grid;
            grid-gap: 5px;
            grid-template-columns: repeat(13, 3fr);
        }
        .box {
            position: relative;
            padding-bottom: 100%;
            color: #fff;
            transition: 1000ms;
        }
            .box .square {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
                border: 2px solid;
                box-sizing: border-box;
                background: #101318;
                display: flex;
                text-align: center;
                flex-direction: column;
                justify-content: center;
            }

            .box:hover {
                transform: scale(2);
                z-index: 2;
            }

        .r1 {  grid-row: 20;  }
        .r2 {  grid-row: 21;  }  
        .c1 {  grid-column: 2;  }
        .c2 {  grid-column: 3;  }           

    </style>

</head>
<body>

    <div class="grid">

        <div class="box r1 c1">
            <div class="square">A</div>
        </div>

        <div class="box r2 c1">
            <div class="square">B</div>
        </div>

        <div class="box r1 c2">
            <div class="square">C</div>
        </div>

        <div class="box r2 c2">
            <div class="square">D</div>
        </div>

    </div>

</body>
</html

Whenever I hover over a box in the grid, it enlarges and moves to the forefront. However, upon leaving the box, it shrinks back to its original size and goes back behind other boxes. Is there a way to keep the hovered box always on top until it returns to its normal size, only being overlapped by another currently hovered box? Thanks for any help!

Answer №1

Applying the CSS property z-index: 1; to the elements with the .box class will prevent them from moving to the background.

<html>
<head>
    <style>
        .grid {
            display: grid;
            grid-gap: 5px;
            grid-template-columns: repeat(13, 3fr);
        }
        .box {
            position: relative;
            padding-bottom: 100%;
            color: #fff;
            transition: 1000ms;
            z-index: 1;
        }
            .box .square {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
                border: 2px solid;
                box-sizing: border-box;
                background: #101318;
                display: flex;
                text-align: center;
                flex-direction: column;
                justify-content: center;
            }

            .box:hover {
                transform: scale(2);
                z-index: 2;
            }

        .r1 {  grid-row: 20;  }
        .r2 {  grid-row: 21;  }  
        .c1 {  grid-column: 2;  }
        .c2 {  grid-column: 3;  }           

    </style>

</head>
<body>

    <div class="grid">

        <div class="box r1 c1">
            <div class="square">A</div>
        </div>

        <div class="box r2 c1">
            <div class="square">B</div>
        </div>

        <div class="box r1 c2">
            <div class="square">C</div>
        </div>

        <div class="box r2 c2">
            <div class="square">D</div>
        </div>

    </div>

</body>
</html

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

Add some animation to elements when the page loads

I am curious about triggering a css animation when the page loads. I am currently experimenting with css animations for the first time. Here is what I have created so far: https://jsfiddle.net/7prg793g. However, it only works upon hover at the moment. * ...

What methods are available for updating the href color of an element using the DOM?

I am looking to update the color of a tab (Mathematics-tab) based on the value of 'aria-selected' changing to true in Bootstrap. I have multiple tabs, including one for mathematics, and I want to visually differentiate between selected and unsele ...

Issue with submitting form using Jquery on select action

There seems to be an issue with this jquery code that is supposed to trigger a form submit when a drop down select is changed. It works fine on our test site, but once we move it to the live website, it breaks. Can someone help us troubleshoot and identify ...

Navigate to the location using AngularJS elements in the JavaScript iframe1

Dealing with an issue while working on AngularJS. I am facing a frustrating problem where I am attempting to use one link to open links in two separate iframes. All aspects of this function correctly, except for the actual links. The issue arises when usin ...

Outputting a variable using javascript

My challenge is to efficiently print the contract variable, which contains HTML content fetched from the server. How can I achieve this easily? I want the print window in Chrome to display the document with the contents of my variable when I click a button ...

Ensuring that the second level unordered list is set to a height of 100% of the first unordered list, rather than the

I'm working with this menu http://codepen.io/alexandernst/pen/oxpGYQ (I wanted to include it here but the result viewer on SO is causing some display issues...) and I am trying to ensure that the second and third level ul elements are the same height ...

The jQuery event listener for clicking on elements with the class "dropdown-menu li a" is not functioning properly

I've been attempting to implement a dropdown menu using Bootstrap that displays the selected option when a user clicks on it. However, I'm encountering issues as the breakpoints set within $(document).on("click", ".dropdown-menu li ...

Is it safe to use handlebars' default escaping in HTML attributes?

Currently, I am working on a project where various HTML escaping methods are being utilized. Some properties are escaped in the backend and displayed as raw strings using triple handlebars {{{escaped-in-backend}}}, while others are passed from the backend ...

Error with website navigation menu

I am facing an issue where everything on my website works perfectly, except when I visit /treatments/manage_your_skin.php Once I navigate to that page and try to return to the homepage, it shows an error. This is because my website is searching for /treat ...

Displaying Title and Description Dynamically on Markers in Angular Google Maps

I am currently utilizing Angular-google-maps, and here is the HTML code snippet: <ui-gmap-google-map center='mapData.map.center' zoom='mapData.map.zoom' events="mapEvents"> <ui-gmap-markers models="mapData.map.markers ...

Is it necessary to use `top` or `parent` when utilizing local scripts in an iFrame?

Is it possible to eliminate the use of top or parent in iFrame localized scripts, as the title suggests? Upon examining the screenshot below, I encounter a "not defined" error unless I include top or parent as a prefix to my function call. test(); does n ...

Using CSS to showcase div elements with varying dimensions

How can I arrange divs with varying heights to be positioned close together, regardless of their height, and with a specific margin similar to buildings? I am aiming for a layout like the one on this website . I tried using float:left but it only float ...

Turning an svg tag into a WebElement: a step-by-step guide

<img src="data:image/svg+xml;base64,PHN2ZyBzdG9rZS1kYXNoYXJyYXk9IiIgdmVjdG9yLWVmZmVjdD0iIiB3aWR0aD0iMjIiIGhlaWdodD0iMjIiIHZpc2liaWxpdHk9InZpc2libGUiIHN0cm9rZS13aWRnZXQ9IjEiIGZpbGw9InRyYW5zbGF0ZW4iIHN0cm9rZT0idHJhbnNsYXRlciI+PC91cz48L3N2Zz4="/> Is it ...

Unable to create property within array in model

I am facing an issue while trying to access the name property of an array called Model[] generated from my Model.ts file. When attempting to use it in my app.component, I receive an error stating that the property 'name' does not exist on type Mo ...

What could be the reason for Internet Explorer pulling styles from the media=print CSS?

The HTML below incorporates a DHTML behavior into a CSS class. Unfortunately, Internet Explorer (specifically version 8 in compatibility mode) does not read the top style exclusively; instead, it also recognizes the @media print. <!--[if IE]> < ...

How can I change the background image using CSS instead of HTML?

I initially created a non-responsive website for an existing project. Now I need to convert it into a responsive site. I tried changing the logo image to make it responsive, but it's not working as expected. <td colspan="2" style="background-image ...

Stylish grey container with crisp white lettering

Just getting started with HTML and I've been working on a project for my class. Here's what I've come up with: <font style="background-color: grey; color: white; display: block">Black and white copies $.10 per page letter/legal size&l ...

What could be causing my UI Bootstrap datepicker-popup to suddenly stop functioning?

After updating to UI Bootstrap 0.11.0, I encountered an issue where my datepickers were no longer appearing correctly. To demonstrate this problem, I have created a plunker which can be viewed here. Essentially, the code snippet causing the problem is as f ...

What impact does the use of CSS in emails have on various email clients and reading formats?

As I delve into customizing my very first (woocommerce) email template, I am considering the option of concealing data using CSS display: none. However, a few questions arise regarding the compatibility and support of CSS and display: none: Do all email ...

Navigation bar's active area does not span the full height

I'm facing some issues with the navigation bar on my responsive website. One issue is that the clickable area for the links does not extend to the full height of the nav bar, and I would like it to cover the entire height. Another problem arises whe ...