使用css标签定位不起作用

时间:2013-01-30 11:56:12

标签: css html label

我在div中有一个标签。我想将标签放在div的中间,标签左侧的文字,垂直对齐到中心。如何在没有填充和边距的情况下执行此操作?

http://jsfiddle.net/E3Wb4/

<div class="center">    
    <label style="width:255px;height:40px;display:inline-block;float:center">Test</label>           
</div>


.center {
padding:0px;
background:#B3B3B3;
height:44px;
width:400px;
border-top:2px solid red;
border-bottom:2px solid red;
border-left:2px solid red;
border-right:2px solid red;
}

label{
    border-top:2px solid yellow;
    border-bottom:2px solid yellow;
    border-left:2px solid yellow;
    border-right:2px solid yellow;
}

3 个答案:

答案 0 :(得分:1)

你可以为div.center设置行高:44px

答案 1 :(得分:1)

最好的方法是将margin: 0px auto;添加到Label。

http://jsfiddle.net/WZ5MU/

您还可以使用border: 2px solid red;

来拍摄边框

border: <border-width> || <border-style> || <color>

答案 2 :(得分:0)

.center{
  position: relative;
}
label{
    position: absolute;
    margin: auto;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}

这是中心元素的好方法。