The following HTML shows a scrollable resizable div with a button and a further content div in it.
<!DOCTYPE html>
<html>
<head>
<style>
.container {
width: 100%;
max-width: 100%;
height: 150px;
position: relative;
overflow: auto;
resize: both;
white-space: nowrap;
background: lightgray;
}
.container .fixed {
position: absolute;
right: 5px;
top: 5px;
}
</style>
</head>
<body>
<div class="container">
<button class="fixed">Test</button>
<div class="content">
texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttext
</div>
</div>
</body>
</html>
The button should be fixed at the top right corner in the div. This works fine for resizing, however, when I scroll, the button moves too. How can I fix this?
3
Answers
In the
.container
selector, you haveoverflow: auto;
. Moveoverflow: auto;
to a selector that targets the.content
class only:We only select
.content
to only add a scrollbar to that div.You could do the following to keep the sizing of the content div and keep the button independent. Leveraging the z-index means you can keep the button’s visibility, too.
You should move the style
overflow: auto
inside thecontent
class.Also, adjust the
content
class to have a height of100%
if you intend to scroll inside container div, if not you can removeheight: 100%
and scroll within the content itself.