2017-10-28 159 views
0

我不明白为什么选择元素的宽度比输入元素的宽度短,我将它们都设置为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/

+0

你的小提琴与这里的源不一样;它不会设置例如“选择”宽度。 –

+0

添加:* {margin:0; padding:0; box-sizing:border-box} – VXp

回答

1

您已经申请了额外的衬垫input,但不是select。为了让他们等宽,你需要更新量的计算方法:

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

这里是解决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%,其输入的表弟..你的情况 一个

风格=“宽度: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> 
+0

您是否有此语句的来源? –

+0

你的建议是不正确的,因为差距的原因是不同的,所以风格的更新将打破你的解决方案 – Flying

+0

@NAVINROY尽管你粗鲁的话,你错了你的假设。你怎么计算出12px的差距等于10%?如果集装箱将更宽/更窄将会是什么? – Flying

0

试着改变你填充到填充顶和padding-底部。你在输入周围放置6个像素,这会使你的输入变大。为了阻止轮换,我们从左侧和右侧取下填充物。实质上,你说的是输入100%+ 6px + 6px,所以你的结果比选择的100%大。

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

填充可能是所需视觉外观的一部分。由于'box-sizing'造成的问题的原因是尺寸计算不正确 – Flying

+0

由于在上面的答案中添加了填充添加到上面的答案,所以尺寸计算已关闭 – Jonny

+0

您已经从输入中删除了水平填充,但您不知道如果它应该留下或不。所以你的解决方案仅限于一些特定的场景。 – Flying