简体   繁体   English

Flex 搜索字段不会响应最小/最大宽度

[英]Flex search field won't respond to min/max-width

I have a text input field in my recreation of the Google homepage.我在重新创建 Google 主页时有一个文本输入字段。 I'm using a flexbox for everything on the line with the search field, and I'm trying to give the search bar a minimum width and a maximum width when resizing the page, but I cannot for the life of me make it responsive—it always defaults to the min- or max-width and stays that way regardless of how narrow the window is.我正在使用 flexbox 来处理与搜索字段相关的所有内容,并且我试图在调整页面大小时为搜索栏提供最小宽度和最大宽度,但我终其一生都无法让它响应——无论 window 有多窄,它始终默认为最小或最大宽度并保持这种状态。

Here's the search bar part of my HTML:这是我的 HTML 的搜索栏部分:

<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
      rel="stylesheet">
<link href="styles/style.css" rel="stylesheet" type="text/css">

  <form id="search-wrapper" name="search-form">
    <div id="searchbar">
      <span class="material-icons-outlined">search</span>
      <input id="search-field">
      <a href="#">
        <img id="search-microphone" src="images/search_mic.png" alt="">
      </a>
    </div>
  </form>

And the CSS: CSS:


:root {
    font-size: 10px;
}
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
html,
body {
    height: 100%;
}

#search-wrapper {
    margin: 10rem auto;
    display: flex;
    flex-direction: column;
    align-items: center;
}
#searchbar {
    display: flex;
    align-items: center;
    width: 70rem;
    min-width: 10rem;
    border: .1rem solid rgba(0,0,0,.3);
    border-radius: 3rem;
    margin: 0 2rem;
    padding: .75rem 1.25rem;
    font-family: "Material Icons";
    font-size: 1.8em;
    color: rgba(0,0,0,.3);
}
#search-field {
    width: 100%;
    margin: 0 1.5rem;
    border: none;
    font-family: Helvetica, sans-serif;
}
#search-microphone {
    width: 2.2rem;
    height: 2.2rem;
}

And if it helps, here's a CodePen with a little more.如果它有帮助,这里还有一个 CodePen

I've been scouring StackOverflow looking at questions that seem like duplicates, but have thus far been unable to find a solution that works.我一直在搜索 StackOverflow,寻找看似重复的问题,但迄今为止无法找到有效的解决方案。 I have tried every combination of width , min-width , and max-width I can think of between the container and input field child, such as:我已经尝试了在容器和输入字段子项之间我能想到的所有widthmin-widthmax-width组合,例如:

  • setting max-width: 70rem on the container, then width: 100% and min-width: 40rem on the child在容器上设置max-width: 70rem ,然后在子级上设置width: 100%min-width: 40rem
  • setting max-width: 70rem on the container, then just min-width: 40rem on the child在容器上设置max-width: 70rem ,然后在子级上设置min-width: 40rem
  • setting width: 70rem and min-width: 40rem on the container, then width: 100% on the child (as well as the opposite with max-width )在容器上设置width: 70remmin-width: 40rem ,然后在子级上设置width: 100% (以及与max-width相反)
  • setting max-width: 70rem and min-width: 40rem on the container, then width: 100% on the child在容器上设置max-width: 70remmin-width: 40rem ,然后在子级上设置width: 100%
  • setting no width on the container, then min-width: 40rem and max-width: 70rem on the child在容器上不设置宽度,然后在子级上设置min-width: 40remmax-width: 70rem
  • setting no width on the container, then width: 70rem and min-width: 40rem on the child (as well the opposite with max-width )在容器上不设置宽度,然后在子级上设置width: 70remmin-width: 40rem (以及与max-width相反)
  • setting min-width: 40rem and max-width: 70rem on the parent, then setting the child to flex: 1 and the other two children in the container to flex: 0在父节点上设置min-width: 40remmax-width: 70rem ,然后将子节点设置为flex: 1并将容器中的其他两个子节点设置为flex: 0
  • setting the child to display: block and playing around with various combinations listed above将孩子设置为display: block和玩耍

No matter what I try, the search field always displays at either 70rem or 40rem and does not grow or shrink at all.无论我尝试什么,搜索字段始终显示为 70rem 或 40rem,并且根本不会增长或缩小。 I usually have no trouble making flex stuff responsive;我通常可以毫不费力地让 flex 的东西做出响应; can anyone see what I'm missing?谁能看到我错过了什么?

Thanks to Hilal Arsa's comment for steering me in the direction that finally solved it.感谢 Hilal Arsa 的评论引导我朝着最终解决它的方向前进。

Including width: 100% on #search-wrapper , #searchbar , and #search-field PLUS min-width: 40rem and max-width: 70rem on #searchbar is what finally did it:包括width: 100% on #search-wrapper#searchbar#search-field PLUS min-width: 40remmax-width: 70rem on #searchbar是最终做到了:

#search-wrapper {
    margin: 10rem auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;                    /* added this */
}
#searchbar {
    display: flex;
    align-items: center;
    width: 100%;                    /* here */
    min-width: 40rem;               /* here */
    max-width: 60rem;               /* here */
    border: .1rem solid rgba(0,0,0,.3);
    border-radius: 3rem;
    margin: 0 2rem;
    padding: .75rem 1.25rem;
    font-family: "Material Icons";
    font-size: 1.8em;
    color: rgba(0,0,0,.3);
}
#search-field {
    width: 100%;                    /* and here */
    margin: 0 1.5rem;
    border: none;
    font-family: Helvetica, sans-serif;
}

In my opinion, because you are using a http link to connect the css it overlays with your personal stylesheet.在我看来,因为您使用 http 链接来连接 css,它会覆盖您的个人样式表。 I had similar issue few weeks ago and a simple solution solve my problem.几周前我遇到了类似的问题,一个简单的解决方案解决了我的问题。 So, on the place that you think that your code can't be read(in your case the width), simply add;important.因此,在您认为无法读取代码的地方(在您的情况下为宽度),只需添加;重要。 After the styling.造型后。

So in example:所以在例子中:

#searchbar { #搜索栏 {

width: 70rem important! ;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM