Optical illusions are very fascinating when it comes to web development. Using optical illusions in our website engages users because it plays with their minds. It tricks our brains into believing something that doesn't actually exist. These illusions can be created using various techniques in CSS. One of the most commonly used illusions is the image stack illusion, which is simply an illusion of depth. In this article, we’ll discuss the steps involved in creating an image stack illusion using only HTML and CSS. let's start.
What is the image stack illusion?
The image stacking illusion is a visual illusion created by stacking multiple images with varying transparency together. When you view it from a certain angle, all the images blend into each other and create the illusion of a three-dimensional image.
This effect was previously done using Photoshop. However, now we can create one simply using HTML and CSS.
Steps to follow
Create a div element that contains the image. This will be the surface of the first stack.
Style the img element using the border and box-shadow properties.
Specify dimensions for the div element (class="stack1"). Keep the position of the div element relative so that the position of the upcoming pseudo-elements (:before and :after elements) will remain relative to the div element, not relative to the Will keep the position of these pseudo-elements as absolute. Float the div element to the left. Add margins and padding for a better look.
Add the first pseudo-element of the stack using the attribute ":before" . Don't add content to it. Specify its dimensions and give it absolute positioning. Style it using the background color, box shadow, and border properties.
Keep the z-index of the pseudo-element at -1. Give top and left different values to give it a different position and create a different illusion. You can also rotate pseudo-elements to see different effects.
Create a second pseudo-element using the attribute ":after" . Its style is similar to the first pseudo-element. Simply change the top, left and transform values to create the optical illusion. This completes your first stack.
Similarly, you can create as many stacks as you want in a web page. Here we have created 2 stacks in one page.
Example
In this example we create a bunch of images. For the stack, we leave the top, left and transform values as -15px, -15px and rotate(-10deg). >:before pseudo-elements, while 5px, 0 and rotate(10deg) are :after pseudo-elements. Give each pseudo-element a different background color for greater impact.
<html> <head> <style> * { margin: 0; padding: 0; } body { background-color: #B9C8BC; } img { height: 253px; width: 262px; border: 10px solid white; box-shadow: 4px 4px 4px grey; } h1 { text-align: center; text-decoration: underline; font-family: Georgia; } .stack1, .stack2, .stack3 { float: left; position: relative; margin: 65px; padding: 3px; } .stack1:before, .stack1:after { content: ""; border: 10px solid white; position: absolute; z-index: -1; } .stack1 { height: 250px; width: 260px; } .stack1:before { height: 280px; width: 260px; background-color: grey; top: -15px; left: -15px; transform: rotate(-10deg); box-shadow: 4px 2px 4px #9a2ca0; } .stack1:after { height: 250px; width: 260px; background-color: #808000; top: 5px; left: 0; transform: rotate(10deg); box-shadow: 4px 2px 4px #9a2ca0; } </style> </head> <body> <h1 id="Image-Stack-Illusion"> Image Stack Illusion </h1> <div class="stack1"> <img src="/static/imghwm/default1.png" data-src="https://www.tutorialspoint.com/images/physics-tutorials_icon.svg?x-oss-process=image/resize,p_40" class="lazy" alt="How to create the illusion of image stacking using HTML and CSS?" > </div> </body> </html>
Where can we use the image stack illusion in web design?
In web design, the image stack illusion can be used to create a variety of exciting visual layouts. It can be used to create eye-catching image galleries and engaging product advertising pages. Additionally, designers can use it to showcase their portfolios. Industries and large businesses can incorporate this effect to create their landing pages.
in conclusion
The image stack illusion is widely used in web development, advertising and graphic design. The appeal to many users convinced developers to create more such eye-catching visuals. Additionally, we no longer rely on the Photoshop application to achieve this effect. We can easily create it using just HTML and CSS. In this article, we use pseudo-classes (:before and :after) to achieve the desired result. We looked at 6 different illusions in this article. However, there are more possibilities, all you need to do is follow the same steps discussed here to practice and create your own visuals.
The above is the detailed content of How to create the illusion of image stacking using HTML and CSS?. For more information, please follow other related articles on the PHP Chinese website!

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version