假设我不想使用Bootstrap,那么如何实现输入框与按钮的完美垂直对齐?当我尝试时,按钮会出现垂直错位。我不想使用“hack”来调整上边距以解决这个问题,因为我担心它在所有浏览器上都看起来不好。Bootstrap是如何实现这种神奇的效果的?
我的目标是实现以下效果:
我的目标是实现以下效果:
我认为答案是使用box-sizing: border-box
,就像Bootstrap一样。这适用于所有现代浏览器:
<input type="text" placeholder="Your text here">
<button>Button</button>
input{
float: left;
height: 30px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
button{
float: left;
height: 30px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
box-sizing
修复了在 Chrome 中对齐的元素在 Firefox 上偏移一个像素的问题。 - SexxLuthor<div class="container">
<input type="text" placeholder="Your text here">
<div class="button">
<a>Button</a>
</div>
</div>
.container{
display:inline-table;
vertical-align:middle;
}
input{
display:table-cell;
padding:5px;
}
.button{
display:table-cell;
background:gray;
padding:5px;
}
有一个具有明确高度的输入框和按钮。在它们之间需要添加注释以“连接”input
和button
,否则它们之间会有一个丑陋的空格。
.target {
height: 2em;
}
.target * {
height: 100%;
display:inline-block;
border:none;
outline:none;
}
.target input {
width:79%;
background-color:black;
padding-left: 5px;
}
.target button {
width:10%;
background-color: orange;
box-sizing: margin-box;
padding: 0; margin: 0;
border-top: 2px orange;
}
<div class="target">
<input placeholder="Foo" type="text"><!--
--><button>Bar</button>
</div>
vertical-align
怎么样? - Sebastian Simon