首页 > 解决方案 > 有没有办法让输入框具有响应宽度,即使它是 flex 的孩子?

问题描述

我对使用 flexboxes 和网格很陌生,我不能完全掌握它,我正在尝试制作一个带有宽度响应搜索栏的导航栏。(有点像 Stack Overflow 在其导航栏上的那个)但由于某种原因,当我尝试width: 100%在搜索栏上使用时,它的宽度不会改变。我尝试查看无数教程和帖子,但似乎没有任何效果。Anyhelp将是惊人的

片段:

@import url('https://fonts.googleapis.com/css2?family=Ubuntu+Mono&display=swap');
* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

body {
  font-family: 'Ubuntu Mono', sans-serif;
  color: #333;
  line-height: 1.6;
}

ul {
  list-style-type: none;
}

a {
  text-decoration: none;
  color: #333;
}

h1,
h2 {
  font-weight: 300;
  line-height: 1.2;
  margin: 10px 0;
}

p {
  margin: 10px 0;
}

img {
  width: 100%;
}

.navbar {
  background-color: #F4F4F5;
  height: 70px;
}

.navbar ul {
  display: flex;
}

.navbar a {
  padding: 10px;
  margin: 0 5px;
}

.navbar a:hover {
  border-bottom: 2px #333 solid;
}

.navbar .flex {
  justify-content: space-between;
}

.container {
  max-width: 1100px;
  margin: 0 auto;
  overflow: auto;
  padding: 0 20px;
}

.flex {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

#searchbar {
  width: 100%;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" integrity="sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ==" crossorigin="anonymous" referrerpolicy="no-referrer"
  />
  <link rel="stylesheet" href="assets/css/style.css">
  <title>Website</title>
</head>

<body>
  <div class="navbar">
    <div class="container flex">
      <h2 class="logo">Website</h2>
      <nav>
        <ul>
          <form>
            <input id="searchbar" type="text" placeholder="Search.."></li>
          </form>
          <li><a href="#">About</a></li>
          <li><a href="#">Login</a></li>
          <button class="navbar-btn">Click me!</button>
        </ul>
      </nav>
    </div>
  </div>
</body>

</html>

标签: htmlcssflexbox

解决方案


尝试这个

.container nav {
  flex: 1;
}

.container form {
  width: 100%;
}

推荐阅读