当我向innerdiv添加绝对位置时滚动条不起作用

时间:2017-03-01 13:06:19

标签: html css css3 position overflow

当我添加position:absolute到.divwidth。

时,

overflow-y滚动条不起作用

JsFiddle

.overflow {
  width:80%;
  margin-left:10%;
  height: 300px;
  overflow-y: auto;
  background: #333;
  color: #fff;
}
.divwidth {
  width:100px;
  height: 30px;
  background: #666;
  position: absolute;
  margin-left: -25px;
}

2 个答案:

答案 0 :(得分:0)

试试这个:您可以从position: absolute;类文件

添加.overflow
.overflow {
  width:80%;
  margin-left:10%;
  height: 300px;
  overflow-y: auto;
  background: #333;
  color: #fff;
  position: absolute;
}
.divwidth {
  width:100px;
  height: 30px;
  background: #666;
  position: relative;

}

DEMO HERE

OR

.overflow {
  width:80%;
  margin-left:10%;
  height: 300px;
  overflow-y: auto;
  background: #333;
  color: #fff;
  position: relative;
}
.divwidth {
  width:100px;
  height: 30px;
  background: #666;
  position: absolute;
  margin-left: -25px;
}

DEMO

答案 1 :(得分:0)

所以如果你把它添加到溢出的那个

.overflow {
 position: relative;
 width:80%;
 margin-left:10%;
 height: 300px;
 overflow-y: auto;
 background: #333;
 color: #fff;
}

这样,内部div的绝对尺寸将限制在此div