【问题标题】:Responsive full width input with button带按钮的响应式全宽输入
【发布时间】:2016-10-23 23:27:12
【问题描述】:

我这里有小提琴:https://jsfiddle.net/ufLpqdtj/

我的问题是试图让我的搜索框和按钮始终全宽显示在页面上,而不管它在什么设备上运行。

在 Javascript 中,我总是可以将文本框的宽度设为 100% 减去按钮的像素宽度(按钮的大小始终相同),但我觉得我好像遗漏了一些东西,而且它可以在 CSS 中本地完成。

有什么想法或建议吗?

#commonSearchContainer {
  display: block;
  clear: both;
  width: 100%;
  position: relative;
}
#commonSearchTerm {
  width: 100%;
  margin: 25px 0px 0px 0px;
  padding: 5px;
  border: 1px solid #999999;
  height: 35px;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
.common-search-term-wrapper {
  width: 90%;
  display: inline-block;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
.common-search-button {
  background-color: #E9700D;
  border: 1px solid #ccc;
  display: inline-block;
  margin: 25px 0px 0px 10px;
  width: 80px;
  color: #fff;
  padding: 7px;
  font-style: italic;
  cursor: pointer;
}
<div id="searchSection" class="common-search-section">
  <div class="common-search-term-wrapper">
    <input id="commonSearchTerm" type="text" autocomplete="off" class="common-search-term">
  </div>
  <div id="commonSearchSubmit" class="common-search-button">
    Search
    <div style="clear: both;"></div>
  </div>
</div>

【问题讨论】:

    标签: html css


    【解决方案1】:

    我通常为这种布局做的是围绕元素创建一个父容器(就像你有的那样)并给它position: relativewidth: 100%

    然后我在内部元素上使用position: absolutedisplay: inline-block。设置固定大小元素的宽度,并使用leftright 定位所有元素。

    在你的情况下,它会是这样的:https://jsfiddle.net/ufLpqdtj/1/

    【讨论】:

    • 完美。把它钉在了头上。我很接近,只是有一些事情发生了。也感谢您的工作示例。
    【解决方案2】:

    您不应该将 div 用作按钮。有 html 元素。

    如果正确理解你想要达到的目标......

    form {
      width: 100%;
      display: inline-flex;
      justify-content: center;
      }
    
    #commonSearchTerm {
      width: 80%;
    }
    #searchButton {
      width: 80px;
      border-radius: 0;
      background-color: red;
      border: none;
      padding: 2px;
      color: white;
    }
    <form >
      <input id="commonSearchTerm" type="text" autocomplete="off" class="common-search-term">
      <input id="searchButton" type="submit">
    </form>

    这是使用 flexbox,在创建响应式内容时更加灵活。

    【讨论】:

    • 感谢您的回答,尽管我的要求有所不同。内联 flex 仅在 CSS3 中受支持,因此不适合,而且 div 按钮也是因为它是按钮的基线,其中包含图像和文本,以及其中的下拉选项。
    • @Nick 好吧,没有人会阻止您将图像添加到按钮并附加下拉菜单,最终它看起来会更干净。只是我的 2 美分。
    • 我接受了另一个答案,因为它获取了信息并提供了微小的更改来完成我需要的内容,而不是重建整个解决方案。当前代码是一个比传统提交按钮所能提供的更复杂的按钮的原型。您会发现构建自己的控件以满足不同要求是一种非常常见的做法。不过感谢您的解决方案!对我没有帮助的可能正是其他人需要的!
    猜你喜欢
    • 2015-01-27
    • 2018-09-20
    • 1970-01-01
    • 2013-11-13
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    相关资源
    最近更新 更多