繁体   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