简体   繁体   English

不扩展父级的绝对定位

[英]Absolute positioning without extending parent

I'm trying to position a "seeker" in a div element that has other elements in it.我正在尝试 position 一个 div 元素中的“搜索器”,其中包含其他元素。 I want the seeker to be able to move on top of the content div, without pushing other content around, while also following the scrollbar.我希望搜索者能够在内容 div 之上移动,而无需推动其他内容,同时也跟随滚动条。 The area is resizable so I can't use constant width/height.该区域可调整大小,因此我不能使用恒定的宽度/高度。 Like the seeker in a video editor would.就像视频编辑器中的搜索器一样。

This is what I've gotten so far这是我到目前为止所得到的

 #container { width: 200px; height:100px; background-color: gray; overflow: scroll; } #content { width: 300px; height: 120px; } #box { width: 40px; height: 40px; background-color: green; } #seekerContainer { position: relative; width: 100%; height: 100%; } #seeker { width: 4px; height: 100%; position: relative; background-color: blue; left: 10%; }
 <div id="container"> <div id="content"> <div id="seekerContainer"> <div id="seeker"></div> </div> <div id="box"></div> </div> </div>

And I've tried different combinations having the seekerContainer and seeker be position absolute/relative, but either the seeker wont follow the scrolling, or it extends the height of the div.而且我尝试了不同的组合,其中 seekerContainer 和 seeker 是 position 绝对/相对,但是搜索者不会跟随滚动,或者它会扩展 div 的高度。

Any pointers to fix this?任何解决此问题的指针?

I think this is what you are looking for:我认为这就是你要找的:

 .container { width: 200px; height: 100px; background-color: gray; overflow: scroll; }.content { height: 500px; position: relative; }.box { width: 40px; height: 40px; background-color: green; }.seeker { width: 4px; height: 100%; position: absolute; background-color: blue; left: 10%; top: 0; }
 <div class="container"> <div class="content"> <div class="seeker"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> </div>

Also, you don't want to put id on every element and use id for styling.此外,您不想将id放在每个元素上并使用id进行样式设置。 You should use classes for that.你应该为此使用classes ID of the element has to be unique for the page so it's not very useful when you need to apply the same styling to more elements.元素的 ID 对于页面必须是唯一的,因此当您需要将相同的样式应用于更多元素时它不是很有用。

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

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