div包裹input,不加定位会出现对不齐的问题,求原因

.inp_box{
    position: relative;
    width: 100px;
    height: 20px;
    margin:50px 0;
    background: red;
}
.inp_box1 input{
    position: absolute;
    top: 0;
    left: 0;
}
.inp_box input{
    height: 100%;
    width: 100%;
    border: none;
    outline: none;
}    
<div class="inp_box inp_box1">
    <input type="text">
</div>
<div class="inp_box">
    <input type="text">
</div>

结果:加了定位的input和div一样,不加会对不齐,为什么会出现这种情况?原因是啥?

clipboard.png

在线预览

阅读 3.1k
3 个回答

第一个问题 有默认1像素 还是2像素的 字体高度 所以对不齐 你给div加一个font-size:0 不加定位也可以对齐

第二个 那个就是你盒子的背景色 被后面元素压在上面 留下了一条背景色...

总结一下两位的答案
@XYShaoKang

@努力学会微笑

  1. div的line-height:0或font-size:0;
  2. input的vertical-align:top或bottom或super或text-top或text-bottom;
  3. 或者使用我的方法定位
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题