i have
<div id="div1">
any not fixed text
but this div has scrollbar
<div id="div2">
this div is fixed (without scrollbar)
</div>
</div>
我需要将
div2
固定在div1
上,但不要固定在浏览器的主滚动条上。类似于“将div2
粘贴到div1
上”。由于
div1
有滚动条,所以我需要固定div2
。如果我这样做:
#div1 {
position: absolute;
}
#div1 #div2 {
position: fixed;
}
它将同时修复div1
和浏览器窗口,但我只需要div1
。
测试示例:
<html>
<head>
<style type="text/css">
#div1 {
border: 1px solid gray;
position: absolute;
height: 200px;
width: 400px;
overflow-y: scroll;
}
#div1 #div2 {
position: fixed;
margin-left: 300px;
}
</style>
</head>
<body>
<br><br><br>
<div id="div1">
<div id="div2">fixed text</div>
<div style="height: 400px;"></div>
</div>
<div style="height: 1400px;"></div>
</body>
</html>
问题1:如何将div2
固定在div
上
问题2:在这种情况下,如何将div2
向右浮动到div1
的右侧(例如,在示例中是〜margin-left: 350px;
)
谢谢。
#div2{ /* style */}
而不是#div1 #div2{ /* style */}
。 - Roimer