简体   繁体   English

垂直对齐div和输入

[英]Vertically aligning a div and an input

After trying to get it right for hours I've finally found a way to align two div s perfectly on one line, but now I'm trying to get a button and an input box aligned and they're just slightly off. 在尝试将其正确对齐了几个小时之后,我终于找到了一种在一行上完美对齐两个div的方法,但是现在我试图将一个按钮和一个输入框对齐,它们只是稍微偏离了一点。 Note: I'm using Bootstrap with this! 注意:我正在为此使用Bootstrap!

排列不正确

Here's my code: 这是我的代码:

html: HTML:

<input id="col1" type="text" class="form-control" placeholder="Enter activity" /><button id="col2" class="btn btn-outline-success my-2 my-sm-0" type = "button">Add</button>

css: CSS:

body {
    margin: 10px;
}

#col1 {
    display: inline-block;
    width: 70%;
    height: 100px;

}

#col2 {
    display: inline-block;
    width: 30%;
    height: 100px;

}

button, input {
  height: 100%;
  width: 100%;
  line-height: 0;
  border: 0;
}

http://jsfiddle.net/x1hphsvb/335/ http://jsfiddle.net/x1hphsvb/335/

Any help is appreciated! 任何帮助表示赞赏!

You have a few more properties to zero out: 您还有其他一些属性可以归零:

button, input {
  ...
  border: 0;
  line-height: 0;
  vertical-align: top; /* or 'middle'; for Bootstrap */
}

Demo 演示

Just add vertical-align: baseline; 只需添加vertical-align: baseline; to the button's CSS. 按钮的CSS。 (The default vertical alignment for buttons in bootstrap is "middle"): (引导程序中按钮的默认垂直对齐方式是“中间”):

http://jsfiddle.net/q3aarre2/1/ http://jsfiddle.net/q3aarre2/1/

My dear friend, you just do it 我亲爱的朋友,你就去做

input, button {
   vertical-align:middle;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM