1

我有一个 div 作为我的容器的一部分,它向各个方向溢出,以便给我一个背景(它位于 z-index:0)我禁用了水平滚动(html {overflow-x:hidden;}),因为它永远不应该被要求。

但是我希望垂直滚动条忽略我的“背景”div,但如果我的其余内容拉伸以适应高度,那么滚动条应该出现。

我已经尝试溢出:隐藏在后台 div 中,但它没有效果。

4

1 回答 1

1

一个简单的解决方案是为您的背景 div 设置以下内容:

#bgdiv {
    max-width:  100%;   /* Won't cause horizontal scrollbars */
    max-height: 100%;   /* Won't cause vertical scrollbars */
    position: fixed;    /* Positions relative to viewport */
    top: 0; left: 0;    /* Sets to top left of viewport */
    z-index: 0;         /* Places at a low z-index level */
}

并提供一个额外的元素来容纳您的内容,它本身具有以下内容:

#body {
    position: relative; /* Permits z-index placement */
    z-index: 1;         /* Places just above background div */
}

小提琴:http: //jsfiddle.net/jonathansampson/R3kBP/4/

于 2012-06-14T16:15:26.477 回答