2

我试图让跨度中的文本相对于相邻文本在中间垂直对齐。这很容易实现,但是当跨度中的文本占据两行时,文本会被截断。为了解决这个问题,当文本占据两行时,跨度需要扩大高度,或者以某种方式将文本与中间对齐。

该问题的一个工作示例在这里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

4 回答 4

2

你有spanwith position: 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/

于 2013-03-07T08:24:03.570 回答
1

如果您可以为您的跨度设置一个固定的高度,例如20px左右,那么您可以使用:line-height: 20px在您的.div_head span风格中。然后会自动将文本居中span

您也不需要设置 adisplay: inline-blockvertical-align: middle那种方式。

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

于 2013-03-07T08:26:34.293 回答
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;
}
于 2013-03-07T08:41:02.737 回答
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;}
于 2013-03-08T03:12:34.717 回答