Live data from Hacker News

A couple CSS tricks for HTML Dialog elements

cassidoo.co

11–20 of 48 posts

Re: A couple CSS tricks for HTML Dialog elements

#11
datalist is one I stumbled upon and blew me away. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/da...

It's not a replacement for select as you still need an input to tie it to but it seems to handle filtering a list of options nicely.

Also, if you have two selects with the same list in it, you can do it once with datalist and have two inputs, say a list of clients with client_a and client_b for inputs.

I don't quite care for how it displays the value, like if you put the ID as the value and the client name in the option element, you can filter by the ID or the name but the input will show the ID only.

Re: A couple CSS tricks for HTML Dialog elements

#12

Thanks for the article! I remember I had a hard time trying to stretch dialog to full screen for mobile devices, but it actually didn't want to work. The code was something like this: dialog { position: absolute; top: 10px; right: 10px; bottom: 10px; left: 10px; }

You can shorthand the last four declarations with a single `inset: 10px;` (or maybe `inset: .625rem;`?

Re: A couple CSS tricks for HTML Dialog elements

#13
post #8

This is a neat piece of modern CSS: body:has(dialog[open]) { overflow: hidden; } https://caniuse.com/css-has confirms the has() selector has had widespread browser support since December 2023.

YMMV / be careful with this, body:has() and html:has() can be extremely expensive (and introduce severe lag visible to the user) if you have dynamic components on the page that are constantly altering the DOM (ex. react/vue apps)

Re: A couple CSS tricks for HTML Dialog elements

#14
post #11

datalist is one I stumbled upon and blew me away. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/da... It's not a replacement for select as you still need an input to tie it to but it seems to handle filtering a list of options nicely. Also, if you have two selects with the same list in it, you can do it once with datalist and have two inputs, say a list of clients with client_a and client_b for inputs. I…

I've tried using datalist with text inputs and it never quite worked out from a UX perspective. Users would always complain about weird quirks with how it populates & clears values. A normal element with an "Other" option + conditional input element is much more predictable.

Re: A couple CSS tricks for HTML Dialog elements

#15
post #14
post #11

datalist is one I stumbled upon and blew me away. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/da... It's not a replacement for select as you still need an input to tie it to but it seems to handle filtering a list of options nicely. Also, if you have two selects with the same list in it, you can do it once with datalist and have two inputs, say a list of clients with client_a and client_b for inputs. I…

I've tried using datalist with text inputs and it never quite worked out from a UX perspective. Users would always complain about weird quirks with how it populates & clears values. A normal element with an "Other" option + conditional input element is much more predictable.

Yeah, for autocomplete it has a weird UX feel to it.

Some of the other examples like using it with the slider and the color picker seem like they're useful.

Re: A couple CSS tricks for HTML Dialog elements

#17
post #8

This is a neat piece of modern CSS: body:has(dialog[open]) { overflow: hidden; } https://caniuse.com/css-has confirms the has() selector has had widespread browser support since December 2023.

I used this same approach in a recent web app and it worked great. You can also use scrollbar-gutter: stable, which disables scrolling but maintains the preserved space to avoid content reflows.

Re: A couple CSS tricks for HTML Dialog elements

#18
post #11

datalist is one I stumbled upon and blew me away. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/da... It's not a replacement for select as you still need an input to tie it to but it seems to handle filtering a list of options nicely. Also, if you have two selects with the same list in it, you can do it once with datalist and have two inputs, say a list of clients with client_a and client_b for inputs. I…

It looks like the UI would clash with the browser's autofill recommendations.

Re: A couple CSS tricks for HTML Dialog elements

#19
post #3

The trick to prevent scrolling by setting overflow: hidden unfortunately results in visual page jumping for me. The reason is I have macOS set up to always show scroll bars, instead of hiding them. At least one browser (I forget which, but I test on Safari, Firefox and Chrome) doesn’t have a disabled scroll bar but removes it altogether. This makes the page wider and causes it to reflow and move. Does anyone know how…

Website authors can set a scrollbar gutter.

scrollbar-gutter: stable; to those unfamiliar. https://developer.mozilla.org/en-US/docs/Web/CSS/scrollbar-g...

Re: A couple CSS tricks for HTML Dialog elements

#20

Thanks for the article! I remember I had a hard time trying to stretch dialog to full screen for mobile devices, but it actually didn't want to work. The code was something like this: dialog { position: absolute; top: 10px; right: 10px; bottom: 10px; left: 10px; }

this is most likely due to the absolute positioning. position: absolute will use the top-left corner of the closest ancestor that is "positioned" as the origin for it's layout [1]. If you want that origin to be the top-left corner of the viewport, use position: fixed.

[1] https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_...

Post reply on HN