Example code for implementing photo stacking effect with CSS

Example code for implementing photo stacking effect with CSS

Achieve results

 

step

1. Initial index.html

To build up the first photo, the one at the top. We just need to add a div that contains the img of the photo. That’s all, the rest of the effects are achieved through CSS. Make sure the div has the class stackone.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Photo Stack</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    html,
    body {
      width: 100%;
      height: 100%;
      overflow: hidden;
    }
    .stackone {
      --img-width: 480px;
      --img-height: 320px;
      border: 6px solid #fff;
      float: left;
      height:var(--img-height);
      width: var(--img-width);
      margin: 50px;
      position: relative;
      -webkit-box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
      -moz-box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
      box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
    }
    .stackone img {
      width: var(--img-width);
    }
  </style>
</head>
<body>
  <div class="stackone">
    <img src="../../../assets/image/landscape-4378548_960_720.jpg">
  </div>
</body>
</html>

The initial effect is as follows:


2. The First Pseudo Element

Now we add a layer of negative. The effect we want is that the bottom image appears to be under the top photo. We can use the CSS pseudo-class: before to achieve this.

.stackone::before {
  content: "";
  height:var(--img-height);
  width: var(--img-width);
  background: #eff4de;
  border: 6px solid #fff;
  -webkit-box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
  -moz-box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
  box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
}

The effect is far different now


3. Improve before

This is not the effect we want. How to fix it? We need to add some positioning to the :before and then set the z-index to put it behind.

.stackone::before {
  content: "";
  height:var(--img-height);
  width: var(--img-width);
  background: #eff4de;
  border: 6px solid #fff;

  position: absolute;
  z-index: -1;
  top: 0px;
  left: -10px;

  -webkit-box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
  -moz-box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
  box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);

  -webkit-transform: rotate(-5deg);
  -moz-transform:rotate(-5deg);
  -o-transform: rotate(-5deg);
  -ms-transform:rotate(-5deg);
  transform: rotate(-5deg);
}

The effect is normal at this time, and the initial signs are seen


4. The Second Pseudo Element

.stackone::after {
  content: "";
  height:var(--img-height);
  width: var(--img-width);
  background: lightblue;
  border: 6px solid #fff;
  position: absolute;
  z-index: -1;
  top: 5px;
  left: 0px;
  -webkit-box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
  -moz-box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
  box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
  -webkit-transform: rotate(4deg);
  -moz-transform:rotate(4deg);
  -o-transform: rotate(4deg);
  -ms-transform:rotate(4deg);
  transform: rotate(4deg);
}

Finally, it's done, with a sense of hierarchy:


Original description

This article is migrated from my blog "CSS to achieve photo stacking effect" on CSDN at 16:38:48 on December 29, 2014
Source blog reference translated from "Use Pseudo Elements to Create an Image Stack Illusion"
Github project source code

<<:  Discuss the development trend of Baidu Encyclopedia UI

>>:  Detailed explanation of Nginx process management and reloading principles

Recommend

Implementation of docker-compose deployment project based on MySQL8

1. First, create the corresponding folder accordi...

Javascript to achieve the drag effect of the login box

This article shares the specific code of Javascri...

Solution to garbled display of Linux SecureCRT

Let's take a look at the situation where Secu...

Analysis of the differences between Iframe and FRAME

1. Use of Iframe tag <br />When it comes to ...

Sample code for implementing PC resolution adaptation in Vue

Table of contents plan Install Dependencies Intro...

Analysis of the principles and usage of Docker container data volumes

What is a container data volume If the data is in...

Summary of 10 amazing tricks of Element-UI

Table of contents el-scrollbar scroll bar el-uplo...

Toolkit: A more powerful front-end framework than Bootstrap

Note: Currently, the more popular front-end frame...

Using System.Drawing.Common in Linux/Docker

Preface After the project is migrated to .net cor...

Vue calls the PC camera to realize the photo function

This article example shares the specific code of ...

HTML embedded in WMP compatible with Chrome and IE detailed introduction

In fact, there are many corresponding writing met...

js dynamically adds example code for a list of circled numbers

1. Add the ul tag in the body first <!-- Unord...

Vue project implements file download progress bar function

There are two common ways to download files in da...

A brief analysis of the responsiveness principle and differences of Vue2.0/3.0

Preface Since vue3.0 was officially launched, man...