如何告诉HTML,让两个元素占据剩余可用宽度的全部空间?

7
我有三个输入框和一个搜索图形在一个较大的DIV中。
<div style="vertical-text-align:center;">
<form id="search-form" action="//search" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓">
    <input type="text" name="first_name" id="first_name" placeholder="First Name" class="searchField" style="width:25%; min-width: 100px;">
    <input type="text" name="last_name" id="last_name" placeholder="Last Name" class="searchField" style="width:25%; min-width: 100px;">
    <input type="text" name="event" id="event" placeholder="Event" class="searchField" style="width: 40%; min-width:100px;">
    <input alt="Search" type="image" src="http://www.racertracks.com/assets/magnifying-glass-0220f37269f90a370c3bb60229240f2ef2a4e15b335cd42e64563ba65e4f22e4.png" class="search_button" height="40" align="middle">
</form> </div>

我希望第三个搜索字段和放大镜图标占据剩余的宽度,因为当我尝试指定宽度时,就像在这个Fiddle中一样——https://jsfiddle.net/stndbt2u/2/,即使有足够的空间显示全部内容,放大镜图形也会换到下一行。如何保持放大镜和第三个搜索字段始终在同一行并占用其余的可用宽度?请注意,如果父容器的最大宽度小于580像素,则第三个搜索字段和放大镜会换到下一行,这是可以接受的,也是更好的做法。
4个回答

5
如果你愿意接受仅使用CSS的解决方案,并且可以牺牲一些与旧浏览器的兼容性,那么CSS flexbox方案是最好的选择。基本上,我们将设置两种情况:
A:当视口大于620px时
计算:登录表单的最大宽度为580px,左右padding各20px。
我们允许#first_name和#last_name的宽度为20%,允许#event增长以填充剩余空间,并使.search_button具有40x40像素的固定尺寸。
这意味着以下规则将起作用:
#search-form {
  display: flex;  /* Enable flexbox */
  flex: 1 0 auto; /* Breakdown:
                     flex-grow: 1 (allow elements to grow)
                     flex-shrink: 0 (prevent elements from collapsing)
                     flex-basis: auto (allow width to determine element dimensions)
                  */
}

#first_name, #last_name { width: 20%; }
#event { } /* Do not specify width to allow it to grow freely */
.search_button { width: 40px; height: 40px; } /* Predefine image dimensions to ensure proper aspect ratio */

B: 当视口小于620px时

和上面的计算相同。

Flexbox默认不换行,试图将元素放在一行上。但在窄视口场景下,我们不希望出现这种行为,因为您要求将表单分成多行。因此,使用flex-wrap:wrap将强制换行(在某种意义上进行断行)。

我们仍然希望#first_name#last_name占据全部宽度。我们可以简单地使用width: 50%,使它们加起来等于100%。由于启用了换行,请确保它们的总和不超过100%。如果您正在添加边框(而不在输入元素上使用box-sizing:border-box;)和/或边距,则需要使用width: calc(50% - x)来确保处理这些额外空间。

在第二行中,我们有#events.search_button。我们仍然希望将.search_button保持为40像素宽,但希望#events扩展以填充第二行的所有空间。这可以通过在#events上声明width: calc(100% - 40px)来实现。

记得将所有这些内容包装在一个@media查询中,并将max-width设置为620px:

@media (max-width: 620px) {
  #search-form {
    flex-wrap: wrap;
  }

  #first_name, #last_name { width: 50%; }
  #event { width: calc(100% - 40px); }
}

请查看此链接以获得概念验证代码示例:https://jsfiddle.net/teddyrised/382fhxpc/。请注意,我已删除display: table和其他内联CSS样式。尽可能避免使用内联CSS。

我还嵌入了一个代码片段示例:

body {
  background-color:grey;
}

#logo {
  margin: 0 auto;
  padding: 10px;
}

#searchForm {
  padding: 20px;
}

#search-form {
  display: flex;
  flex: 1 0 auto;
}

#first_name, #last_name { width: 20%; }
#event { } /* Do not specify width to allow it to grow freely */
.search_button { width: 40px; height: 40px; } /* Predefine image dimensions to ensure proper aspect ratio */

#loginLogos {
  margin: 0 auto;
  padding: 20px;
}

#loginArea {
  border-radius: 25px;
  font-size: 20px;
  padding: 20px;
  background-color: #ffffff;
  color: #000000;
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translateX(-50%) translateY(-50%);
          transform: translateX(-50%) translateY(-50%);
  width: 100%;
  max-width: 580px;
}

@media (max-width: 620px) {
  #search-form {
    flex-wrap: wrap;
  }
  
  #first_name, #last_name { width: 50%; }
  #event { width: calc(100% - 40px); }
}
.searchField {
  line-height: 40px;
  font-size: 22px;
  margin: 0;
  padding: 5px;
  border: 1px solid #ccc;
  box-sizing: border-box;
  -webkit-appearance: textfield;
  background-color: white;
  -webkit-rtl-ordering: logical;
  -webkit-user-select: text;
  letter-spacing: normal;
  word-spacing: normal;
  text-transform: none;
  text-indent: 0px;
  text-shadow: none;
  text-align: start;
}
<div id="loginArea">
  <div id="searchForm">
    Search For Results
    <br />
    <div>
      <form id="search-form" action="/events/search" accept-charset="UTF-8" method="get">
        <input name="utf8" type="hidden" value="✓">
        <input type="text" name="first_name" id="first_name" placeholder="First Name" class="searchField">
        <input type="text" name="last_name" id="last_name" placeholder="Last Name" class="searchField">
        <input type="text" name="event" id="event" placeholder="Event" class="searchField">
        <input alt="Search" type="image" src="http://www.racertracks.com/assets/magnifying-glass-0220f37269f90a370c3bb60229240f2ef2a4e15b335cd42e64563ba65e4f22e4.png" class="search_button">
      </form>
    </div>
  </div>
</div>


嗨,这在620像素以下看起来非常好 - 正是我想要的,但如果它超过620像素,是否有任何方法使事件字段和放大镜占用其余可用空间?我更新了Fiddle,您可以看到仍然有一些橙色空间 - https://jsfiddle.net/382fhxpc/1/,我希望文本字段填满它。 - Dave
1
只需将 #event 添加 flex-grow: 1 即可使其填充剩余空间 https://jsfiddle.net/fmy1syfw/. - David Mann
@Dave,我似乎无法重现你的问题(http://imgur.com/jtj7a3e),因为在父元素中已经将`flex-grow`设置为`1`,即在`flex: 1 0 auto;中声明了flex缩写。但是,如果将事件字段特别设置为flex-grow: 1`可以解决你面临的问题,那就继续吧 :) - Terry

2
解决方案:https://jsfiddle.net/stndbt2u/5/ 只需要在输入框中添加float: left,并将图片的最大宽度设置为10%(因为您已经有25+25+40)。 如果您希望在表单宽度不足时,第三个输入框和图像转到下一行,请在媒体查询中将clear: left添加到该第三个输入框。
以下是代码:
#search-form input{
  float:left;
}
input.search_button {
  max-width:10%;
}
@media (max-width: 580px) {
  input#event {
    clear:left;
  }
}

1
使用Chrome的“检查”功能检查Fiddle页面后,发现父容器(具有id =“search_form”的表单标签)的宽度为540像素,而不是580像素。

enter image description here

此外,似乎在每个输入文本元素之间添加了额外的间距,以在这些元素之间再添加6个像素的间距。

enter image description here

将两个名称输入框的宽度相加 (2 X 135),再加上事件输入框 (216),放大镜图标 (40) 和这些元素之间的额外间距 (3 X 6),总共为544像素。由于此宽度大于父容器的宽度,最后一个元素 (放大镜图标) 被推到了下一行。

我对 input 元素的类定义中使用的 display:table-cell CSS 表示怀疑。相反,尝试使用 display:inline-block,看看是否可以减少输入元素之间的间距。

如果这样做不起作用,则需要进行其他宽度调整,以使父容器能够在单行上容纳所有这些元素。


1

您需要将所有输入框放在一个包装器中(相对位置),并以百分比设置宽度,并为其设置绝对位置的玻璃。

body{
  background:#ccc;
}
.parent-wrapper{
  float:left;
  width:100%;
  text-align:center;
}
#searchForm, #searchForm *{
  box-sizing: border-box;
  outline:none;
}
#searchForm{
  border-radius:5px;
  background:#FFF;
  padding:20px;
  box-sizing:border-box;
  max-width:580px;
  display:inline-block;
}
#searchForm form{
  width:100%;
  position:relative;
}
#searchForm .input{
 padding:0 5px;
  float:left;
  width:25%;
}
#searchForm .search-wrapper{
  width:50%;
  float:left;
  padding-right:50px;
}
#searchForm .search-wrapper .input{
 width:100%;
}
#searchForm .searchField{
  float:left;
  border-radius:5px;
  border:1px solid #ccc;
  padding:0 10px;
  height:40px;
  width:100%;
}
#searchForm .search_button{
  position:absolute;
  right:0;
}
@media (max-width: 400px) {
 #searchForm .search-wrapper,#searchForm .search-wrapper .input {
    width:100%;
  }
  #searchForm form{
    padding-right:0;
  }
  #searchForm .input {
    width:50%;
  }
  #searchForm .search-wrapper{
    margin-top:10px;
  }
}
<div class="parent-wrapper">
<div id="searchForm">
 Search For Results<br> 
 <form id="search-form" action="/events/search" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓">
        <div class="input">
  <input type="text" name="first_name" id="first_name" placeholder="First Name" class="searchField">
        </div>
      <div class="input">
  <input type="text" name="last_name" id="last_name" placeholder="Last Name" class="searchField">
       </div>
       <div class="search-wrapper">
       <div class="input">
  <input type="text" name="event" id="event" placeholder="Event" class="searchField">
        </div>
  <input alt="Search" type="image" src="http://www.racertracks.com/assets/magnifying-glass-0220f37269f90a370c3bb60229240f2ef2a4e15b335cd42e64563ba65e4f22e4.png" class="search_button" height="40" align="middle">
     </div>
      <div style="clear:both;"></div>
</form>
 </div>
  </div>

并且像在Bootstrap中一样使用列来包装输入框,并添加box-sizing属性。最好只需检查并学习我提供的样式。


我用你的代码构建了一个JSFiddle -- https://jsfiddle.net/stndbt2u/3/,但存在一些问题。首先,搜索区域不再位于屏幕中央。其次,如果我压缩屏幕大小,使其宽度像手机屏幕一样小,第三个搜索框和放大镜图标不会换行,这应该是在屏幕宽度小于580像素时应该发生的事情。 - Dave
谢谢。我将您的代码插入了一个Fiddle - https://jsfiddle.net/stndbt2u/4/。我注意到您将每个输入都设置为33%,而以前我的名字和姓氏是25%,然后希望用其余空间填充(如果有空间)。您的示例是否有任何控制宽度的方法,或者只能使用每个输入都是33%宽度? - Dave
我编辑了片段-将前两个字段设为25%,现在您可以看到它是什么样子。 我认为这是我在这种情况下能提出的最好的建议:媒体查询可以按您的意愿进行切换。干杯! - user3455726
看起来不错,但当我运行你的代码片段时,搜索框不再垂直居中。 - Dave

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接