考虑以下示例
https://jsfiddle.net/gq5bru8d/1/
* {
box-sizing: border-box; }
input, a {
background-color: red;
color: white;
border: .1rem solid black;
padding: 1rem;
font-size: 1rem;
line-height: 1rem; }
a {
text-decoration: none; }
<input type="text" placeholder="Input">
<a href="#">Anchor</a>
尽管input和anchor标记具有相同的font-size
,line-height
,padding
和border
,但输入的高度似乎比锚标记高1px。为什么这样,你怎么能这样做,所以他们都有相同的尺寸?
答案 0 :(得分:3)
将它们包裹在flex div中。
* {
box-sizing: border-box; }
input, a {
background-color: red;
color: white;
border: .1rem solid black;
padding: 1rem;
font-size: 1rem;
line-height: 1rem; }
a {
text-decoration: none;
margin-left: 5px;
}
&#13;
<div style="display: flex;">
<input type="text" placeholder="Input">
<a href="#">Anchor</a>
</div>
&#13;
另一种解决方案。将此样式添加到元素font: 13.3333px Arial
* {
box-sizing: border-box; }
input, a {
background-color: red;
color: white;
border: .1rem solid black;
padding: 1rem;
font-size: 1rem;
line-height: 1rem;
font: 13.3333px Arial;
}
a {
text-decoration: none;
margin-left: 5px;
}
&#13;
<input type="text" placeholder="Input">
<a href="#">Anchor</a>
&#13;
答案 1 :(得分:1)
如果您不介意使用flex在输入和锚点周围使用包装,并且设置为
显示:挠曲
它会使它们的高度相同。
* {
box-sizing: border-box; }
.wrapper{display:flex}
input, a {
background-color: red;
color: white;
border: .1rem solid black;
padding: 1rem;
font-size: 1rem;
line-height: 1rem; }
a {
text-decoration: none;
margin-left: 5px;
}
<div class="wrapper">
<input type="text" placeholder="Input">
<a href="#">Anchor</a>
</div>
答案 2 :(得分:1)
这有几个原因:
* {
box-sizing: border-box;
}
input, a {
background-color: red;
color: white;
border: .1rem solid black;
/* Remove the top and bottom padding and replace with line height */
padding: 0 1rem;
line-height: 3rem;
/* Set both elements' display property to be inline-block */
display: inline-block;
font-size: 1rem;
/* Fix the font family */
font-family: Arial, sans-serif;
/* Fix the line height */
line-height: 3rem;
/* Set the vertical alignment for both elements */
vertical-align: center;
}
a {
text-decoration: none;
}
<input type="text" placeholder="Input">
<a href="#">Anchor</a>
答案 3 :(得分:0)
将此添加到您的a
和input
代码
display: inline-block;
vertical-align: middle;
* {
box-sizing: border-box;
}
input,
a {
background-color: red;
color: white;
border: .1rem solid black;
padding: 1rem;
font-size: 1rem;
line-height: 1rem;
display: inline-block;
vertical-align: middle;
}
a {
text-decoration: none;
}
<input type="text" placeholder="Input">
<a href="#">Anchor</a>
答案 4 :(得分:0)
检查输入和锚标记的字体系列。 默认情况下,输入在您的示例中具有一些字体样式属性。
将%example
更改为:
%example {
background-color: red;
color: white;
border: .1rem solid black;
padding: 1rem;
font-size: 1rem;
line-height: 1rem;
font-family: Arial;
}
在这里,我只向css添加了font-family: Arial;
。
答案 5 :(得分:0)
要明确设置元素height
并进行以下更改,请执行以下操作:
%example {
background-color: red;
color: white;
border: .1rem solid black;
padding: 1rem;
font-size: 1rem;
line-height: 1rem;
height: 50px; // Keyline
display: inline-block; // without this line height not apply properly
vertical-align: top; // This line making alignment perfectly
}
答案 6 :(得分:0)
尝试使用此...
* {
box-sizing: border-box; }
input, a {
background-color: red;
color: white;
border: .1rem solid black;
padding: 1rem;
font: 400 14.3333px Arial;
line-height: 1rem; }
a {
text-decoration: none; }
答案 7 :(得分:-1)
这种情况正在发生,因为您的a
仍然是内嵌的。将其设置为显示为内联块允许line-height
属性通过。您还需要确保它们都在同一个vertical-align
:
* {
box-sizing: border-box;
}
input, a {
background-color: red;
color: white;
border: .1rem solid black;
padding: 1rem;
font-size: 1rem;
line-height: 1rem;
vertical-align: top;
}
a {
display: inline-block;
text-decoration: none;
}
<input type="text" placeholder="Input">
<a href="#">Anchor</a>