Home > Web Front-end > CSS Tutorial > Cool! Create responsive stacked card hover effects with CSS

Cool! Create responsive stacked card hover effects with CSS

藏色散人
Release: 2021-09-24 17:54:43
Original
2678 people have browsed it

In the previous article "Using CSS to quickly change the color of PNG images (two methods)", I introduced you how to use CSS to quickly change the color of PNG images. Interested friends can check it out. Look~

This article will introduce you to a cool effect, which is to use CSS to create a responsive stacked card hover effect. What is the effect? Let's continue reading!

Before we start, let me introduce the overall general idea of ​​the implementation: First we will design a simple card structure in HTM; then we will define the ::before and ::after pseudo-elements and relative them to The parent card is positioned absolutely; then the transform attribute is used to move the div of class "card-inner" from its original position; and finally a hover effect is added to a stack of cards by using a transform that can translate the card before and after the hover effect.

The code will be uploaded directly below!

Sample code one:

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        body {
            color: #FDFAF6;
            background: #50CB93;
        }

        .card {
            position: relative;
            width: 400px;
            margin: 60px auto;
        }

        .card::before,
        .card::after {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }

        .card::before,
        .card::after,
        .card .card-inner {
            background-color: #423F3E;
            border: 1px solid #01937C;
            transition: transform 0.5s;
        }

        .card::before,
        .card-inner {
            z-index: 1;
        }

        .card-inner {
            position: relative;
            padding: 4rem;
        }

        /* 将叠好的卡片放在不同的位置 */
        .cards:hover {
            transform: translate(5px, 5px);
        }

        .cards:hover::before {
            transform: translate(-5px, -5px);
        }

        .cards:hover::after {
            transform: translate(-10px, -10px);
        }
    </style>
</head>

<body>
<center>
    <h1>欢迎来到PHP中文网</h1>
    <div class="card-container">
        <div class="card cards">
            <div class="card-inner">
                <h1>将鼠标移至方框内</h1>
                <h3 class="card-title">
                    PHP中文网
                </h3>
                <div class="card-body">
                    编程学习平台
                </div>
            </div>
        </div>
    </div>
</center>
</body>

</html>
Copy after login

The effect is as follows:

GIF 2021-8-31 星期二 下午 3-16-35.gif

Sample code two:

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        body {
            color: #FDFAF6;
            background: #50CB93;
        }

        :root {
            --offset-before: 8px;
            --offset-after: 16px;
        }

        .card {
            position: relative;
            width: 400px;
            margin: 60px auto;
        }

        .card::before,
        .card::after {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }

        .card::before,
        .card::after,
        .card .card-inner {
            background-color: #423F3E;
            border: 1px solid #01937C;
            transition: transform 0.5s;
        }

        .card::before,
        .card-inner {
            z-index: 1;
        }

        .card-inner {
            position: relative;
            padding: 4rem;
        }

    
        .cards-diagonal::before {
            transform: translate(calc(-1 * 8px),
            calc(-1 * 8px));
        }

        .cards-diagonal::after {
            transform: translate(calc(-1 * 16px),
            calc(-1 * 16px));
        }

        .cards-diagonal:hover::before {
            transform: translate(8px, 8px);
        }

        .cards-diagonal:hover::after {
            transform: translate(16px, 16px);
        }


        .cards-rotate::before,
        .cards-rotate::after {
            transform-origin: 50% 100%;
        }

        .cards-rotate:hover {
            transform: translate(2.5px, 0) rotate(2.5deg);
        }

        .cards-rotate:hover::before {
            transform: translate(2.5px, 0) rotate(2.5deg);
        }

        .cards-rotate:hover::after {
            transform: translate(5px, 0) rotate(5deg);
        }
        }
    </style>
</head>

<body>
<center>
    <h1>欢迎来到PHP中文网</h1>
    <div class="card-container">
        <div class="card cards-diagonal">
            <div class="card-inner">
                <h1>将鼠标移至框内</h1>
                <h3 class="card-title">PHP中文网</h3>
                <div class="card-body">
                    编程学习平台
                </div>
            </div>
        </div>
    </div>
    <div class="card-container">
        <div class="card cards-rotate">
            <div class="card-inner">
                <h1>将鼠标移至框内</h1>
                <h3 class="card-title">PHP中文网</h3>
                <div class="card-body">
                    编程学习平台
                </div>
            </div>
        </div>
    </div>
</center>
</body>

</html>
Copy after login

The effect is as follows:

GIF 2021-8-31 星期二 下午 3-20-31.gif

Sample code three:

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        body {
            color: #FDFAF6;
            background: #50CB93;
        }

        :root {
            --offset-before: 8px;
            --offset-after: 16px;
        }

        .card {
            position: relative;
            width: 400px;
            margin: 60px auto;
        }

        .card::before,
        .card::after {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }

        .card::before,
        .card::after,
        .card .card-inner {
            background-color: #423F3E;
            border: 1px solid #01937C;
            transition: transform 0.5s;
        }

        .card::before,
        .card-inner {
            z-index: 1;
        }

        .card-inner {
            position: relative;
            padding: 4rem;
        }

        /*Stacked => Up*/
        .cards-up::before,
        .cards-up::after {
            transform-origin: center bottom;
        }

        .cards-up:hover {
            transform: translate(0, -5px);
        }

        .cards-up:hover::before {
            transform: translate(0, 5px) scale(0.95);
        }

        .cards-up:hover::after {
            transform: translate(0, 10px) scale(0.90);
        }

        /*Stacked => Right */
        .cards-right::before,
        .cards-right::after {
            transform-origin: left center;
        }

        .cards-right:hover {
            transform: translate(5px, 0);
        }

        .cards-right:hover::before {
            transform: translate(-10px, 0) scale(0.95);
        }
        }

        .cards-right:hover::after {
            transform: translate(-10px, 0) scale(0.90);
        }
        }
    </style>
</head>

<body>
<center>
    <h1>欢迎来到PHP中文网</h1>
    <div class="card-container">
        <div class="card cards-up">
            <div class="card-inner">
                <h1>将鼠标移至框内</h1>
                <h3 class="card-title">PHP中文网</h3>
                <div class="card-body">
                    编程学习平台
                </div>
            </div>
        </div>
    </div>
    <div class="card-container">
        <div class="card cards-right">
            <div class="card-inner">
                <h1>将鼠标移至框内</h1>
                <h3 class="card-title">PHP中文网</h3>
                <div class="card-body">
                    编程学习平台
                </div>
            </div>
        </div>
    </div>
</center>
</body>

</html>
Copy after login

The effect is as follows:

GIF 2021-8-31 星期二 下午 3-23-57.gif

PHP Chinese website platform has a lot of video teaching resources. Welcome everyone to learn "css Video Tutorial"!

The above is the detailed content of Cool! Create responsive stacked card hover effects with CSS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
css
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template