In my website i have a container and a search bar to search items, the thing is my search bar only goes half way instead of to the end of the container. I tried adding width:19% in "container form" and it does work except when i shrink or stretch the screen it moves out of it. Can someone help me fix this issue?
This is my code:
body {
width: 100%;
height: 100vh;
background: rgb(240, 239, 243);
margin: auto;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.container {
width: 375px;
height: 520px;
background: white;
border-radius: 15px;
box-shadow: 4px 4px 30px rgb(0, 0, 0, 0.06);
padding: 20px;
overflow-y: scroll;
}
.container::-webkit-scrollbar {
display: none;
}
.container form {
border: 1px solid rgb(82, 74, 235);
border-radius: 4px;
display: flex;
flex-direction: row;
align-items: center;
position: absolute;
}
.container form input {
border: none;
outline: none;
box-shadow: none;
width: 100%;
font-size: 16px;
font-weight: 600;
padding: 8px 10px;
}
<section class="container">
<form>
<i class="fas fa-search"></i>
<input type="text" name="" id="search-item" placeholder="Search products" onkeyup="search()">
</form>
<div class="product-list" id="product-list">
<div class="product">
<img src="img/tshirt1.jpg" alt="">
<div class="p-details">
<h2>Black Tshirt</h2>
<h3>$25</h3>
</div>
</div>
</div>
</section>
4
Answers
While there are improvements that could be made in addition to this, the one change I made – which resolved the problem – was the removal of
position: absolute
from the following declaration:I managed to fix it by setting the form width to
inherit
so that it takes on the containing parent section element’s width.To make your search bar fit to the end of the container and be responsive, you can use the following CSS changes:
position: absolute;
from the.container form
selector.width: 100%;
to the.container form
selector.flex: 1;
to the.container form input
selector.Here’s the updated CSS:
These changes will make your search bar fit the container width and be responsive when resizing the screen.
I fixed the input width just by giving its width 375px which is exactly equal to container width..
Here is the updated code