简体   繁体   English

使元素粘在其父容器上

[英]Make elements sticky to their parent container

I am attempting to create the following layout:我正在尝试创建以下布局: 在此处输入图片说明

As you can see when I scroll, the alignment of the 1st and 3rd items has scrolled with the content.正如您在滚动时所看到的,第 1 项和第 3 项的对齐方式随内容一起滚动。 I need a way to get the first and third items to stay sticky to their respective sides.我需要一种方法来让第一项和第三项保持在各自的两侧。 I also need this solution to be responsive as the container scales with screen size.我还需要此解决方案在容器随屏幕尺寸缩放时做出响应。

 #container { height: 300px; width: 50%; position: relative; overflow: auto; border: solid 1px black; } .item { height: 500px; width: 100px; z-index: 5; position: absolute; background-color: red; } .item:nth-child(2) { width: 800px; z-index: 2; background-color: green; } .item:nth-child(3) { right: 0; }
 <div id="container"> <div class="item">&nbsp</div> <div class="item">&nbsp</div> <div class="item">&nbsp</div> </div>

What I Am Actually Building This is a generalized problem.我实际上在构建什么这是一个普遍的问题。 The actual problem I am trying to solve is that of a custom-built HTML table.我试图解决的实际问题是定制的 HTML 表格。 The left and right items (red sections) are going to be containers for static (sticky) columns.左侧和右侧项目(红色部分)将成为静态(粘性)列的容器。 With that being said, I need the constraints of the problem to stay the same.话虽如此,我需要问题的约束保持不变。 Changing the width of the item:nth-child(2) to 100% will not work.将 item:nth-child(2) 的宽度更改为 100% 将不起作用。 Wrapping the contents inside of the item:nth-child(2) in another div and adding overflow: auto to that div will not work because then the scrollbar will not be shared amongst the entire container like it is in my example.将 item:nth-child(2) 内的内容包装在另一个 div 中并将 overflow: auto 添加到该 div 将不起作用,因为这样滚动条将不会像在我的示例中那样在整个容器之间共享。

You can try like below:您可以尝试如下:

 #container { height: 300px; display:flex; /* added */ overflow: auto; border: solid 1px black; } .item { height: 500px; width: 100px; background-color: red; flex-shrink:0; /* added */ } .item:nth-child(2) { width: 800px; background-color: green; } .item:nth-child(1) { left: 0; top:0; position: sticky; } .item:nth-child(3) { right: 0; top:0; margin-left:auto; /* this is important*/ position: sticky; }
 <div id="container"> <div class="item">&nbsp</div> <div class="item">&nbsp</div> <div class="item">&nbsp</div> </div>

.item:nth-child(2) {
    width: 100%;
    z-index: 2;
    background-color: green;
}

You can make .item:nth-child(2) width is 100%您可以使 .item:nth-child(2) 宽度为 100%

As I understand, you want the left and right sections to be fixed and the middle item to be responsive.据我了解,您希望左右部分是固定的,中间项目是响应式的。 If you consider a different approach to the problem.如果您考虑采用不同的方法来解决问题。 Here's my solution to it.这是我的解决方案。

 #container { display: grid; grid-template-columns: 200px 1fr 200px; height: 80vh; } .item1 { padding: 20px; background-color: #f90; } .item2 { flex: 1; padding: 20px; } .item3 { padding: 20px; background-color: #936; }
 <div id="container"> <div class="item1">Left</div> <div class="item2">Middle</div> <div class="item3">Right</div> </div>

You can add the position: sticky;您可以添加position: sticky; as required to any item to make it sticky.根据任何项目的要求使其具有粘性。

The main idea is use absolute wrapper to contain fixed child.主要思想是使用绝对包装器来包含固定的孩子。

Also remember to add padding in long content.还记得在长内容中添加填充。

 body { padding: 10px; background: #eee; display: flex; justify-content: center; } .parent { width: 100%; height: 100px; overflow-x: scroll; position: relative; border: 1px solid #000; background: #0f0; } .fix { position: fixed; background: rgba(255, 0, 0, .3); width: 100px; height: 100px; z-index: 2; } .fix-wrapper { position: absolute; top: 0; } .left { left: 0; } .right { right: 100px; } .content { height: 100%; width: 1000px; padding: 0 100px; }
 <div class="parent"> <div class="fix-wrapper left"> <div class="fix">Left</div> </div> <div class="content"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Pellentesque id nibh tortor id aliquet lectus proin. Viverra nam libero justo laoreet sit amet. Gravida cum sociis natoque penatibus et magnis dis parturient montes. At urna condimentum mattis pellentesque id nibh tortor id aliquet. Aenean pharetra magna ac placerat. Pellentesque habitant morbi tristique senectus et netus. Sed odio morbi quis commodo odio aenean. Ac turpis egestas maecenas pharetra convallis posuere morbi. Consectetur adipiscing elit ut aliquam purus sit amet luctus venenatis. Mollis aliquam ut porttitor leo a diam sollicitudin tempor. Amet mattis vulputate enim nulla aliquet porttitor lacus. Curabitur vitae nunc sed velit. Cursus eget nunc scelerisque viverra. Morbi tincidunt augue interdum velit euismod in pellentesque massa placerat. Etiam non quam lacus suspendisse faucibus. </div> <div class="fix-wrapper right"> <div class="fix">Right</div> </div> </div>

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

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