How do I make a button position:sticky
stick to the top right at all times?
I tried:
div {
background-color: red;
overflow: auto;
height: 20rem;
width: 20rem;
}
input {
background-color: blue;
position: sticky;
top:0;
right:0;
}
article{
width:200%;
}
<div>
<input type="button" value="copy" />
<article>
In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available. In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available. In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available. In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available. In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available. In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available. In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available. In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available. In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available.
</article>
</div>
This works perfectly vertically but not horizontally
Note:
The width and height of the parent div can vary, it is set to 20rem
only for this example
3
Answers
One solution I found was to add another div as such:
It's similar to Dalton's solution except it works regardless of the text length inside the button
i.e. you don't have to guess the width of the button only the scrollbar
I'm still open to a pure css solution if anyone has one
You can make the button stick to the top right at all times by adding left: 90% to the input as somewhat explained in this answer solving a similar problem: https://stackoverflow.com/a/39226548/22822396
You can simply do
position:fixed;
in css property