简体   繁体   English

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

[英]Set position of div relative to position of another percentage-width div

I two divs set within a parent div - a sidebar on the left & a content area to the right of that. 我在父div中设置了两个div - 左侧的侧边栏和右侧的内容区域。 I need to set the position of my content area (which has a fixed width) to always be 15px to the right of the sidebar (even as the browser window / sidebar stretches). 我需要将我的内容区域(具有固定宽度)的位置设置为始终为侧边栏右侧的15px(即使浏览器窗口/侧边栏拉伸)。

Setting the sidebar with position: absolute & a % width worked perfectly for the sidebar itself, but as the sidebar position is then absolute, the content doesn't recognise where the sidebar is & can't be positioned relative to it with CSS. 设置侧边栏的位置:绝对和%宽度对于侧边栏本身非常有效,但由于侧边栏位置是绝对的,因此内容无法识别侧边栏的位置,也无法使用CSS相对于侧边栏定位。

Here is an image of what I am trying to achieve: 这是我想要实现的目标的图像:

在此输入图像描述

Why are you using position absolute for the side bar? 为什么你在侧栏使用绝对位置?

I would just do it like this 我会这样做的

    <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 链接到JS Fiddle来看看

Unless I'm not understanding your question? 除非我不理解你的问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM