DEV Community

Cover image for Frost Effect in CSS
Ustariz Enzo
Ustariz Enzo

Posted on • Edited on

Frost Effect in CSS

Hey fellow creators

How to create a glassomophorism (frost effect) in CSS?

If you prefer to watch the video version, it's right here :

 

1. The HTML structure.

Create a button inside two divs:

<div class="home">
      <div class="container">
        <button>Welcome</button>
      </div>
</div>
Enter fullscreen mode Exit fullscreen mode

 

2. Style your button.

Add an image to the background of the page:

.home {
    background: url("https://images.unsplash.com/photo-1631515998058-69359a50da68?ixlib");
    background-repeat: no-repeat;
    background-attachment: fixed; 
    background-size: cover;
    height: 100vh;
    padding-top: 125px;
}
Enter fullscreen mode Exit fullscreen mode

 
Now style the container that will look frosted:

.container {
    margin: auto;
    width: 450px;
    height: 275px;
    box-shadow: 0 0 10px 0 rgba(0, 0, 0, .4);   
    border-radius: 5px;
    position: relative;
    z-index: 1;
    background: inherit;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}
Enter fullscreen mode Exit fullscreen mode

(The z-index will create a new stacking context).

You do inherit the background of the page at the same position (if you comment it, you’ll see that otherwise you inherit the background as a whole inside of that container, which isn’t what we want!).

 
Next, style the button:

.container button {
    padding: 10px 15px;
    border-radius: 5px;
    border: none;
    cursor: pointer;
    font-family: Lora;
    background: #b6604f;
    color: #f1f1f1;
    font-size: 27px;
    padding: 15px 30px;
    text-transform: uppercase;
    letter-spacing: 2px;
    font-weight: 800;
}
Enter fullscreen mode Exit fullscreen mode

 

3. Add the frost effect!

You need to use the pseudo-element before in order to create that effect:

.container::before {
    content: "";
    position: absolute;
    background: inherit;
    z-index: -1;
    inset: 0;
    filter: blur(10px);
    margin: -20px;
}
Enter fullscreen mode Exit fullscreen mode

The z-index will also create a new stacking context, but since the pseudo-element before is kind of a child of the container, it will always stay at the top of the container.

Bravo, you are done!

 
Come and take a look at my Youtube channel: https://www.youtube.com/c/TheWebSchool

See you soon!

Enzo.

Top comments (1)

Collapse
 
hilgardite profile image
𝐧𝐚𝐭𝐚𝐥𝐢𝐚

Hi, today I found another one of your tutorials, about what I was looking for, cube creation in CSS. I really appreciate your work, so am here to say thanks ^-^.


I see this article is from 2022, so I'd just like to add a tiny update — since 2024 creating this effect got fortunately a bit easier, thanks to a property introduced that year:)).

  • The new property, backdrop-filter, is pretty handy. There is no longer need to define the ::before pseudo-element for the chosen <div> to achieve the frosty/glassy/blurry background effect.
  • Instead, use the backdrop-filter property directly on the <div> used as the container; values accepted in backdrop-filter are the same as in the filter property — they take on the <filter-function> data type.
  • It will work as long as the <div>'s background is (at least partly) transparent, so probably the background property for .container would require changing its value from inherit to transparent or the <div>'s opacity may need to be set for a value lesser than 1.

I guess with the use of backdrop-filter, the style definition for a <div> with the .container class would look like this:

.container {
    margin: auto;
    width: 450px;
    height: 275px;
    box-shadow: 0 0 10px 0 rgba(0, 0, 0, .4);   
    border-radius: 5px;
    position: relative;
    z-index: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: transparent;
    backdrop-filter: filter: blur(10px);
}
Enter fullscreen mode Exit fullscreen mode