简体   繁体   English

固定 position 的子 div 应具有与 position 相对的父 div 相同的宽度

[英]child div with position fixed should have same width as parent div with position relative

I'd like to have a div's position fixed after scrolling.我想在滚动后修复一个 div 的 position。 But when I change the position to fixed the div goes out of it's container.但是当我将 position 更改为fixed div 时,它会从它的容器中取出。 The div with the class fixed should be just as wide as the div with the class relative . class fixed的 div 应该与 class relative的 div 一样宽。 The width of the parent div is dynamic.父 div 的宽度是动态的。 Only the max-width is known.只有最大宽度是已知的。 No position sticky should be used.不应该使用 position 粘性。

I'm trying to achieve the following: When you click a button, a form opens.我正在尝试实现以下目标:单击按钮时,会打开一个表单。 the form should be fixed.表格应该是固定的。

黏

There are already some threads on this topic.已经有一些关于这个主题的主题。 However, the solutions didn't work for me.但是,这些解决方案对我不起作用。 It can also be solved with JS (but no JQuery).也可以用 JS(但不用 JQuery)来解决。

Set width of a "Position: fixed" div relative to parent div 设置“位置:固定”div 相对于父 div 的宽度

Set width of fixed positioned div relative to his parent having max-width 相对于具有最大宽度的父级设置固定定位 div 的宽度

The following solutions didn't work for me:以下解决方案对我不起作用:

  • width: inherit宽度:继承
  • max-width: inherit最大宽度:继承
  • width: 100%宽度:100%

 .relative { position: relative; background-color: red; max-width: 1200px; height: 150vh; }.absolute { position: absolute; background-color: blue; top: 0; left: 0; right: 0; bottom: 0; width: inherit; }.fixed{ position: fixed; background-color: green; height: 50px; width: inherit; z-index: 5; }
 <div class="relative"> <div class="absolute"> <form class="fixed"></form> </div> </div>

You need to inherit your max-width on both child elements, and on the last one apply width: 100%.您需要在两个子元素上继承您的最大宽度,并在最后一个应用宽度:100%。 And also apply your sticky class to the fixed div.并将您的粘性 class 应用到固定 div。

 .relative { position: relative; background-color: red; max-width: 1200px; height: 150vh; }.absolute { position: absolute; background-color: blue; top: 0; left: 0; right: 0; bottom: 0; width: inherit; max-width: inherit; }.sticky { position: fixed; background-color: green; height: 50px; width: inherit; z-index: 5; max-width: inherit; width: 100%; }
 <div class="relative"> <div class="absolute"> <form class="sticky"></form> </div> </div>

Understand Fixed: ( Full Blog Here )了解固定:( 完整博客在这里
"Unlike absolute, fixed doesn't position itself from its closest relative parent. Instead, fixed positions itself relative to the viewport . The viewport will always stay fixed , which is why you get the effect that you do. “与绝对不同,固定 position 本身不会从其最近的相对父级。相反,固定位置本身相对于视口视口将始终保持固定,这就是为什么你会得到你所做的效果。

That being said, whenever you "inherit" any width it will be respective to the viewport .话虽如此,只要您“继承”任何宽度,它将与 viewport 相关 So it does us no good when we're trying set the width of our target element to the width of it's parent"因此,当我们尝试将目标元素的宽度设置为其父元素的宽度时,这对我们没有好处”

Css fixed position is a bit tricky to play with, maybe you should consider using position: sticky . Css 固定 position 玩起来有点棘手,也许你应该考虑使用position:sticky It remains relative to its parent element.它保持相对于其父元素。

Try this:尝试这个:

 * { margin: 0%; padding: 0%; box-sizing: border-box; }.relative { background-color: red; width: 50%; height: 350vh; }.absolute { background-color: blue; position: sticky; top: 50%; margin-left: 50%; transform: translate(-50%, -50%); width: 50%; height: 25vh; display: flex; justify-content: center; align-items: center; }.fixed { background-color: green; height: 50%; width: 80%; }
 <div class="relative"> <div class="absolute"> <form class="fixed"></form> </div> </div>

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

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