CSS位置固定在overflow-y内:滚动

时间:2016-03-12 08:08:22

标签: html css

我遇到问题,DIV position:fixed;加入overflow-y:scroll;

的DIV

检查这个小提琴:https://jsfiddle.net/1d5asvo7/

为什么position:fixed不会在DIV overflow-y:scroll之内?

为什么position:fixed引用window

我的代码出了什么问题? 如何使这个东西工作?

提前感谢...

1 个答案:

答案 0 :(得分:1)

您需要使用width: inherit fixed元素从width获取parent

#scrollable {width:400px; height:100%; overflow-y:scroll; }
#fixed {
    width: inherit;
    height: 40px;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1;
    background: red;
}
#content {width:100%; height:200px; }
<div id="scrollable">
  <div id="fixed">
  this is fixed
  </div>
  <div id="content">
    this is content this is content this is content this is content this is content this is content this is content this is content this is content this is content this is content this is content this is content v this is contentv v this is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is content this is content this is content this is content this is content this is content this is content this is content this is content this is content this is content this is content this is content this is content v this is contentv v this is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is content this is content this is content this is content this is content this is content this is content this is content this is content this is content this is content this is content this is content this is content v this is contentv v this is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is contentthis is content
  </div>
</div>