Overlay cards on each other

Donald Symmons 2,861 Reputation points
2023-01-15T10:26:09.24+00:00

Hello forum,

Please how can a situation where I have about 4 cards that overlay each other on a webpage, just as shown in the image below?

When the cursor hovers on any card, the card will be brought forward and other cards go behind.

Here is what I tried


        .mycards {
            justify-content: center;
            text-align: center;
            align-items: center;
            height: 250px;
            width: 500px;
            background-color: rgb(36, 168, 36);
            font-size: 20px;
        }
 
        .card1 {
            margin-top: 80px;
            color: white;
            font-size: 40px;
            font-weight: bold;
            background-color: #909090;
            border: 0.5px solid #3b3b3b;
            border-radius: 7px;
        }
        .card2 {
             border-radius: 7px;
        }
 
        .card1:hover {
            z-index: -1;
            opacity: 0.5;
            font-size: 20px;
            text-align: center;
            transform-style: all;
            transition-duration: 1s;
        }
 
        .card2:hover {
            z-index: -1;
            font-size: 40px;
            text-align: center;
            transform-style: all;
            transition-duration: 1s;
        }
 
<div class="row col-sm-11" style="width: 100%; margin: 0 auto; margin-top: 8%; margin-bottom: 4%;">
                    <div class="mycards col-sm-5">
                        <div class="">
                            <div class="card1" style="background-color: #0b2436;">1st Card</div>
                        </div>
                        <div class="">
                            <div class="card2" style="background-color: #145c7c;">2nd Card</div>
                        </div>
                    </div>
                    <div class="col-sm-5" style="margin: 0 auto;"></div>
                </div>

overlay card

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,465 questions
0 comments No comments
{count} votes

Accepted answer
  1. QiYou-MSFT 4,321 Reputation points Microsoft Vendor
    2023-01-17T09:53:37.1433333+00:00

    Hi @Donald Symmons

    My idea is to combine the box-shadow property and the transform property. Then we set 4 cards and different displacements according to the situation.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        
    </head>
    <style type="text/css">
        
        .card {
            padding: 10px;
            width: 300px;
            height: 180px;
            background-color: #FFF;
            border: none;
            border-radius: 6px;
            -webkit-transition: all 250ms cubic-bezier(0.02, 0.01, 0.47, 1);
            transition: all 250ms cubic-bezier(.02, .01, .47, 1);
        }
            .card:hover {
                box-shadow: 0 16px 32px 0 rgba(48, 55, 66, 0.15);
                transform: translate(0,-100px);
                transition-delay: 0s !important;
            }
        .box-shadow {
            -webkit-box-shadow: 0 0.25rem 1rem rgba(48, 55, 66, 0.15);
            box-shadow: 0 4px 16px rgba(48, 55, 66, 0.15);
        }
        
        .card-header {
            text-align: center;
        }
        .card-body, .card-footer {
            text-align: left;
        }
    </style>
    <body style="background: #e3e3e3;">
        <div class="card box-shadow">
            <div class="card-header">
                <p>This is a card</p>
            </div>
            
        </div>
    </body>
    </html>
    

    Card

    Best Regards

    Qi You

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Tasadduq Burney 8,421 Reputation points MVP
    2023-01-15T12:20:13.5466667+00:00

    Based on the code you've provided, you are trying to change the z-index and opacity of the cards when they are hovered over. However, this alone will not achieve the desired effect of bringing the hovered card forward and pushing the other cards behind it.

    One way to achieve this effect is to use the CSS position property, and set it to relative for all cards and absolute for the hovered card. This way, the hovered card will be positioned on top of the other cards and will appear to be in front of them.

    Here is an example of how you can modify your code to achieve the desired effect:

    .card {
        position: relative; /* all cards are positioned relatively */
        border-radius: 7px;
    }
    .card:hover {
        position: absolute; /* hovered card is positioned absolutely */
        z-index: 1; /* brings the hovered card to the top */
        font-size: 40px;
        text-align: center;
        transform-style: all;
        transition-duration: 1s;
    }
    
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.