I have a positioned div whose content can be too long so scrollbars appear (overflow:auto
set). It functions as a dialog box in an ajax app. I want to fix a clo
You can use the position:fixed;
, but without set left
and top
. Then you will push it to the right using margin-left
, to position it in the right position you wish.
Check a demo here: http://jsbin.com/icili5
Seems, css transforms can be used
"‘transform’ property establishes a new local coordinate system at the element",
but ... this is not cross-browser, seems only Opera works correctly
I know this is an old post but I had the same question but didn't find an answer that set the element fixed relative to a parent div
. The scroll bar on medium.com is a great pure CSS solution for setting something position: fixed;
relative to a parent element instead of the viewport (kinda*). It is achieved by setting the parent div
to position: relative;
and having a button wrapper with position: absolute;
and the button of course is position: fixed;
as follows:
<div class="wrapper">
<div class="content">
Your long content here
</div>
<div class="button-wrapper">
<button class="button">This is your button</button>
</div>
</div>
<style>
.wrapper {
position: relative;
}
.button-wrapper {
position: absolute;
right: 0;
top: 0;
width: 50px;
}
.button {
position: fixed;
top: 0;
width: 50px;
}
</style>
working example
*Since fixed elements don't scroll with the page the vertical position will still be relative to the viewport but the horizontal position is relative to the parent with this solution.
I achieved to have an element with a fixed position (wiewport) but relative to the width of its parent.
I just had to wrap my fixed element and give the parent a width 100%. At the same time, the wrapped fixed element and the parent are in a div which width changes depending on the page, containing the content of the website. With this approach I can have the fixed element always at the same distance of the content, depending on the width of this one. In my case this was a 'to top' button, always showing at 15px from the bottom and 15px right from the content.
https://codepen.io/rafaqf/pen/MNqWKB
<div class="body">
<div class="content">
<p>Some content...</p>
<div class="top-wrapper">
<a class="top">Top</a>
</div>
</div>
</div>
.content {
width: 600px; /*change this width to play with the top element*/
background-color: wheat;
height: 9999px;
margin: auto;
padding: 20px;
}
.top-wrapper {
width: 100%;
display: flex;
justify-content: flex-end;
z-index: 9;
.top {
display: flex;
align-items: center;
justify-content: center;
width: 60px;
height: 60px;
border-radius: 100%;
background-color: yellowgreen;
position: fixed;
bottom: 20px;
margin-left: 100px;
cursor: pointer;
&:hover {
opacity: .6;
}
}
}
Try position:sticky on parent div of the element you want to be fixed.
More info here: http://html5-demos.appspot.com/static/css/sticky.html. Caution: Check for browser version compatibility.
If your close button is going to be text, this works very well for me:
#close {
position: fixed;
width: 70%; /* the width of the parent */
text-align: right;
}
#close span {
cursor: pointer;
}
Then your HTML
can just be:
<div id="close"><span id="x">X</span></div>