How to add ::-webkit-scrollbar pseudo element in Chakra UI element? (React)
Asked Answered
U

4

14

I'm working with Chakra UI and i needed to customize the scrollbar style using the css pseudo element ::-webkit-scrollbar, but Chakra UI doesn't seen to have this pseudo element and a I don't know where I can style this specific component without creating a global css class.

Here is a sample of my code:

<Box
  overflowX="auto"
  maxW="100vw"
  h="100%"
  whiteSpace="nowrap"
  pb="17px"
  color="white"
  px="32px"
  // the ::-webkit-scrollbar property should goes here
>
  <!-- content -->
</Box>
Unbraid answered 27/11, 2020 at 18:45 Comment(0)
M
33

Try css prop:

<Box
  overflowY="auto"
  css={{
    '&::-webkit-scrollbar': {
      width: '4px',
    },
    '&::-webkit-scrollbar-track': {
      width: '6px',
    },
    '&::-webkit-scrollbar-thumb': {
      background: scrollbarColor,
      borderRadius: '24px',
    },
  }}
>
Mallette answered 2/1, 2021 at 2:2 Comment(3)
Do you know how to add ::-webkit-scrollbar-thumb:horizontal, cannot figure out what rules chakra is using internally for pseudo css.Vaporize
This doesn't seem to work anymore.Lingonberry
Not sure if you still need this @Vaporize but for horizontal scroll bar you need to specify the height instead of the width.Sternick
F
8

Use Chakra sx prop here.

<Box
  overflowX="auto"
  maxW="100vw"
  h="100%"
  whiteSpace="nowrap"
  pb="17px"
  color="white"
  px="32px"
  sx={
     { 
    '::-webkit-scrollbar':{
           display:'none'
       }
    }
  }
>
  <!-- content -->
</Box>

To know more about Chakra sx prop, refer docs

Freshet answered 24/6, 2022 at 16:23 Comment(0)
P
2

It worked for me you can use overflow="scroll" and with this the prop sx={}

<Flex
    mt="20px"
    overflow="scroll"
    sx={{
      "::-webkit-scrollbar": {
        display: "none",
      },
    }}
  />
Phenyl answered 30/8, 2022 at 10:5 Comment(0)
D
1

its not gonna work with just css you need to __css

<Box
  overflowY="auto"
  __css={{
        '&::-webkit-scrollbar': {
          w: '2',
        },
        '&::-webkit-scrollbar-track': {
          w: '6',
        },
        '&::-webkit-scrollbar-thumb': {
          borderRadius: '10',
          bg: `gray.100`,
        },
      }}
>
Drawee answered 15/6, 2022 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.