簡體   English   中英

如何在不使用CSS'table-cell'屬性的情況下在IE7中垂直對齊文本?

[英]How to vertically align text in IE7 without using CSS 'table-cell' property?

我有固定的高度div,其中包含文本。 我希望文本在div的中間垂直對齊,但問題在於一些文本是單行的,有些文本分成兩行。 對於IE8,Chrome和Firefox,使用display: table-cellvertical-align: middle提供了我需要的解決方案:

JS Fiddle就在這里 從星號上取下星號width: 300px ,查看文本在一行時的格式。

但是,IE7不支持display: table-cell屬性。 我發現的唯一解決方案僅適用於單行,而不適用於可能是1行或2行的文本。 如何在IE7中顯示它,就像在更現代的瀏覽器中一樣,而不使用任何腳本?

如何將IE7 CSS調用放置position:relative對於divabsolute位於h6 ,並保持代碼為現代瀏覽器的vertical-align

http://jsfiddle.net/yap59cn3/

<!--[if IE 7]>
    <link rel="stylesheet" type="text/css" href="ie7.css">
<![endif]-->

ie7.css

div
{
    /* Use inheritance, and override only the declarations needed. */
    position:relative;
}

h6
{
    height:auto; /* override inherited css */
    position:absolute;
    top:45%;
}

目標是使IE7“顯得” - 無論你做什么,它都不會像現代瀏覽器那樣漂亮。 對我來說,這不值得頭痛(甚至不是一點點)。

就個人而言,我已經開始(ab)使用填充來獲得垂直對齊。 如果使用固定高度,它會特別方便,因為您可以使用填充值來偏移高度,以獲得完美的全高元素。

注意:此解決方案僅在您事先知道<h6>中將包含哪些文本時才有效。 如果你動態添加它,我建議使用wordcounting試圖弄清楚它是否會換行。

解:

HTML

<div>
    <h6 class="OneLineVertCentered">Here is some text. Look at this lovely text. Isn't it nice?</h6>
</div>

<div style="margin-top: 1em;"> <!-- Margin only for displaying the boxes properly -->
    <h6 class="TwoLineVertCentered">Here is some text. Look at this <br />
        lovely two-line text. Isn't it nice?</h6>
</div>

CSS

div {
    background-color: yellow;
    height: 30px;
    width: 200px;
    width: 300px;
}

h6.OneLineVertCentered,
h6.TwoLineVertCentered {
    font-size: 12px;
    line-height: 1em;
}
    h6.OneLineVertCentered {
        padding-top: 10px;
    }
    h6.TwoLineVertCentered {
        padding-top: 3px;
    }

小提琴: http //jsfiddle.net/Snorbuckle/CnmKN/

片段(與小提琴相同):

  div { background-color: yellow; height: 30px; width: 200px; width: 300px; } h6.OneLineVertCentered, h6.TwoLineVertCentered { font-size: 12px; line-height: 1em; } h6.OneLineVertCentered { padding-top: 10px; } h6.TwoLineVertCentered { padding-top: 3px; } 
 <div> <h6 class="OneLineVertCentered">Here is some text. Look at this lovely text. Isn't it nice?</h6> </div> <div style="margin-top: 1em;"> <h6 class="TwoLineVertCentered">Here is some text. Look at this <br /> lovely two-line text. Isn't it nice?</h6> </div> 

你應該能夠用line-heightvertical-align: middle;完成這個vertical-align: middle;

div {
    background-color: yellow;
    height: 30px;
    line-height: 30px;
    width: 200px;
    *width: 300px;
}

h6 {
    font-size: 12px;
    line-height: 1em;
    height: 30px;
    vertical-align: middle;
}

您可以使用輔助span元素垂直對齊文本,如下例所示:

HTML

<div class="container">
    <span class="aligner"></span>
    <h3>Text to be aligned center in the beloved ie7</h3>
</div> 

CSS

div.container {
    background-color: #000;
    color: #fff;
    height: 300px;
    width: 250px;
    position:relative;
    margin:12px auto;
    text-align:center;
}
.aligner {
    display: inline-block;
    height: 100%; 
    content: ' ';
    margin-right: -0.25em; 
    vertical-align: middle;
}
h3 {
    display: inline-block; 
    vertical-align: middle;
}

小提琴: http//jsfiddle.net/groumisg/dbx4rr0f/

通常情況下,我們會使用偽元素,但ie7(多么令人驚訝!)不支持:after,:before ... etc。 另請注意,ie7不支持display:inline-block,用於默認不內聯的元素,如div。 要使用display:inline-block for div,你必須使用以下hack:

div {
    display: inline-block;
    *display: inline;
    zoom: 1;
}

正如此處所建議的那樣內聯塊在Internet Explorer 7,6中不起作用

看一下這個

http://jsfiddle.net/CnmKN/59/

CSS代碼

div {
    background-color: yellow;
    height: 30px;
    width: 200px;
    *width: 300px;
    display:table;
}

h6 {
    font-size: 12px;
    line-height: 1em;
    display: table-cell;
    vertical-align: middle;
    height:90px;
}

我知道垂直居中元素的兩種方法比使用table-cell更方便:

1)線高:

.element {
height: 60px;
line-height: 60px
}

這僅在文本在一行中時才有效。

2)位置絕對/保證金自動

.parentElement {
position: relative;
}

.element {
position: absolute;
top: 0;
bottom: 0;
margin: auto 0;
}

您可能必須使用高度(自動或值)並顯示內聯/內聯塊。 試一試。

關鍵是不要使用像素進行對齊,只使用%-s。 即使在IE5上工作 :)

這是演示

 .wrapper{ position: relative; width: 100%; height: 200px; /* change this value to see alignment*/ background-color: red; margin: 0 auto; } .cell{ position: absolute; display:block; background-color: blue; left:50%; top:50%; /*this puches element half down*/ margin-left:-100px; /* this is the half size of cell width:200px;*/ margin-top: -.5em; /*this is the half size of font size*/ width: 200px; color: #fff; text-align:center; } 
 <div class='wrapper'> <div class='cell'>vertically aligned text</div> </div> 

div {
    background-color: yellow;
    height: 400px;
    width: 200px;
    display: table-cell;
    vertical-align: middle;
    width: 300px;
}
h6 {
    font-size: 12px;
    line-height: 1em;
    height: 30px;   
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM