Home ยป Blog ยป How To Make A CSS Text Reflection Effect
Purple text with reflection over green background. Image made by the author.

How To Make A CSS Text Reflection Effect

Hi there! ๐Ÿ˜ƒ In this post, we’ll learn bit by bit how to create a CSS text reflection effect. When we say reflection, we’re referring to a mirror-like effect that looks like the text reflects on a surface, much like the way we see our reflection in a mirror.

This is a simple yet amazing way to enhance the appearance of your text. Letโ€™s analyze our effect so that you can easily follow along.

HTML structure

We will begin with our HTML structure. As you can see below, I prepared a div with the class .reflection-text , this is where our effect will take place.

<div class="reflection-text">HELLO WORLD</div>
CSS

CSS foundation

Letโ€™s move forward with the CSS basic structure. We start with defining the background. Itโ€™s worth noting that using radial-gradient can make our effect more impressive. ๐Ÿ˜ƒ

body {
  background: radial-gradient(lightgreen, darkgreen);
  height: 100vh;
}
CSS

Below, we can see the background we just created.

In web development, we use the flex method in order to center our text. Then we will enhance its appearance, by making some adjustments. We will set the font-size to 100 pixels, select the โ€œRobotoโ€ font-family, and choose white as the text color.

body {
  ...
  display: flex;
  align-items: center;
  justify-content: center;
}

.reflection-text {
  font-size: 100px;
  font-family: 'Roboto', sans-serif;
  color: white;
}
CSS

This is what is rendered on the screen for now. A perfectly centered, ๐ŸŽฏ white text.

Adding the CSS structure for the reflection effect

Creating a reflection can be achieved by using pseudoelements like :before and :after. To ensure this works properly, we need to include position: relative in our .reflection-text class.

.reflection-text {
  ...
  position: relative;
}
CSS

Now, we are ready to proceed and create our reflection by adding the :before pseudoelement with all the necessary properties, as shown in the following code snippet.

.reflection-text:before {
  content: "HELLO WORLD";
  position: absolute;
  top: 65px;
  transform: rotate(180deg) scaleX(-1);
  background: linear-gradient(
    to bottom, 
    rgba(255, 255, 255, 0) 20%, 
    rgba(255, 255, 255, 0.5) 60%, 
    rgba(255, 255, 255, 2) 100%
  );
  background-clip: text;
  color: transparent;
  opacity: 0.3;
}
CSS

Breaking down the process

๐ŸŸข To begin, we add the text โ€œHELLO WORLDโ€ to create another text element with the same content. This new text will serve as our reflection. Then, we set position: absolute so that we can move our reflected text below the original text. We use the top property to move the reflected text 65 pixels from the top, but you can always move the reflection in any direction you prefer. It is important to position the text and its reflection closely together for a more realistic reflection effect. ๐Ÿ˜‰

๐ŸŸข We move forward and use the transform CSS property to rotate the text by 180 degrees

and then flip it horizontally using scaleX(-1). Now we have the perfect reflection! Letโ€™s continue and make it more realistic.

๐ŸŸข In the next step, we will adjust the color of our reflected text. To achieve this, we will utilize the linear-gradient CSS property and specify the direction as downwards. This will create white gradients, with the top appearing more intense and gradually fading towards the bottom of the text.

๐ŸŸข It is commonly known that gradients cannot be directly applied to texts. Donโ€™t worry! ๐Ÿค” We already have the solution to this problem. For now, let’s give a quick explanation. To create a clipped linear background pattern for text, first, we add the -webkit-background-clip: text property, and at the same time, we set the color to transparent, and our text automatically turns to transparent. In that way, our text takes the background: linear-gradient as its real color.

๐ŸŸข For a more transparent text, we can adjust the opacity. The lower the opacity, the more transparent our text becomes.So, here we are! Our reflection is ready! ๐Ÿฅณ

Exploring different colors

๐Ÿ”– It is always an option to use black or any other color in our work. Below, Iโ€™ve included examples of texts with black, purple, and green colors. It’s important to remember that the key factor is to set the correct gradients at the linear-gradient property. That way, we can create respective shades. Therefore, please give extra attention to that! ๐Ÿ˜Š

Black text

.reflection-text {
  position: relative;
  font-size: 100px;
  font-family: 'Roboto', sans-serif;
  color: black; /* default color */
}
CSS
.reflection-text:before {
  content: "HELLO WORLD";
  position: absolute;
  top: 65px;
  transform: rotate(180deg) scaleX(-1);
  background: linear-gradient(to bottom, 
  rgba(0, 0, 0, 0) 20%, 
  rgba(0, 0, 0, 0.5) 60%, 
  rgba(0, 0, 0, 2) 100%);
  background-clip: text;
  color: transparent;
  opacity: 0.3;
}
CSS

Purple Text

.reflection-text {
  position: relative;
  font-size: 100px;
  font-family: 'Roboto', sans-serif;
  color: purple;
}
CSS
.reflection-text:before {
  content: "HELLO WORLD";
  position: absolute;
  top: 65px;
  transform: rotate(180deg) scaleX(-1);
  background: linear-gradient(to bottom, 
  rgba(255, 55, 205, 0) 20%, 
  rgba(255, 55, 205, 0.5) 60%, 
  rgba(255, 55, 205, 2) 100%);
  background-clip: text;
  color: transparent;
  opacity: 0.3;
}
CSS

Green Text

.reflection-text {
  position: relative;
  font-size: 100px;
  font-family: 'Roboto', sans-serif;
  color: green;
}
CSS
.reflection-text2:before {
  content:"HELLO WORLD";
  position: absolute;
  top: 65px;
  transform: rotate(180deg) scaleX(-1);
  background: linear-gradient(to bottom, 
  rgba(20, 150, 20, 0) 20%, 
  rgba(20, 150, 20) 60%, 
  rgba(20, 150, 20, 2) 100%);
  background-clip: text;
  color: transparent;
  opacity: 0.3;
}
CSS

๐ŸŒผ Hope you found my post interesting and helpful. Thanks for being here! ๐ŸŒผ

Leave a reply

Your email address will not be published. Required fields are marked *

3 comments