vertical-align:span中间元素

时间:2013-03-07 08:18:09

标签: html css

我试图让跨度中的文本在相对于相邻文本的中间垂直对齐。这很容易实现,但是当跨度中的文本占用两行时,文本被截止。 要解决这个问题,当文本占用两行时,跨度要么需要扩展高度,要么以某种方式将文本对齐到中间..

问题的一个工作示例是http://jsfiddle.net/BxLnN/

非常感谢任何建议或解决方案。

包含元素的当前尺寸必须保持不变。

干杯!

html

<div class="divisions_container">
    <div class="division">
        <div class="div_head">
            DIVISION 1 <span>SIX WINNING NUMBERS</span>
        </div>
        <div class="div_head">
            DIVISION 2
            <div>
                <span>FIVE WINNING NUMBERS PLUS ONE OF THE TWO SUPPLEMENTARY NUMBERS</span>
            </div>
        </div>
    </div>
</div>

css

/* division winnings */
.divisions_container {
    font-size: 13px;
    padding: 0 10px;
    width: 7.4cm;
    height: 8.5cm;
}
.div_head {
    margin-top: 20px;
    text-align: left;
    padding-left: 5px;
    position: relative;
    background-color: #000;
    color: #fff;
    max-height: 6mm;
    font-weight: bold;
    font-size: 1.2em;
    }
/* # winning numbers */
.div_head span {
    font-size: 0.5em;
    vertical-align: middle;
    font-weight: 200;
    border: 1px solid red;
    display: inline-block;
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    left: 100px;
}

4 个答案:

答案 0 :(得分:2)

spanposition: absolute;。当您将元素设置为绝对定位时,它将变为block元素而没有任何边距,并且您不能对它们使用vertical-align,因为它仅适用于inlineinline-block元素

所以我建议这个CSS:

/* division winnings */
.divisions_container {
    font-size: 13px;
    padding: 0 10px;
    width: 7.4cm;
    height: 8.7cm;
}
.div_head {
    margin-top: 20px;
    text-align: left;
    padding-left: 5px;
    position: relative;
    background-color: #000;
    color: #fff;
    max-height: 6mm;
    font-weight: bold;
    font-size: 1.2em;
    }
/* # winning numbers */
.div_head span {
    font-size: 0.5em;
    vertical-align: middle;
    font-weight: 200;
    border: 1px solid red;
    display: inline-block;

}

.div_head div {position: absolute;
    right: 0;
    top: -2px;
    bottom: 0;
    left: 100px;
}

小提琴:http://jsfiddle.net/praveenscience/BxLnN/1/

答案 1 :(得分:1)

如果您可以设置固定高度(例如20px左右),那么您可以使用line-height: 20px样式中的.div_head span。然后,这会自动将文本置于span中。

您也不需要设置display: inline-blockvertical-align: middle

示例:http://jsfiddle.net/BxLnN/2/

答案 2 :(得分:1)

试试这个...... 您必须指定跨度和位置的宽度为相对值,并增加或减少顶部和左侧的值;

.div_head span {
    font-size: 0.5em;
    vertical-align: middle;
    font-weight: 200;
    border: 1px solid red;
    display: inline-block;
    position: relative;
    right: 0;
    top: -20px;
    bottom: 0;
    left: 100px;
    text-align: left;
    width: 200px;
}

答案 3 :(得分:0)

试试这个...... 您必须指定跨度和位置的宽度为相对值,并增加或减少顶部和左侧的值;

.div_head span {
    font-size: 0.5em;
    vertical-align: middle;
    font-weight: 200;
    border: 1px solid red;
    display: inline-block;
    position: relative;
    right: 0;
    top: -20px;
    bottom: 0;
    left: 100px;
    text-align: left;
    width: 200px;
}

好人就在这里 HTML

<div class="Division_Container">
<span>Division 1</span>
<span class="Inner_Container">
six winning numbers
</span>
</div>
<div class="Division_Container">
<span>Division 2</span>
<span class="Inner_Container">
five winning numbers and rest of the nubmers to be displayede winning numbers and rest of the nubmers to be displayed
</span>
</div>
<div class="Division_Container">
<span>Division 2</span>
<span class="Inner_Container">
five winning numbers and rest of the nubmers to be displayede winning n
</span>
</div>

CSS

.Division_Container{display: block; width: 300px; background: #000; color: #fff; margin: 10px;}
.Inner_Container{display: inline-block; vertical-align: middle; left: 200px;  width: 200px; margin-left: 20px; border: 1px solid red;}
/* If you want to specify height add following */
.Division_Container:before{content: "."; display: inline-block; height: 100px; vertical-align: middle; visibility: hidden;}