Nick Grealy
Nick Grealy

Reputation: 25942

Is it possible to add a blur to the HTML5 dialog backdrop?

I'm trying to add a blur effect to the backdrop of a dialog (I realise dialog currently only has limited support - I'm using Chrome v66).

I've tried adding a blur filter to the ::backdrop css (no luck), and the backdrop-filter isn't supported yet.

Anyone know what I should be doing instead?

window.onload = function() {
  document.getElementById('mydialog').showModal();
}
dialog::backdrop {
  background: rgba(255, 0, 0, .25);
}

/* attempt #1 - using a blur filter */
dialog::backdrop {
  -webkit-filter: blur(2px);
  -moz-filter: blur(2px);
  -o-filter: blur(2px);
  -ms-filter: blur(2px);
  filter: blur(2px);
}

/* attempt #2 - using backdrop-filter */
dialog::backdrop {
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

/* attempt #3 - using an svg */
dialog::backdrop {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='100%' height='100%'><defs><filter id='blur'><feGaussianBlur stdDeviation='5' /></filter></defs><rect filter='url(%23blur)' fill='rgba(255,255,255,0.5)' x='0' y='0' width='100%' height='100%'/></svg>");
}
Here is some other text that I want blurred.
<dialog id="mydialog">This is the dialog window</dialog>

Upvotes: 7

Views: 7489

Answers (4)

Mohamed Rabie
Mohamed Rabie

Reputation: 1

dialog:open::backdrop {
  background-color: rgb(0 0 0 / 25%);
  backdrop-filter: blur(4px);
}

that's works with me

Upvotes: 0

For the love of
For the love of

Reputation: 31

CSS :has() selector to the rescue here!

body:has(dialog[open]) {
  filter: blur(4px);
}

Upvotes: 3

Ziarek
Ziarek

Reputation: 877

Use backdrop-filter:

dialog::backdrop {
        backdrop-filter: blur(1px);
}

Upvotes: 19

Nick Grealy
Nick Grealy

Reputation: 25942

So not an ideal solution (I'd prefer to use css only), but this is an example workaround using javascript to apply a .blur class to the body.

window.onload = function(){
  document.getElementById('mydialog').showModal();
  document.getElementsByTagName('body')[0].classList.add("blur")
}
dialog::backdrop {
  background: rgba(255,0,0,.25);
}
.blur {
  -webkit-filter: blur(2px);
  -moz-filter: blur(2px);
  -o-filter: blur(2px);
  -ms-filter: blur(2px);
  filter: blur(2px);
}
Here is some other text that I want blurred.
<dialog id="mydialog">This is the dialog window</dialog>

Upvotes: 2

Related Questions