CSS:滚动条始终可见而不减小容器的宽度

问题描述 投票:2回答:1

我试图在一个垂直可滚动的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>
html css html5 css3 stylesheet
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

© www.soinside.com 2019 - 2024. All rights reserved.