a元素和按钮元素的垂直位置

时间:2014-11-13 11:06:51

标签: html css position

我想将<button><a>元素设置为相同的格式。我使用以下代码:

button,
a {
  border: solid 1px black;
  background-color: white;
  color: black;
  font-family: 'Arial';
  padding: 0 15px;
  font-size: 13px;
  height: 35px;
  line-height: 35px;
  display: inline-block;
}
#dummy-block {
  background-color: black;
  padding: 0;
  margin: 0;
  height: 20px;
}
<div id="dummy-block"></div>
<button>My Button</button>
<a>My Link</a>

但是<button>元素似乎忽略了高度而我的<a>元素没有触及上面的黑色虚拟<div>的边缘:

enter image description here

您可以在我的小提琴中测试代码:http://jsfiddle.net/gyrrcrqc/1/

4 个答案:

答案 0 :(得分:3)

试试这个: -

button, a {
    background-color: white;
    border: medium none;
    box-shadow: 0 0 1px #000 inset;
    color: black;
    display: inline-block;
    font-family: "Arial";
    font-size: 13px;
    height: 35px;
    line-height: 35px;
    padding: 0 15px;
    vertical-align: top;
}

或者: -

button, a {
    background-color: white;
    border: medium none;
    vertical-align:top;
    color: black;
    display: inline-block;
    font-family: "Arial";
    font-size: 13px;
    height: 35px;
    line-height: 35px;
    padding: 0 15px;
    border:1px solid #000;
    box-sizing:border-box
}

<强> DEMO2

DEMO

答案 1 :(得分:3)

显然,按钮的默认大小调整方法是border-box,而内联块的默认大小调整方法是content-box。所以:

  • 35px高意味着<a>实际上是37px高(边框 添加 2px)
  • 35px高度表示<button>标记高35像素(35px 包含 边框)

在两个元素上设置box-sizing: border-box

&#13;
&#13;
button,
a {
  border: solid 1px black;
  background-color: white;
  color: black;
  font-family: 'Arial';
  padding: 0 15px;
  font-size: 13px;
  height: 35px;
  line-height: 35px;
  display: inline-block;
  box-sizing: border-box;
}
#dummy-block {
  background-color: black;
  padding: 0;
  margin: 0;
  height: 20px;
}
&#13;
<div id="dummy-block"></div>
<button>My Button</button>
<a>My Link</a>
&#13;
&#13;
&#13;

答案 2 :(得分:2)

尝试将vertical-align: bottom添加到button, a选择器

button, a
{
    border: solid 1px black;
    background-color: white;
    color: black;
    font-family: 'Arial';
    padding: 0 15px;
    font-size: 13px;
    height: 35px;
    line-height: 35px;
    display: inline-block;
    vertical-align: top;
}

#dummy-block
{
    background-color: black;
    padding: 0;
    margin: 0;
    height: 20px;
}
<div id="dummy-block"></div>

<button>Okay</button>
<a>Edit</a>

答案 3 :(得分:1)

    a
    {
        padding:1px 15px;
    }
    button,a
    {
        border: solid 1px black;
        background-color: white;
        color: black;
        font-family: 'Arial';

        font-size: 13px;
        height: 35px;
        line-height: 35px;
        display: inline-block;

    }
    button
    {
        padding: 0 15px;    
    }