gpt4 book ai didi

javascript - 设置 div 相对于另一个百分比宽度 div 位置的位置

转载 作者:太空宇宙 更新时间:2023-11-04 04:34:58 26 4
gpt4 key购买 nike

我在一个父 div 中设置了两个 div - 左侧是侧边栏,右侧是内容区域。我需要将我的内容区域(具有固定宽度)的位置设置为始终位于侧边栏右侧 15 像素(即使浏览器窗口/侧边栏拉伸(stretch))。

使用 position: absolute 和 % width 设置侧边栏对于侧边栏本身非常有效,但是由于侧边栏位置是绝对的,因此内容无法识别侧边栏的位置并且无法相对于它定位CSS。

这是我想要实现的目标的图像:

enter image description here

最佳答案

为什么要对侧边栏使用绝对位置?

我会这样做

    <div class="container">
<div class="sidebar">
<p>This is your sidebar</p>
</div>
<div class="content">
<p>This is your content</p>
</div>
</div>


.container {width:100%; height: 1000px; background:blue;} /*didn't supply a width*/
.sidebar {width:25%; float:left; min-height:200px; background:lightblue; margin-right:15px;} /*you said sidebar has a % width*/
.content {width:250px; height:200px; background:red; float:left;} /*you said content had fixed width*/

Link to JS Fiddle to have a look

除非我不明白你的问题?

关于javascript - 设置 div 相对于另一个百分比宽度 div 位置的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16581018/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com