How To Make CSS Reflection – The Easy Way

Marilena

By Marilena

3 min read
Bookmark this post

Hello there! Today, we will explore a simple yet valuable topic. How do you create a CSS reflection using only one property? Let’s unlock the amazing –webkit-box-reflect property and effortlessly craft repeatable elements with minimal 😉 code! 🥳

Simple CSS Reflection

We will start by creating two identical boxes. We set 200 pixels width and 100 pixels height and also add a solid black 2 pixel border. Continuing, we give our boxes an indigo background-color.

<div class="box box--adjacent"></div>
<div class="box box--spaced"></div>
HTML

Next, we proceed with our -webkit-box-reflect where we create the reflection of each box, ensuring that their direction remains the same (right direction). However, the second reflection is located 150 pixels from the original box. That way, we can explore both direction and distance.

.box {
  width: 200px;
  height: 100px;
  border: 2px solid black;
  background-color: indigo;
  &--adjacent {
    -webkit-box-reflect: right;
  }
  &--spaced {
    -webkit-box-reflect: right 150px;
  }
}
SCSS

In the images provided, we can see two boxes along with their corresponding reflections. In the first image, the box’s reflection is located exactly on the right-hand side, adjacent to the original box, while in the second image, the box also has a reflection on the right-hand side but is positioned 150 pixels away from the original box.

CSS reflection. An image showing a box reflection on the right-hand side of the origin box.
-webkit-box-reflect: right
CSS reflection. An image showing a box reflection on the right-hand side positioned 150 pixels away from the original box.
-webkit-box-reflect: right 150px

CSS Reflection With Fade-Out Effect

It is essential to know that we can use the -webkit-box-reflect CSS property to create a reflection effect that accurately mirrors the original element’s appearance and style. We can further improve this effect and make it seem more authentic by using the linear-gradient CSS property, which gives us the impression of a gradual fade-out. This technique is pretty cool! 😎

We will start with our HTML code snippet, creating again two boxes with identical characteristics. We set 200 pixels width and 100 pixels height. Next, we give both boxes an indigo background-color.

<div class="box box--top"></div>
<div class="box box--bottom"></div>
HTML

Next, we proceed with our -webkit-box-reflect where we create the reflections of each box, ensuring that they have the same direction (below). However, the second reflection has a linear-gradient that creates a fade-out effect from top to bottom.

.box {
  width: 200px;
  height: 100px;
  background-color: indigo;
  &--simple-reflection {
    -webkit-box-reflect: below 10px;
  }
  &--fade-out-reflection {
    -webkit-box-reflect: below 10px
    linear-gradient(
      rgba(0,0,0,0.0), 
      rgba(0,0,0,0.2),
      rgba(0,0,0,0.4),
      rgba(0,0,0,0.6),
      rgba(0,0,0,0.8)
    );
  }
}
SCSS

When adjusting the reflection vertically, we need to use above and below instead of top and bottom.

Below, you can observe two boxes along with their related reflections. In the first image, the box’s reflection is located 10 pixels above the original box. In the second image, the reflection stands again 10 pixels above the origin box, but this time has the fade-out effect. It looks nice, doesn’t it?

CSS reflection. An image showing a box reflection 10 pixels below the original box.
-webkit-box-reflect: below 10px
CSS reflection. An image showing a box reflection positioned 10 pixels below the original box. It has a linear-gradient(
      rgba(0,0,0,0.0), 
        rgba(0,0,0,0.2),
        rgba(0,0,0,0.4),
        rgba(0,0,0,0.6),
        rgba(0,0,0,0.8)) that creates the fade-out effect.
-webkit-box-reflect: below 10px linear-gradient(rgba(…))

If you are interested in diving deeper, there is an amazing post about CSS text reflection that you might enjoy reading!

🌼 Hope you found my post interesting and helpful. Thanks for being here! 🌼

DigitalOcean Referral Badge
guest
0 Comments
Inline Feedbacks
View all comments

Continue reading

Front, Programming

Learn How to Select Only the CSS First Child

Front, Programming

The CSS nth-of-type Selector – How To Target Elements By Type

Front, Programming

What You Need to Know About CSS nth child Selector – A Practical Guide

Front, Programming

CSS Selectors nth-child VS nth-of-type And How To Use Them Correctly

Front, Programming

Coding Made Easy With CSS Selectors: An Ultimate Guide

Front, Programming

How to Center in CSS Using Different Ways

Front, Programming

What You Need to Know About CSS Color Methods

Programming

Most Common Typescript Errors and How to Solve Them

Front, Programming

Make Your Work Easier by Using Variables in CSS

Front, Programming

CSS Rainbow Text: How To Make Astonishing And Vibrant Fonts

Front, Programming

Most Useful HTML Elements for Maximizing Better Results

Back, Programming

Environment Variables 101: Secure Your Secrets Like a Pro

Subscribe to our newsletter

Dive into the Fun Side of Tech! Posts, News, and More Delivered to Your Inbox!

Intuit Mailchimp