如何居中包含绝对元素的图片?
我已经设法在图像中定位绝对元素,但现在我想将图像居中放置在de red容器中…多么?
例如:

HTML
<div id="image">
<div id="the-image">
<img src="..." width="..." height="..." alt="">
<a href="..."><img src="" alt=""></a>
</div>
</div>CSS
#image {background-color: red; float: left; width: 660px;}
#the-image {display: inline-block; position: relative;}
#the-image img {}
#the-image a {position: absolute; bottom: 10px; right: 10px;}发布于 2014-03-13 08:35:37
你可以这样做
#the-image {
text-align: center;
display: block;
}
#the-image img {
display: inline-block;
}或者这个
#the-image img {
display: block;
margin: 0 auto;
}为此,您需要确保您的图像设置了width属性。
编辑:
#the-image {
display: block; /* changed from inline-block to block */
}
#the-image img {
display: block;
margin: 0 auto;
}发布于 2014-03-13 10:30:44
好了,几个小时后,我找到了解决方案……我怎么会错过它呢!
这个解决方案来自另一篇文章https://stackoverflow.com/a/4980600/888139
它说:如果您有一个带有-:center;的容器div,那么其中的任何文本都将相对于该容器元素的宽度居中。为此目的,内联块元素被视为文本,因此它们也将居中。
所以,我只需要让带有id 图像的div居中对齐文本...仅此而已。
<div id="image">
<div id="the-image">
<img src="..." width="..." height="..." alt="">
<a href="..."><img src="" alt=""></a>
</div>
</div>
#image {background-color: red; float: left; width: 660px; text-align: center}
#the-image {display: inline-block; position: relative;}
#the-image img {}
#the-image a {position: absolute; bottom: 10px; right: 10px;}发布于 2014-03-13 13:03:07
我不想让它变得复杂或其他什么。但是试试这个,看看它是否有效。
<div id="image">
<center>
<div id="the-image">
<img src="image.jpg" width="..." height="..." alt="">
<a href="..."/><img src="" alt=""></a>
</div>
</center>
</div>这是为了以防你需要把所有的东西都放在中心位置。:)
https://stackoverflow.com/questions/22366794
复制相似问题