我尝试创建一个包含两个输入和一个按钮的表单。我宁愿使用SVG代替文本按钮,但它无法正确对齐。
假设以下CSS和HTML:
body {
background: #222;
margin: 25px;
}
form {
border: 1px solid #444;
padding: 2px;
}
input {
border: 1px solid transparent;
background: #444;
color: #ddd;
margin-left: 6px;
padding: 3px 5px;
}
input:focus {
outline: 0;
border: 1px solid #666;
}
button {
border: 0;
padding: 0;
background: transparent;
margin-left: 6px;
height: 23px;
width: 23px;
fill: #bbb;
}
button:focus {
outline: 0;
fill: #ddd;
}
<form>
<input type="email" id="email" />
<input type="password" id="password" />
<button type="submit">
<svg viewBox="0 0 1024 1024">
<path d="M384 512h-320v-128h320v-128l192 192-192 192zM1024 0v832l-384 192v-192h-384v-256h64v192h320v-576l256-128h-576v256h-64v-320z" />
</svg>
</button>
</form>
运行代码段时,输入字段在底部对齐,但是SVG按钮在顶部对齐。如果我将SVG替换为文本,则表示对齐。
答案 0 :(得分:1)
您需要将button
本身与input
对齐。我们在vertical-align: middle
中的每个元素上使用form
来达到相同的目标。
body {
background: #222;
margin: 25px;
}
form {
border: 1px solid #444;
padding: 2px;
}
form * {
vertical-align: middle;
}
input {
border: 1px solid transparent;
background: #444;
color: #ddd;
margin-left: 6px;
padding: 3px 5px;
}
input:focus {
outline: 0;
border: 1px solid #666;
}
button {
border: 0;
display: inline-block;
padding: 0;
background: transparent;
margin-left: 6px;
height: 23px;
width: 23px;
fill: #bbb;
}
button:focus {
outline: 0;
fill: #ddd;
}
<form>
<input type="email" id="email" />
<input type="password" id="password" />
<button type="submit">
<svg viewBox="0 0 1024 1024">
<path d="M384 512h-320v-128h320v-128l192 192-192 192zM1024 0v832l-384 192v-192h-384v-256h64v192h320v-576l256-128h-576v256h-64v-320z" />
</svg>
</button>
</form>
答案 1 :(得分:1)
只需在选择器中添加 vertical-align:middle; 。
body {
background: #222;
margin: 25px;
}
form {
border: 1px solid #444;
padding: 2px;
}
input {
border: 1px solid transparent;
background: #444;
color: #ddd;
margin-left: 6px;
padding: 3px 5px;
}
input:focus {
outline: 0;
border: 1px solid #666;
}
button {
border: 0;
padding: 0;
background: transparent;
margin-left: 6px;
height: 23px;
width: 23px;
fill: #bbb;
/*Just add this line to your target element to align in the middle*/
vertical-align: middle;
}
svg{margin:auto;}
button:focus {
outline: 0;
fill: #ddd;
}
<form>
<input type="email" id="email" />
<input type="password" id="password" />
<button type="submit">
<svg viewBox="0 0 1024 1024">
<path d="M384 512h-320v-128h320v-128l192 192-192 192zM1024 0v832l-384 192v-192h-384v-256h64v192h320v-576l256-128h-576v256h-64v-320z" />
</svg>
</button>
</form>