简体   繁体   English

使锚标签中的图像居中

[英]Centering an image that is in an anchor tag

I have an image in an anchor tag, and I need to center it. 我在锚标签中有一个图像,需要居中。

I have managed to do so using this technique that I just came across messing about in the DOM inspector: http://jsfiddle.net/m6e3m/ 我设法使用了我在DOM检查器中碰到的这种技术来做到这一点: http : //jsfiddle.net/m6e3m/

it uses: 它用:

display: table;
margin 0 auto;

I tried using: 我尝试使用:

display: block;
margin: 0 auto;
width: (?)px;

but I found it wasn't actually centering it unless I messed with the width a lot which isn't really very good as anchor tag widths seem to not work as well (imo), it was like basically using a margin-left. 但是我发现它实际上并没有居中,除非我把宽度弄得一塌糊涂,因为锚标签的宽度似乎不太好(imo),这并不是很好,这就像基本上使用了margin-left一样。

Curious as to whether any of you have used this before or found an even better technique as display: table isn't supported on IE 7 and back 好奇是否有人使用过此功能,或者发现了一种更好的显示方式:IE 7及更高版本不支持表格

You can use text-align: center; 您可以使用text-align: center; on the #container to center elements within it. 在#container上将元素居中。 Example here: http://jsfiddle.net/m6e3m/3/ 此处的示例: http : //jsfiddle.net/m6e3m/3/

use -ms-flexbox supported by all browser. 使用所有浏览器支持的-ms-flexbox。 DEMO DEMO

#container{
    width: 600px;
    height:600px;
    background: red;
}

.centre{ 
height: 100%;
    width:100%;

/* Internet Explorer 10 */
display:-ms-flexbox;
-ms-flex-pack:center;
-ms-flex-align:center;

/* Firefox */
display:-moz-box;
-moz-box-pack:center;
-moz-box-align:center;

/* Safari, Opera, and Chrome */
display:-webkit-box;
-webkit-box-pack:center;
-webkit-box-align:center;

/* W3C */
display:box;
box-pack:center;
box-align:center; 
}

It will work out for you.. 它将为您解决..

.center
{
width: 100%;
display:block
text-align: center;
}

Demo Fiddle 演示小提琴

Try: 尝试:

#container{
    width: 600px;
    height:600px;
    background: red;
    display: table-cell; /* <-- allows use of vertical align */
    text-align:center; /* <-- horizontal centering */
    vertical-align:middle; /* <-- vertical centering */
}

Use the above and you wont need any more CSS for the img 使用上面的内容,您将不再需要img CSS

您可以在容器中添加text-align: centervertical-align: middle;

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

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