简体   繁体   English

垂直居中<a>在IE7中</a>的块

[英]Vertical Centering a block <a> in IE7

I'm trying to get vertical centering a block in IE7 (IE6 if possible too), let me get one thing clear - I'm not vertically centering the actual element, but the text within the element. 我试图让IE7中的一个块垂直居中(IE6,如果可能的话),让我明白一件事 - 我不是垂直居中的实际元素,而是元素中的文本。 This is my CSS and HTML which works on IE8 and above, but not below. 这是我的CSS和HTML,适用于IE8及以上版本,但不适用于以下版本。

a {
    display: table-cell;
    width: 100px;
    height: 54px;
    background: black;
    color: white;
    text-align: center;
    text-decoration: none;
    vertical-align: middle;
}

<a href="#">Hello superlongword</a>

Now I know IE6 is virtually dead, I'd still like to support it if possible but if not then IE7 is fine. 现在我知道IE6几乎已经死了,如果可能的话,我仍然想支持它,但如果没有,那么IE7就可以了。 I want to try keep it with a single element as much as possible - it's for a navigator, so I don't want elements upon elements just for one link. 我想尝试尽可能多地使用单个元素 - 这是一个导航器,所以我不希望元素仅用于一个链接。

EDIT 编辑
I've decided to go with sprites, it'll be much easier for a navigator - not the best solution, but I'll be happy with the outcome. 我决定选择精灵,导航员会更容易 - 不是最好的解决方案,但我会对结果感到满意。 If any posted solutions do work, I'll swap over to them. 如果任何发布的解决方案确实有效,我会交换它们。 :) :)

Use display:inline-block with a placeholder element to vertically center the block hyperlink: 使用display:inline-block带有占位符元素的display:inline-block来垂直居中块超链接:

  <style type="text/css"> #blockbox { width: 500px; height: 500px; border: 1px solid black;} #container, #placeholder { height: 100%; } #content, #placeholder { display:inline-block; vertical-align: middle; } </style> <div id="blockbox"> <div id="container"> <a id="content"> Vertically centered content in a block box of fixed dimensions </a> <span id="placeholder"></span> </div> </div> 

References 参考

If you know it will be just one line of text, use line-height. 如果您知道它只是一行文本,请使用line-height。

It is far from a single element, but you could just use an actual table cell. 它远非单个元素,但您可以使用实际的表格单元格。 It's ugly, but supporting IE6 is an ugly affair. 这很难看,但支持IE6是一件丑陋的事情。

table {
    border: 0;
    border-collapse: collapse;
    height: 54px;
    width: 100px;
}
td {
    vertical-align: middle;
}
a {
    background: black;
    color: white;
    display: block;
    height: 100%;
    text-align: center;
    text-decoration: none;
}

<table><tr><td><a href="#">Hello superlongword</a></td></td></table>

If you know the link will be a certain number of lines, you can center using one extra element and a margin. 如果您知道链接将是一定数量的行,则可以使用一个额外元素和边距来居中。 (eg <a><em>anchor text</em></a> , em { margin-top:12px } ) (例如<a><em>anchor text</em></a>em { margin-top:12px }

If you don't know the height of the element to be centered, you need table-cell layout behavior. 如果您不知道要居中的元素的高度,则需要表格单元格布局行为。 The only way to get this in IE6 is with an actual table cell or JavaScript. 在IE6中获取此功能的唯一方法是使用实​​际的表格单元格或JavaScript。 Sorry. 抱歉。

This is a known bug. 这是一个已知的错误。 The way to fix this, which may not be applicable in your situation, is to add a Float to the element and have it display as inline-block to make hasLayout work in IE. 修复此问题的方法(可能不适用于您的情况)是向元素添加Float并将其显示为inline-block以使hasLayout在IE中工作。 I will also supply FF2 hacks to get around issues there too. 我也将提供FF2黑客来解决那里的问题。

Fixed code: 固定代码:

a { 
    display: inline-block;
    display: -moz-inline-stack; /*FF2 Hack*/
    zoom: 1; /*IE inline-block star hack*/
    *display: inline; /*IE inline-block star hack*/
    float: left; 
    width: 100px;
    _height: 54px; /*FF2 Hack*/
    background: black;
    color: white;
    text-align: center; 
    text-decoration: none; 
    margin: 0px auto;
}

<a href="#">Hello superlongword</a> 

EDIT 编辑

I didn't add a float, because I figured with the other hacks being used, it wouldn't matter. 我没有添加浮动,因为我认为使用其他黑客,这没关系。

Why don't you try a padding? 你为什么不尝试填充?

a {
    display: inline-block;
    overflow: hidden;
    padding: 20px;
    background: black;
    color: white;
    text-align: center;
    text-decoration: none;
}

<a>Hello superlongword</a>

That sure works crossbrowser atleast for IE7 (couldn't test on IE6), example 对于IE7来说,确实可以使用crossbrowser(无法在IE6上测试), 例如

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM