【问题标题】:How do I add a pixel of space between my inputs?如何在输入之间添加一个像素空间?
【发布时间】:2017-02-16 21:36:40
【问题描述】:

我在搜索表单中有这些字段……

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

那么我有这种样式的表单......

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

问题是,元素紧挨着出现——https://jsfiddle.net/fmy1syfw/。我希望它们之间至少有一个像素的空间,以免让一切看起来如此狭窄。我已经尝试添加

margin: 1px;

到上面的风格,但无济于事。如何在我的元素之间添加一个像素空间而不破坏表单在不同屏幕宽度下的行为?

【问题讨论】:

  • #search-form input { }留边距...jsfiddle.net/fmy1syfw/1
  • 不幸的是,这破坏了一切。用你的 Fiddle 将屏幕压缩到小于 680 像素,并将其与压缩我的小于 680 像素进行比较。事情不再一样了。
  • 那是因为你设置的是固定宽度,你可以用flex-grow代替jsfiddle.net/fmy1syfw/3
  • 对 flex 值、最大宽度和边距进行了一些修复,这是否是预期的结果:jsfiddle.net/fmy1syfw/5jsfiddle.net/fmy1syfw/6

标签: html css flexbox margin display


【解决方案1】:

只需根据需要为输入添加边距,然后在媒体查询中将其删除(同样,根据需要)。

由于您在媒体查询中使用了 50% 的固定宽度,因此必须使用 calc 调整这些宽度以考虑额外的边距。

body {
  background-color: grey;
}
#logo {
  margin: 0 auto;
  padding: 10px;
}
#searchForm {
  padding: 20px;
}
#search-form {
  background-color: orange;
  display: flex;
  flex: 1 0 auto;
}
#last_name,
#event {
  margin-left: 1px;
}
#first_name,
#last_name {
  width: 20%;
}
#event {
  flex-grow: 1;
}
/* Do not specify width to allow it to grow freely */

.search_button {
  width: 40px;
  height: 40px;
}
/* Predefine image dimensions to ensure proper aspect ratio */

#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 {
    width: 50%;
    margin: 0;
  }
  #last_name {
    width: calc(50% - 1px);
    margin-left: 1px;
  }
  #event {
    width: calc(100% - 40px);
    margin: 0;
  }
}
.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>

JSFiddle Demo

【讨论】:

  • 谢谢,但我也想在小屏幕上的元素之间留出一个像素的空间。我尝试将媒体查询中的“margin:0”增加到“margin:1px”,但这会破坏小屏幕上的所有内容。
  • 那么您不能使用 50% 的固定宽度 - jsfiddle.net/tbausb1g 您必须使用 calc 进行调整。
猜你喜欢
  • 2021-06-15
  • 2010-11-14
  • 2017-06-06
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
  • 1970-01-01
  • 2021-10-08
  • 1970-01-01
相关资源
最近更新 更多