CSS background color is erased by scroll bar on Ch

2019-07-03 16:50发布

问题:

I have a container with specified height, and inside it items are stacked. By default, I set overflow items as hidden and once mouse hover I enable the container to show scroll bar.
The HTML

#container {
  width: 200px;
  background-color: gray;
  height: 200px;
  overflow-y: hidden;
}
#container:hover {
  overflow-y: auto;
}
.item {
  margin-top: 2px;
  margin-bottom: 2px;
  background-color: yellow;
}
<div id="container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
  <div class="item">Item 3</div>
  <div class="item">Item 4</div>
  <div class="item">Item 5</div>
  <div class="item">Item 6</div>
  <div class="item">Item 7</div>
  <div class="item">Item 8</div>
  <div class="item">Item 9</div>
  <div class="item">Item 10</div>
</div>

It works as I wished except on Chrome browser after mouse moving out the container and scroll bar is disappear, the background color of scroll bar region is erased. This happens only on Chrome. Any idea?

Demo

回答1:

try this code:

DEMO

#container:hover .item{
    width:auto;
}

.item {
    margin-top:2px;
    margin-bottom: 2px;
    background-color: yellow;
    width:200px;
}

you can use 100% instead 200px too.