In this article, we will create an illusion of images appearing beneath the main image, a visually appealing effect using just HTML and CSS. This guide walks you through a straightforward approach to achieving this using basic web technologies, making it accessible to developers of any skill level.
Overview
The main objective of this article is to demonstrate how to create a simple illusion where images seem to extend below the main image, creating a layered visual effect. The task can be easily accomplished using only HTML and CSS, without requiring any JavaScript or other advanced tools.
Approach
To create the effect, we will start by defining a main div container in the HTML, which holds both the primary content and the illusionary elements. We will add a heading using an HTML <h1> tag, and then use CSS to apply styling and create the desired visual effect of depth and layers beneath the main image. By applying specific CSS selectors, we can easily manipulate the presentation of elements and add styling to create the illusion.
Step-by-Step Guide
HTML Structure
- Create an HTML File: Start by creating a new HTML file (e.g.,
index.html). This file will form the foundation of our webpage. - Add a Title to the Webpage: In the
<head>section of the HTML file, use the<title>tag to provide a title for your webpage. This title will appear in the browser tab when the page is opened.
<head>
<title>Image Illusion Effect</title>
</head>
Link the CSS File: After setting the title, link an external CSS file (e.g., style.css) in the <head> section to provide styling and animations for the illusion effect. This CSS file will be responsible for the visual appearance and effects applied to the HTML elements.
<head>
<link rel="stylesheet" href="style.css">
</head>
Body Section Setup: Move to the <body> section of the HTML, where the content of the page is structured. Begin by adding a heading to your page using an <h1> tag. This will be the main title that users see on the page.
<body>
<div class="container">
<h1>Main Image with Illusion Effect</h1>
<div class="image-container">
<img src="main-image.jpg" alt="Main Image">
</div>
</div>
</body>
Add Span Elements for Illusion: Inside the heading, or after it, you can create multiple <span> elements to store data or represent the images that create the illusion of depth. In this example, we’ll use these <span> tags to create a perspective text effect or display additional layers of images.
<body>
<div class="container">
<h1>Main Image with Illusion Effect</h1>
<div class="image-container">
<img src="main-image.jpg" alt="Main Image">
<span class="illusion-layer layer1"></span>
<span class="illusion-layer layer2"></span>
<span class="illusion-layer layer3"></span>
</div>
</div>
</body>
CSS Styling
Now that we have the HTML structure in place, it’s time to add the CSS to bring the illusion effect to life.
- Basic Styling for the Page: Set some general styles for the
bodyand the maincontainerto position the content in the center of the page.
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
text-align: center;
margin: 0;
padding: 0;
}
.container {
width: 80%;
margin: 0 auto;
padding-top: 50px;
}
Styling the Main Image: Apply styles to the main image so that it stands out as the focal point of the effect. Use relative positioning to position the image within its container.
.image-container {
position: relative;
display: inline-block;
}
.image-container img {
width: 300px;
height: auto;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
}
Creating the Illusion Layers: Use the span elements to represent the images or shapes that appear beneath the main image, creating the illusion. These layers are styled using absolute positioning, different colors, and opacity to create a depth effect.
.illusion-layer {
position: absolute;
width: 300px;
height: 200px;
background-color: rgba(0, 0, 0, 0.1);
top: 0;
left: 0;
z-index: -1;
transform: translateY(20px) rotate(-2deg);
transition: transform 0.3s ease;
}
.illusion-layer.layer1 {
background-color: rgba(0, 255, 0, 0.3);
transform: translateY(40px) rotate(-5deg);
}
.illusion-layer.layer2 {
background-color: rgba(0, 0, 255, 0.3);
transform: translateY(60px) rotate(-8deg);
}
.illusion-layer.layer3 {
background-color: rgba(255, 0, 0, 0.3);
transform: translateY(80px) rotate(-10deg);
}
.image-container:hover .illusion-layer {
transform: translateY(0) rotate(0deg);
}
Let see an example along with the output:
Html:
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet"
href="sp.css">
</head>
<body>
<h1>
<span>Codemagnet</span>
<span>Your</span>
<span>Coding Resource</span>
</h1>
</body>
</html>
CSS:
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
height: 100vh;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
background: #000;
color: teal;
}
h1{
width: 3em;
height: 1em;
font-size: 6em;
position: relative;
white-space: nowrap;
color: transparent;
}
h1 span{
position: absolute;
background-color: rgb(7, 138, 138);
color: #000;
}
span:nth-child(odd){
transform: skew(50deg,-20deg) scaleY(0.75);
}
span:nth-child(even){
transform: skew(-5deg,-20deg);
}
span:nth-child(1){
bottom: 1em;
left: 0.5em;
}
span:nth-child(2){
top: 0.5em;
left: 2em;
}
span:nth-child(3){
top: 2em;
left: 2.5em;
}
Output:

In this tutorial, we have explored how to master the art of creating stunning perspective text effects using HTML and CSS. These effects can transform ordinary text into visually captivating elements that enhance the user experience on a webpage. By understanding the core concepts of 3D transformations, perspective, and CSS properties, developers can create dynamic and engaging text designs that give depth and dimension to their websites.
We began by breaking down the key CSS properties, such as transform, perspective, rotate, and translate, and how they work together to create a 3D illusion. The perspective property is especially important, as it sets the viewer’s point of view and defines how the 3D transformation is perceived. This property allows you to simulate depth, making the text appear as though it’s receding into the background or moving closer to the viewer.
By combining rotateX, rotateY, and rotateZ transformations, we learned how to manipulate the text along the x, y, and z axes, creating different angles and rotations that contribute to the 3D effect. We also explored how adjusting the perspective-origin can change the focal point of the perspective, giving you full control over how the text is viewed. With these properties, the possibilities for creative text styling are virtually endless.
In addition, we examined how applying CSS transitions and hover effects can add interactivity to the perspective text. This allows users to engage with the text in a more dynamic way, making your webpage not only visually appealing but also more engaging. These techniques can be used in various scenarios, from highlighting key information on landing pages to enhancing titles and headings in blogs or marketing sites.
One of the significant advantages of using perspective text effects is that they are purely CSS-based, meaning no JavaScript is required. This ensures that the effects are lightweight and easy to implement without affecting the overall performance of the website. Moreover, because these effects are responsive by nature, they can be adapted for different screen sizes, ensuring that your design remains consistent across devices.
Ultimately, mastering perspective text effects in HTML and CSS offers an excellent opportunity for developers and designers to elevate their web designs. Whether you’re building a personal portfolio, creating a professional website, or working on a creative project, these effects provide a unique way to stand out from the crowd. With the flexibility and control CSS provides, you can experiment with various perspectives, rotations, and animations to achieve the perfect look for your text.
In conclusion, perspective text effects are a powerful tool in modern web design. By leveraging CSS transformations and perspectives, you can create depth and dimension that brings your text to life. Now that you have the knowledge and techniques to create these effects, the only limit is your imagination. So, dive into your next project with confidence, and let your text make a lasting impression on your audience.





Leave a Reply