【问题标题】:Input element width is shorter than select element width [duplicate]输入元素宽度小于选择元素宽度[重复]
【发布时间】:2017-10-28 16:46:27
【问题描述】:

我不明白为什么select元素的宽度比输入元素的宽度短,我都设置为100%,但是输入似乎由于某种原因覆盖了网格边界。

HTML:

<div class="gridView">
  <input type="text"   placeholder="Name" />
  <input type="text"  placeholder="Last Name" />
  <select></select>
  <input type="text"  placeholder="Birth Date" />
  <select> </select>
  <select> </select>
  <input type="text" placeholder="Passport Number" />
  <span>
  <input  placeholder="Passport Expiry Date"/>
  <span class="dateFormat">YYYY-MM-DD</span>
  </span>  
</div>

CSS:

input {
  width: 100%;
  padding: 6px;
  height: 25px;
}
select {
  height: 41px;
  width:100%;
}
.gridView {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-column-gap: 25px;
  grid-row-gap: 10px;
  font-family: sans-serif;
  margin: auto;
  width: 50%;
}
.dateFormat {
  color: red;
  font-size: small;
  display: block;
  margin-left: 15px;
  margin-bottom: 5px;
  font-weight: bold;
}

JSFIDDLE
https://jsfiddle.net/AlexLavriv/oxmq71sn/1/

【问题讨论】:

  • 你的fiddle和这里的源码不一样;例如,它没有设置 select 宽度。
  • 添加:* {margin:0;padding:0;box-sizing:border-box}

标签: html css


【解决方案1】:

您为input 应用了额外的填充,但没有为select 应用。要使它们的宽度相等,您需要更新尺寸计算方法:

input, select {
  width: 100%;
  box-sizing: border-box;
}

【讨论】:

    【解决方案2】:

    尝试将填充更改为填充顶部和填充底部。您在输入周围放置了 6 px,这使您的输入更大。为了停止移位,我们将填充物从左右两侧移开。本质上,您是说输入 100%+6px+6px,因此您的结果大于为选择设置的 100%。

    input {
    width: 100%;
    padding-top: 6px;
    padding-bottom: 6px;
    height: 25px;
    }
    

    【讨论】:

    • 填充可能是所需视觉外观的一部分。问题的原因是由于box-sizing 导致的尺寸计算不正确
    • 尺寸计算已关闭,因为我在上面的答案中添加了填充,希望对您有所帮助
    • 您已经从输入中删除了水平填充,但您不知道它是否应该保留。因此,您的解决方案仅限于某些特定场景。
    • 请解释一下,据我所知,这适用于所有场景?可以举个例子吗?
    • 当然。想象一下,您必须保留这些水平填充(例如,由于设计要求)。在这种情况下,您提出的删除它们的解决方案将破坏项目要求。这意味着您的解决方案并非适用于所有情况,而仅适用于某些特定情况。大小差异的根本原因不是填充本身,而是浏览器计算元素大小的方式。它由box-sizing 样式控制,默认为content-box 并且不计算导致问题的填充。
    【解决方案3】:

    这是已解决的Fiddle

    .registerForm {
        font-family: sans-serif;
        margin: auto;
        width: 50%;
    }
    input {
        width: 100%;
        padding: 6px;
        height: 25px;
    }
    
      
    
    select {
        height: 41px;
         width: 110%;
    }
    .gridView {
        display: grid;
        grid-template-columns: 1fr 1fr;
        grid-column-gap: 25px;
        grid-row-gap: 10px;
    }
    .dateFormat {
        color: red;
        font-size: small;
        display: block;
        margin-left: 15px;
        margin-bottom: 5px;
        font-weight: bold;
    }
    <div class="registerForm">
       <div class="gridView">
          <input type="text"   placeholder="Name" />
          <input type="text"  placeholder="Last Name" />
          <select></select>
          <input type="text"  placeholder="Birth Date" />
          <select> </select>
          <select> </select>
          <input type="text" placeholder="Passport Number" />
          <span>
          <input  placeholder="Passport Expiry Date"/>
          <span class="dateFormat">YYYY-MM-DD</span>
          </span>
       </div>
    </div>

    实际上,默认情况下,选择宽度比其输入表亲小 10%.. 在你的情况下

    style="width:110%;

    填补空白

    <div class="registerForm">
       <div class="gridView">
          <input type="text"   placeholder="Name" />
          <input type="text"  placeholder="Last Name" />
          <select   style="width:110%;"></select>
          <input type="text"  placeholder="Birth Date" />
          <select   style="width:110%;"> </select>
          <select   style="width:110%;"> </select>
          <input type="text" placeholder="Passport Number" />
          <span>
          <input  placeholder="Passport Expiry Date"/>
          <span class="dateFormat">YYYY-MM-DD</span>
          </span>
       </div>
    </div>
    

    【讨论】:

    • 你有这个声明的一些来源吗?
    • 您的建议不正确,因为差距的原因不同,所以更新样式会破坏您的解决方案
    • @NAVINROY 尽管您的言辞粗鲁,但您的假设是错误的。你是如何计算出 12px 的差异等于 10% 的?如果容器变宽/变窄会怎样?
    • @AlexLavriv 这里是已解决的fiddle
    猜你喜欢
    • 2021-11-02
    • 2012-09-18
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 2013-10-27
    相关资源
    最近更新 更多