I would like to have a horizontally scrollable column flex container with sticky buttons on the right. But when the items exceed their containers by different amounts, then one of the button gets detached from the right border after scrolling fully to the right.
It it possible to make this right – i.e. let the first button detach from the paragraph and stay on the right as it would if the container didn’t overflow?
(No javascript, pure css)
(Note the items could also have different margins or paddings on the left since I want to make this with a Tree structure)
* {
font-size: 30px;
}
.container {
display: flex;
flex-direction: column;
overflow-x: auto;
/* Enables horizontal scrolling when content overflows */
}
.item {
display: flex;
align-items: flex-start;
margin-bottom: 20px;
position: relative;
border: 3px solid plum;
}
.item p {
flex: 1;
margin: 0;
white-space: nowrap;
/* Prevents text from wrapping */
}
.item button {
position: sticky;
right: 0;
margin-left: 10px;
white-space: nowrap;
}
<!DOCTYPE html>
<html lang="en">
<body>
<div class="container">
<div class="item">
<p>First item text is a bit longer and continues without wrapping.</p>
<button>First Button</button>
</div>
<div class="item">
<p>Second item text is also extended to demonstrate horizontal scrolling.</p>
<button>Second Button</button>
</div>
</div>
</body>
</html>
2
Answers
Use a grid instead of flexbox on the container