I’d like to have a div's position fixed after scrolling. But when I change the position to fixed
the div goes out of it’s container.
The div with the class fixed
should be just as wide as the div with the class relative
. The width of the parent div is dynamic. Only the max-width is known. No position sticky should be used.
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).
Set width of a "Position: fixed" div relative to parent div
Set width of fixed positioned div relative to his parent having max-width
The following solutions didn't work for me:
- width: inherit
- max-width: inherit
- width: 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>