Navigate back to the homepage
AboutWeb-DevelopmentSoftware-DevSelf-Improvement
x
x
x
x
x
x
x

Frosted glass effect using CSS

Divyansh Tripathi
September 23rd, 2021 · 2 min read

In this blog I’m going to show you how you can create a frosted glass panel for your website or side project using just CSS. We will be learning this method in 2 ways.

  • css backdrop-filter property
  • inset drop-shadows You will be learning 2 hopefully new CSS concepts along the way so let’s get started.

First things First

Get the template ready. Just make a div as follows.

1<div class="glass frosted">
2 <h1>Frosted Glass</h1>
3</div>

Also remember to give your background a cool image wallpaper to notice the glass effect. Or you can always refer to the demo below ;)

Let’s get to business

Method 1

For this we will be using css backdrop-filter property. We will add the following css to the frosting class. The backdrop-filter CSS property lets you apply graphical effects such as blurring or color shifting to the area behind an element. Which means it is filter but for background.

So one of the filter in css we have is blur which takes the parameter in pixels relating to the intensity of the blur.

1.frosted_1 {
2 /* Frosted glass affect */
3 -webkit-backdrop-filter: blur(8px);
4 backdrop-filter: blur(8px);
5}

Note - The extra css with -webkit- appended is for Safari and similar browsers which use webkit engine.

So how does this look? Something like this -

g1

We can clearly see the frosty blur effect.

But something is still lacking. There is not depth among the elements so it doesn’t exactly look like a seperate surface or in our case a glass.

For that we will use another css property that is box-shadow. The box-shadowCSS property adds shadow effects around an element’s frame. So exactly how its named. Also while we are at it we will also give the element some extra background color to make it look more realistic as a glass.

1.frosted_2 {
2 /* Frosted glass affect */
3 -webkit-backdrop-filter: blur(8px);
4 backdrop-filter: blur(8px);
5
6 /* Add box-shadow for more depth */
7 box-shadow: 0px 10px 15px 10px rgba(0 ,0 ,0 ,0.15);
8 background-color: rgba(228 ,228 ,228 ,0.10);
9}

After this change our glass panel will look more realistic and way cooler. As shown below -

g2

Method 2

This method is a little more complex than the above one but it is more widely supported in even older browsers too. So this is the tradeoff you always have to make.

Let’s begin. So instead of backdrop-filter we will be using filter and we will be using drop-shadow filter of css. And the given filter does exactly what the name suggests so nothing to explain here. Let’s see the code and the result.

1.frosted_3 {
2 filter: drop-shadow(2px 4px 6px black);
3 background-color: rgba(152, 151, 151, 0.2);
4}

Oh and also we added some background color with opacity set to 20% just to emulate the blur effect of method 1. The result will look something like this -

g3

Last trick up my sleeve

We are not yet done with method 2. There is another css property known as box-shadow. Which we also used above in method 1. There isa different way of using this property as seen till now. And it is to use inset box-shadow. Which will mean the shadow will not cast outside the element but only inside itself. Creating a depth effect.

So our final CSS for method 2 will end up looking like this.

1.frosted_3 {
2 filter: drop-shadow(2px 4px 6px black);
3 background-color: rgba(152, 151, 151, 0.2);
4 box-shadow: inset 0 0 0 200px rgb(255, 255, 255, 0.08);
5}

And the result which will be a huge improvement over the last one of method 2 would look like this -

g3

So that is it folks. That is all we have for this tutorial if you want I have setup all this code in a github repo for you. Over here -

https://github.com/silent-lad/frosted_glass_tutorial

Join my email list and get notified about new content

I promise I won't spam or unnecesarily promotional emails through this mailing list. You might also get chance to be eligible for my monthly giveaways. Also I promise to not spam your inbox or share your email with any third parties.

More articles from Silentlad

Looks like develop for this site is already running, can you visit http://localhost:8000 ? If it is not, try again in five seconds

Looks like develop for this site is already running, can you visit http://localhost:8000 ? If it is not, try again in five seconds!, Solve this Gatsby error.

September 19th, 2021 · 1 min read

Show your work

Show your work

September 19th, 2021 · 1 min read
xx
© 2018–2023 SilentladSitemapRSS feed
Link to $https://twitter.com/silent_lad_Link to $https://medium.com/@silentladLink to $https://github.com/silent-ladLink to $https://www.instagram.com/silent_lad_/Link to $https://www.linkedin.com/in/silentlad/Link to $mailto:silentlad29@gmail.com