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

Get rid of the box-shadow on the Vuetify element

I currently have a special-table component that includes a box shadow when the row is expanded https://i.stack.imgur.com/8zgjp.png I am aiming for the removal of the box-shadow effect. After inspecting the console-style tab, I identified https://i.stac ...

Finding strikeout text within <s> or <del> tags can be identified by closely examining the HTML codes

The issue arises with the text that reads as follows: 316.6.1 Structures. Structures shall not be constructed This is represented in HTML as: <b> <s> <span style='font-size:10.0pt'>316.6.1 Structures</span> ...

Understanding JavaScript Regular Expressions

To ensure that no HTML tags are entered into a textarea, I am utilizing the following regex for validation. If any HTML tags are detected in the textarea, I need to display a validation message. The regex being used is: /<(\w+)((?:\s+\w ...

What is the best method to retrieve a global variable from two separate PHP files?

Within file1.php, there is a variable: global $name; $name = ""; In the same directory, within file2.php, there is: <label> <span>Username:</span> <input id="name" type="text" name="username" value ="<?php echo $GLOBALS[& ...

Show the checked options retrieved from controller

I am facing an issue with displaying certain checkbox values from the controller. In order to properly save these values, I believe it would be best to declare them in the controller and then use them in the HTML. However, my current code is not successful ...

Use jQuery to trigger a click event when an element is in focus, unless it was clicked to

I am currently developing a website using the MDL framework. One issue I encountered is that there is no default select form element provided. After some research, I found a solution by utilizing a menu component that displays when the input is clicked. Th ...

Hamburger Menu in Bootstrap not functioning as expected

Despite following each step meticulously for implementing the Hamburger menu in the navbar, I encountered an issue. The navbar collapses when resizing the window but fails to open upon clicking it. Below is a sample code snippet for reference. It utilizes ...

Space in the middle of a body and a div

After extensively searching for a solution to the issue plaguing my website, I found that there was always an annoying gap at the top of the page. Despite trying various solutions, none seemed to work. Upon inspecting the website in Firefox, I discovered t ...

Discover the Magic of Jquery Hover Effect Unleashed

$('.outerBox').hover( function() { $(this) > $('.innerBox').stop(); $(this) > $('.innerBox').slideDown(750); }, function() { $(this) > $('.innerBox').stop(); $(this) > $(&apos ...

Show off markdown formatting without the need for v-html

I run a unique type of platform that allows users to publish their own blog posts using a WYSIWYG editor, and then share them on the site. My top priority is to ensure security against XSS attacks, which is why I am looking for alternatives to using v-htm ...

Reveal each element individually upon clicking the button

I am trying to display 5 div elements one by one when clicking a button, but the current code is not working. I am open to alternative solutions for achieving this. Additionally, I want to change the display property from none to flex in my div element. ...

Whenever I select a link on a navigation bar, it transports me to the desired section of the page. However, I often find that the navbar ends up

Recently, I came across some website templates where clicking on a link in the navbar smoothly scrolls to the corresponding section with perfect alignment. The content at the top of the page aligns perfectly with the top of each division. Upon attempting ...

IE6 is not displaying the tag styles correctly

My website has a section that shows two links next to each other. While it's displaying correctly on most browsers, it seems to have issues specifically in IE6. You can view the issue on this fiddle. Can anyone shed some light on why this is happenin ...

Conceal the navbar during the process of the ajax loader loading

My current issue involves utilizing an AJAX loader to conceal my page until all elements have loaded. Unfortunately, the navbar is not hidden along with the other content as shown in the image below. I need assistance in ensuring that everything, including ...

Is it possible to modify the background-image using Typescript within an Angular project?

I have been struggling to change the background image in my Angular project using Typescript. I attempted to make the change directly in my HTML file because I am unsure how to do it in the CSS. Here is the line of code in my HTML file: <div style="ba ...

Streamlining programming by utilizing localStorage

Is there a more efficient way to streamline this process without hard-coding the entire structure? While attempting to store user inputs into localStorage with a for loop in my JavaScript, I encountered an error message: CreateEvent.js:72 Uncaught TypeErr ...

Find elements that are not contained within an element with a specific class

Imagine having this HTML snippet: <div class="test"> <div class="class1"> <input type="text" data-required="true"/> </div> <input type="text" data-required="true"/> </div> I'm looking to select ...

Preventing ReactJS tooltips from exceeding the boundaries of the screen

Here is a simple demo showcasing blocks with tooltips that appear when hovered over. However, there seems to be an issue with the functionality. The tooltip should ideally be displayed either from the left or right side of the block. To determine the size ...

Is there a way to send me the result of the radio input (Yes/No) via email?

Is it feasible to send the results of a radio input (Yes/No) back to my email directly using HTML, CSS, and Javascript? ...

Modifying the Position of the Search Box within DataTables by Manipulating DOM Elements

As a newcomer to the jQuery datatables plugin, I am still learning how to use it effectively. I have connected the plugin to my tables using the following code: $(document).ready(function() $('#table_id').dataTable({ }); }); ...