One scenario I'm dealing with involves a container where users can input a value into an input field. As they type, an auto-complete box (an element with the z-index property) appears below the input. When the user hits enter, the input value is saved within a tag. For a quick example of the HTML and CSS, you can check out this codepen (same code provided below).
.main {
align-items: center;
display: flex;
height: 100vh;
justify-content: center;
width: 100vw;
}
.tag-container {
align-content: flex-start;
border: 1px solid black;
display: flex;
flex-direction: center;
flex-wrap: wrap;
gap: 5px;
overflow-y: scroll;
padding: 5px;
height: 70px;
width: 600px;
}
.tag {
display: flex;
justify-content: center;
align-items: center;
border: 1px solid black;
height: 30px;
width: 100px;
}
.input {
height: 25px;
width: 145px;
}
.auto-complete-box {
background: #F6F2F2;
border: 1px solid black;
position: absolute;
height: 150px;
text-align: center;
width: 150px;
z-index: 1;
}
<div class="main">
<div class="tag-container">
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class=input-container>
<input
class="input"
type="text"
placeholder="input"
/>
<div class="auto-complete-box">
Auto Complete Box
</div>
</div>
</div>
</div>
The issue arises when the container begins to scroll (after more tags are added) - the z-index element fails to follow it accordingly. This problem is illustrated in the following codepen example (with the same code provided below).
.main {
align-items: center;
display: flex;
height: 100vh;
justify-content: center;
width: 100vw;
}
.tag-container {
align-content: flex-start;
border: 1px solid black;
display: flex;
flex-direction: center;
flex-wrap: wrap;
gap: 5px;
overflow-y: scroll;
padding: 5px;
height: 70px;
width: 600px;
}
.tag {
display: flex;
justify-content: center;
align-items: center;
border: 1px solid black;
height: 30px;
width: 100px;
}
.input {
height: 25px;
width: 145px;
}
.auto-complete-box {
background: #F6F2F2;
border: 1px solid black;
position: absolute;
height: 150px;
text-align: center;
width: 150px;
z-index: 1;
}
<div class="main">
<div class="tag-container">
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class="tag">Tag</div>
<div class=input-container>
<input
class="input"
type="text"
placeholder="input"
/>
<div class="auto-complete-box">
Auto Complete Box
</div>
</div>
</div>
</div>
Is there a way to accomplish my goal using only CSS?
Appreciate any help!