CSS Vertical Scrollbar Padding Left/Right in Ul Possible

CSS vertical scrollbar padding left/right in UL possible?

You can see an example below, basically forget adding margin or padding there, just increase the width/height of scroll area, and decrease the width height of thumb/track.

Quoted from how to customise custom scroll?

body {
min-height: 1000px;
font-family: sans-serif;
}

div#container {
height: 200px;
width: 300px;
overflow: scroll;
border-radius: 5px;
margin: 10px;
border: 1px solid #AAAAAA;
}

div#content {
height: 1000px;
outline: none;
padding: 10px;
}

::-webkit-scrollbar {
width: 14px;
}

::-webkit-scrollbar-thumb {
border: 4px solid rgba(0, 0, 0, 0);
background-clip: padding-box;
border-radius: 9999px;
background-color: #AAAAAA;
}
<div id="container">
<div id="content" contenteditable>
Click to type...
</div>
</div>

How to make the padding in scrollbar? CSS

Scrollbar Padding

I think you'll have to use a container to accomplish the not touching part of your requirements.

Chrome vs Firefox

Be aware that the support to adjust the scrollbar is very limited in firefox compared to chrome browsers. The result of it will not show up in this snippet, nor on websites like jsfiddle. Rounded corners are impossible to achieve in firefox without using your own implementation or a third party library like thisone for example.

Example

body {
background-color: #14142B;
}

/* FIREFOX */

html {
scrollbar-width: thin;
scrollbar-color: #6E7191 #262338;
}

/* CHROME */

::-webkit-scrollbar {
width: 12px;
border-radius: 34px;
}

::-webkit-scrollbar-track {
background: #262338;
border-radius: 8px;
}

::-webkit-scrollbar-thumb {
background: #6E7191;
border-radius: 8px;
transition: all 0.4s;
-moz-transition: all 0.4s;
-webkit-transition: all 0.4s;
}

::-webkit-scrollbar-thumb:hover {
background: #7E81A1;
}

.container {
margin: 1.5rem .5rem;
overflow-y: scroll;
max-height: calc(100vh - 3rem);
}

.content {
height: 25rem;
}

hr {
border: 0;
border-top: 2px solid #201F36;
}

.filler {
height: 3rem;
}
<div class="container">
<div class="content">
<div class="filler"></div>
<hr>
<div class="filler"></div>
<hr>
<div class="filler"></div>
<hr>
<div class="filler"></div>
</div>
</div>

How to add left and right padding to ::webkit-scrollbar-thumb?

You could try faking the padding with background-clip: padding-box and applying a transparent border-right and left.

::-webkit-scrollbar {
background: #181818;
width: 20px;
}

::-webkit-scrollbar-thumb {
padding: 0 4px;
border-right:4px solid transparent;
border-left:4px solid transparent;
background: #909090;
background-clip: padding-box;
border-radius: 100px;
&:hover {
background: #606060;
}
}

html,
body {
height: 300%;
}

Add padding to scrollbar

Initially, I have found and started using react-custom-scrollbars npm package, but it seemed it had a lot of open issues and I had problems implementing it, so I switched to simplebar package which is very lightweight and works similarly to a facebook custom scrollbar. Solved my issue.

Faking left and right margins for webkit scrollbar?

All right. Was not so hard, but unfortunately there is a lot of extra html in my solution. Please tell if need comments and/or explanation of something.
Result is below.

.dropdown {    width: 360px;    background-color: green;    padding-bottom: 10px;    max-height: 365px;    margin-bottom: 20px;}.itemContainer {    max-height: 355px;    overflow-y: auto;    padding-left: 10px;    padding-top: 10px;    margin-right: 10px;}.itemContainer>div {    display: table;    width: 100%;}.item {    display: table-row;}.item>div{    display: table-cell;    vertical-align: top;    padding-bottom: 10px;}.item:last-child>div {    padding-bottom: 0;}.item > div:nth-child(1) {    width: 100%;}.item>div:nth-child(1)>div{    background-color: white;    padding: 10px;    height: 51px;}.item:nth-child(5) > div:nth-child(2) > div {    width: 10px;}@media screen {    .itemContainer::-webkit-scrollbar {        width: 6px;        border-radius: 2px;    }    .itemContainer::-webkit-scrollbar-thumb {        border-radius: 2px;        background-color: black;        border: solid red 10px;    }    .itemContainer::-webkit-scrollbar-track {        margin-top: 10px;        background-color: yellow;        width: 6px;    }}
  <div class="dropdown">    <div class="itemContainer"><div>      <div class="item"><div><div>thing</div></div><div><div></div></div></div>      <div class="item"><div><div>thing</div></div><div><div></div></div></div>      <div class="item"><div><div>thing</div></div><div><div></div></div></div>      <div class="item"><div><div>thing</div></div><div><div></div></div></div>      <div class="item"><div><div>thing</div></div><div><div></div></div></div>    </div></div>  </div>
<div class="dropdown"> <div class="itemContainer"><div> <div class="item"><div><div>thing</div></div><div><div></div></div></div> <div class="item"><div><div>thing</div></div><div><div></div></div></div> <div class="item"><div><div>thing</div></div><div><div></div></div></div> <div class="item"><div><div>thing</div></div><div><div></div></div></div> </div></div> </div>

Adding scroll bar to dynamic ul li

I've made a couple of adjustments to your original stylesheet, and now it will show the scroll bar, but only when it exceeds the height of 3 list items (63 pixels in this case). Here's the final CSS code:

#mnav {
margin-left: -30px;
margin-right: -30px;
}
#mnav li {
float: left;
list-style: none;
margin:0 10px;/*Keeping 10px space between each nav element*/
}
#mnav li a,/*These can all be merged into a single style*/
#mnav li a:link,
#mnav li a:visited,
#mnav li a:hover,
#mnav li a:active {
text-decoration: none;
}
#mnav li ul {
display: none;/*This is the default state.*/
z-index: 9999;
position: absolute;
width: 400px;
max-height:63px;/*The important part*/
overflow-y:auto;/*Also...*/
overflow-x:hidden;/*And the end of the important part*/
margin: 0px;
padding-left:5px;/*Removing the large whitespace to the left of list items*/
border: 1px solid #ddd;
}
#mnav li:hover ul, #mnav li.sfhover ul {
display:block;/*This is the hovered state*/
}
#mnav li ul li a, #mnav li ul li a:link, #mnav li ul li a:visited {
display: block;
margin: 0;
text-decoration: none;
z-index: 9999;
border-bottom: 1px dotted #ccc;
width:400px;
}
#mnav li ul li a:hover, #mnav li ul li a:active {
display: block;
margin: 0;
text-decoration: none;
}

Here's the demo of it. I've also, for demonstration purposes, inserted 2 extra <li> elements to the Home and SQL Server vs Oracle items. The Home item will show how the popup behaves if there are fewer than 3 list items there.

To explain each of the changed bits, first the code that actually does the trick:

  • defining the max-height to 63 will make the ul behave normally if it stays under 63px high, but if it exceeds that, it will overflow and show a scroll bar. If you want to show more items, just increase the max-height to the desired height. Currently the height of each item is 21px, so I used that to get the 63px for 3 items.
  • Since the overflow should only show a scroll bar for the vertical direction, only overflow-y:auto; should be there, and overflow-x:hidden to prevent a scrollbar in the horizontal direction.

And then the other general changes I made:

  • I've added 20px margin between items (10px on either side of the element) to make the list look a bit better here. You may want to apply your own style though, this is just for this demo.
  • I've changed your hiding technique from 'shoving it off to -999em to the left' to hiding it via display:none. This is better to work with, since elements that are display:none will not render in search engines, so this will help in those situations. In general I think hiding things with display:none is just better than shoving it off the screen, while it's still actually there
  • I've removed the padding to the left of the ul since it just looked quite bad. No need for the default padding if you're not using it for a dotted/numbered list.

This should also work considering your comment to Zachary Carter's answer, since this won't show a huge white box if you define the max-height to 210px (10 items).

Unexpected padding or white space at bottom of page causing unnecessary vertical scroll?

The following elements have height:100%:

  • html
  • body
  • photoFrameContainer

Margins and padding are not calculated within the height, so 100% + margin + padding is what you see. Here is what the spec says:

For absolutely positioned elements, the used values of the vertical dimensions must satisfy this constraint:

'top' + 'margin-top' + 'border-top-width' + 'padding-top' + 'height' + 'padding-bottom' + 'border-bottom-width' + 'margin-bottom' + 'bottom' = height of containing block

Since the calculated height is greater than the height of the viewport, and the default stylesheet defines overflow:visible as the default for all elements, the scrollbar is shown.

References

  • Visual Formatting Model Details: Absolutely Positioned, non-replaced elements

  • CSS 2.1: Overflow

  • CSS Dimensions

How to Add Padding to Vertical Dropdown without Messing up Dropdown?

@Grace Sawyer, it's an issue of specificity. When you implement your code ul li { padding 10px; } that styling is being inherited by the list-items in your sub-menu. One way of overcoming this is to add selecting those submenu list-items and setting their styling directly. This way it overrides the inherited styling. If you need a refresher or are new to CSS, this is a good article that I always refer back to for referencing specificity.

If you add these lines of code to your CSS, you'll get your desired results

nav ul li { padding: 15px; }

nav ul li ul li { padding: 0px;}

I also forked your jsfiddle and implemented the code, so you can see it in action here.



Related Topics



Leave a reply



Submit