我试图将图像放置在第二个居中图像的右侧,如下所示:
|..........{IMAGE}{IMAGE2}..|
关于如何居中图像的所有答案都使用 margin: auto;
或<center>
,这会导致 Image2 被发送到 Image1 下方,或周围带有 text-align: center;
的 div ,我无法使图像居中。
我有一个临时解决方案,在 Image1 上使用左边距,并将两个图像放置在 <center>
内。 ,但是如果窗口太小,这会导致图像不居中。有更好/更优雅的解决方案吗?
最佳答案
您实际上可以将第二个图像的宽度减小到零并使用负边距。
body {
text-align:center;
}
img {
vertical-align:middle;
}
img + img {
margin-right:-100px;/* equal to width of image , so it is virtually 0 width */
}
和
<body><!-- this can be a <p> container or else -->
<img src="http://lorempixel.com/50/100/" />
<img src="http://lorempixel.com/100/50/" />
</body>
http://codepen.io/gc-nomade/pen/rkwGj
只要您知道它的宽度,这适用于任何内联框
关于html - 将元素与居中元素相邻对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21369784/