Since textarea element can't have a child like a button, you need to make a parent around the textarea which then could hold a button and a textarea. Then you want to make the botton absolute. It would work like the following example:
html:
<div class="wrapper">
<textarea></textarea>
<button></button>
</div>
css:
.wrapper {
position: relative;
width: max-content;
}
textarea {
width: 15rem;
height: 10rem;
}
button {
position: absolute;
right: 1rem;
bottom: 1rem;
width: 1rem;
height: 1rem;
}
the parents size is automatic the size of the child. So if you resize the textarea, the div will also be resize. Since the button is placed absolute at the bottom right corner of the div, it will alway move when you resize.
I've found a new and short option. If you do it like this it should work fine.
const textarea = document.querySelector("textarea");
const button = document.querySelector("button");
function setPosition() {
const left = textarea.offsetLeft;
const top = textarea.offsetTop;
const width = textarea.offsetWidth;
const height = textarea.offsetHeight;
button.setAttribute(
"style",
"position: absolute; top:" +
(top + height - 30) +
"px; left: " +
(left + width - 30) +
"px;"
);
}
setPosition();
new ResizeObserver(setPosition).observe(textarea);
Hope i could help!