我尝试在一个垂直滚动的 div 中使用始终可见的滚动条,但不想让滚动条减少 div 内元素的宽度。基本上,我希望使用 "overflow-y: scroll" 的 div 与浏览器的默认行为相同,但不会在不滚动时使滚动条消失。
我尝试了以下方法,但是如您所见,滚动条减少了 div 可用的宽度,这不是我想要的。
这是可能的吗?
我尝试了以下方法,但是如您所见,滚动条减少了 div 可用的宽度,这不是我想要的。
这是可能的吗?
/* Css Reset */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
/* ------------------------------------------------------ */
body {
width: 100%;
height: 10%;
background: #ddd;
padding: 2em;
}
#content {
width: 400px;
height: 12em;
background: red;
overflow-y: scroll;
}
#content::-webkit-scrollbar {
border-radius: 0.5em;
height: 4px;
width: 0.5em;
background: transparent;
}
#content::-webkit-scrollbar-thumb {
background: green;
border-radius: 0.5em;
}
#content::-webkit-scrollbar-track {
border-radius: 0.5em;
background-color: transparent;
}
.item {
width: 100%;
height: 4em;
line-height: 4em;
text-align: center;
vertical-align: middle;
color: white;
background: #333;
border-bottom: 1px solid white;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>scrollbar</title>
<meta name="viewport" content="width=device-width initial-scale=1.0 maximum-scale=1.0 user-scalable=yes" />
</head>
<body>
<div id="content">
<ul>
<li class="item">1</li>
<li class="item">2</li>
<li class="item">3</li>
<li class="item">4</li>
</ul>
</div>
</body>
</html>