内联块div元素的中心内容

时间:2015-03-25 08:59:29

标签: html css alignment

我尝试在CSS / HTML中执行类似的操作:

example

每个“Box”应该是指向错误详细信息的链接。 文本和框内的红色div应水平居中,如图所示。

这是我的代码:JSFiddle

.outerDiv {
    display: inline-block;
    border-radius: 10px;
    border: 2px solid #c3c3c3;
    height: 100px;
    width: 100px;
    margin: 5px;
}

.innerDiv {
    width: 50%;
    height: 100%;
    margin: 0 auto;
}

.errorLabel {    
    background-color: #a90329;
    display: inline;
    padding: .2em .6em .3em;
    font-size: 75%;
    color: #fff;
    border-radius: .25em;
    line-height: 1;
    vertical-align: baseline;
    font-weight: 700;
    text-align: center;
    white-space: nowrap;
    box-sizing: border-box;
}

修改Fabrizio Calderan

的答案之后

如果检测到两个小问题。

  • 当标签的文字非常短时,它会与文本显示在同一行。
  • 当标签的文字很长或包含换行符时,标签看起来不太好。

New JsFiddle

我能够通过将标签转换为块元素来解决第一个问题。 现在我正在寻找一种方法来确保所有标签与底部边框具有相同的空间。 (它应该看起来像图像,而不是小提琴)

enter image description here

2 个答案:

答案 0 :(得分:2)

为内部div添加行高,例如外部div {/ 1}}和line-height: 50px;

http://jsfiddle.net/2mxud8uk/3/

text-align: center

结果

enter image description here

答案 1 :(得分:0)

我找到了解决问题的方法:JsFiddle

.outerDiv {
    text-align: center;
    float: left;
    border-radius: 10px;
    border: 2px solid #c3c3c3;
    height: 100px;
    width: 100px;
    margin: 5px;
    line-height: 50px;
}

.errorLabel {
  display:inline-block;
    vertical-align: bottom;
    margin-bottom:7px;
    width:84%;
    background-color: #a90329;
    padding: .2em .6em .3em;
    color: #fff;
    border-radius: .25em;
    line-height: 1;
    font-weight: 700;
    white-space: nowrap;
    box-sizing: border-box;
}