Last active
April 15, 2024 09:43
-
-
Save flekschas/a817fd1a67aaca511964 to your computer and use it in GitHub Desktop.
Custom styled scroll bar with 'margin'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Custom scroll bar with 'margin'</title> | |
</head> | |
<body> | |
<div id="content"></div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html, body { | |
width: 100%; | |
height: 100%; | |
margin: 0; | |
padding: 0; | |
overflow: scroll; | |
background: linear-gradient(#f2f2f2, #d9d9d9); | |
} | |
#content { | |
height: 100em; | |
} | |
::-webkit-scrollbar { | |
width: 2em; /* Total width including `border-width` of scrollbar thumb */ | |
height: 0; | |
} | |
::-webkit-scrollbar-thumb { | |
height: 1em; | |
border: 0.5em solid rgba(0, 0, 0, 0); /* Transparent border together with `background-clip: padding-box` does the trick */ | |
background-clip: padding-box; | |
-webkit-border-radius: 1em; | |
background-color: rgba(0, 0, 0, 0.15); | |
-webkit-box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.025); | |
} | |
::-webkit-scrollbar-button { | |
width: 0; | |
height: 0; | |
display: none; | |
} | |
::-webkit-scrollbar-corner { | |
background-color: transparent; | |
} |
@lobarevk As mentioned in the comment above, when specifying the width of the scroll bar you should consider the width of border as well. So if you change the width of the scroll bar o px
you should also change the width of the border. E.g.
...
::-webkit-scrollbar {
width: 10px;
}
::-webkit-scrollbar-thumb {
...
border: 2px solid rgba(0, 0, 0, 0);
...
}
In this example the final width of the handle will be 6px
(2px
+6px
+2px
= 10px
)
@bajcsiadel
Is it possible to create an margin on the right side only while saving the border-radius?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works, but how to change width? If I change width to '10px' (or some 'em' to 'px') scrollbar disappears