css 我怎样才能使我的搜索栏适合我的容器的结尾,同时又能响应?

jchrr9hc  于 2023-04-01  发布在  其他
关注(0)|答案(4)|浏览(109)

在我的网站中,我有一个容器和一个搜索栏来搜索项目,问题是我的搜索栏只走了一半,而不是到容器的末尾。我试图在“容器形式”中添加宽度:19%,它确实有效,除了当我缩小或拉伸屏幕时,它会从它里面移动出来。有人能帮我解决这个问题吗?

这是我的代码:

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>
wrrgggsh

wrrgggsh1#

虽然除此之外还有一些改进,但我所做的一个更改(解决了这个问题)是从以下声明中删除了position: absolute

.container form {
  border: 1px solid rgb(82, 74, 235);
  border-radius: 4px;
  display: flex;
  flex-direction: row;
  align-items: center;
  /* comment out, or remove, the following
     line:
  position: absolute; */
}

x一个一个一个一个x一个一个二个x

iaqfqrcu

iaqfqrcu2#

我通过将表单宽度设置为inherit来修复它,以便它采用包含父节元素的宽度。

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;
}

.myform{
  width:inherit;
}
<section class="container">

  <form class="myform">
    <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>
2jcobegt

2jcobegt3#

要使您的搜索栏适合容器的末尾并具有响应性,您可以使用以下CSS更改:
1.从.container form选择器中取出position: absolute;
1.将width: 100%;添加到.container form选择器。
1.将flex: 1;添加到.container form input选择器。
以下是更新后的CSS:

```css
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.);
  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;
  width: 100%; /* Add this line */
}

.container form input {
  border: none;
  outline: none;
  box-shadow: none;
  width: 100%;
  font-size: 16px;
  font-weight: 600;
  padding: 8px 10px;
  flex: 1; /* Add this line */
}

这些更改将使您的搜索栏适合容器宽度,并在调整屏幕大小时做出响应。
1cklez4t

1cklez4t4#

我修正了输入的宽度,只是给它的宽度375px,这是完全等于容器的宽度..这里是更新后的代码

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: 375px;** 
  font-size: 16px;
  font-weight: 600;
  padding: 8px 10px;
}

.search-item {
  padding:110px;
}

相关问题