👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Select All Elements Inside the UI using CSS

19 replies
Last updated: Aug 5, 2022
Hi! I am trying to use a JS in my Sanity Backend.Whivh is the best option to include a small script like this for example?
Thanks

var test = document.querySelectorAll('[data-ui="Grid"]')
Aug 5, 2022, 9:22 AM
What are you trying to do?
Aug 5, 2022, 9:22 AM
I need to select all ellements inside [data-ui=“Grid”] and add to them a custom class for example
Aug 5, 2022, 9:24 AM
For styling purposes or?
Aug 5, 2022, 9:26 AM
yes
Aug 5, 2022, 9:26 AM
i need to hide some items.
Aug 5, 2022, 9:26 AM
Have you tried following this document: https://www.sanity.io/docs/styling ?
Aug 5, 2022, 9:27 AM
yes , but I cant hide it with CSS. Need JS to do it 🙂
Aug 5, 2022, 9:28 AM
Oh, how come?
Aug 5, 2022, 9:28 AM
I need to hide those divs that have no child inside. Is it posible to do it with CSS?
Aug 5, 2022, 9:30 AM
Sure, with the
:empty
pseudo-class.
Aug 5, 2022, 9:32 AM
Thank you!!
Aug 5, 2022, 9:35 AM
Hi
user F
, I am trying to change the CSS from this div. But it is not working, any idea?
[data-ui="Grid"] [data-ui="Card"] .dINwnn{
  background: #000000 !important;
}
Aug 5, 2022, 11:20 AM
Did you import the stylesheet and everything as per the guide?
Aug 5, 2022, 11:20 AM
yes
Aug 5, 2022, 11:21 AM
Have you restarted your server?
Aug 5, 2022, 11:28 AM
I would also not rely on mangled classes (like
dINwnn
) since they’ll change with each deployment.
Aug 5, 2022, 11:28 AM
ohh, and how can I change the css only to this div? 😇
Aug 5, 2022, 11:29 AM
You could use
:last-child
maybe?
Aug 5, 2022, 11:45 AM
Don't be afraid to use the heck out of descendant selectors as well -- there are so many UI component containers I had to write a chain of six of them just to get the right "path" for my custom CSS.
Aug 5, 2022, 6:06 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?