I am trying to create a modal which has frosted glass backdrop. Imagine an element that pops up when clicked.
So:
page -> element
Upon clicking on element:
Page -> frosted backdrop -> element
All the solutions I have read online require the use of filter. However, by CSS specifications, position: fixed
does not work if filter or transform is used anywhere in the parent DOM hierarchy. That is because it creates a new context.
Is there any other to create a frosted glass backdrop?
Update
This the CSS specification which explicitly mentions that fixed will not work if the ancestor has transform
, filter
or perspective
. In other words, if any item in the ancestry uses blur effect, this will not work.
fixed
The element is removed from the normal document flow, and no
space is created for the element in the page layout. It is positioned
relative to the initial containing block established by the viewport,
except when one of its ancestors has a transform, perspective, or
filter property set to something other than none (see the CSS
Transforms Spec), or the will-change property is set to transform, in
which case that ancestor behaves as the containing block. (Note that
there are browser inconsistencies with perspective and filter
contributing to containing block formation.) Its final position is
determined by the values of top, right, bottom, and left.This value always creates a new stacking context. In printed
documents, the element is placed in the same position on every page.
https://developer.mozilla.org/en-US/docs/Web/CSS/position
In summary, all the solutions I have read online require the use of filter. I need one without (or a way to create a full screen backdrop without using
position: fixed
2
Answers
For your frosted backdrop, just create a
position-fixed
element that fills the screen behind your modal dialog that uses thebackdrop-filter
property.Quoting from MDN’s page on
backdrop-filter
:There may be something wrong in the way you constructed your document with main content and modal, because testing below snippet on Chrome/Edge/Firefox shows nothing out of the ordinary and displays a
backdrop-filter
as advertised.The snippet shows four differently positioned elements
.wrapper
, one positionedabsolute
and the otherfixed
.body
, one positionedfixed
and the otherabsolute
.backdrop-filter
.Each ‘modal’ contains
position: relative
content which in turn has an ‘absolute’ positioned.modal-content::before
as header.backdrop-filter
defined.As positioning and filters each create new stacking context, you now have an example using at least eight new stacking context, each with their own
backdrop-filter
being frosted glass over some content.I left out the
onclick
mechanism…A codepen for instant fiddling: SO75550961, frosted glass