CSS:如何使滚动条始终可见而不减少容器的宽度

3
我尝试在一个垂直滚动的 div 中使用始终可见的滚动条,但不想让滚动条减少 div 内元素的宽度。基本上,我希望使用 "overflow-y: scroll" 的 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>


你想要滚动条吗?因为在Mac系统中通常会自动隐藏,而在Windows中则始终可见。 - manny
1
你想要一个始终存在的滚动条,但它不应该减小其所在容器的大小。在这种情况下,您可以在元素上使用overflow-y: scroll,并向右侧提供额外的填充以适应滚动条的宽度(我认为大多数浏览器的宽度约为17px)。 - Steven Kuipers
1个回答

1
如果您特别希望它与webkit伪类一起使用,则需要按照以下方式进行。

.scrollbar {
 margin-left: 22px;
 float: left;
 height: 180px;
 width: 200px;
 background: #F5F5F5;
 overflow-y: scroll;
  margin:10px;
}
.force-overflow {
 min-height: 450px;
}
#wrapper {
 text-align: center;
 margin: auto;
}
#style-1::-webkit-scrollbar {
 width: 20px;
}

/**  STYLE A */
#style-1::-webkit-scrollbar-thumb {
 border-radius: 10px;
 background-color: green;
}

#style-1::-webkit-scrollbar-track {
 border-radius: 10px;
 background-color: red;
}
<div id="wrapper">
  <div class="scrollbar" id="style-1">
    <div class="force-overflow"><h2>A</h2></div>
  </div>
</div>
<div id="wrapper">
  <div class="scrollbar" id="style-default">
    <div class="force-overflow"><h2>B</h2></div>
  </div>
</div>

注意,我只是在做这个-webkit-scrollbar


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接