简体   繁体   English

当margin:0自动时将图像居中; 绝对定位不起作用

[英]Centring an image when margin:0 auto; and absolute positioning doesn't work

I'm trying to centre most of the images in this page, all the ones I need to centre are of class .loadMeLater . 我正在尝试将本页中的大多数图像居中,我需要居中的所有图像都属于.loadMeLater类。

The image is a loading spinner placeholder and then the real image is loaded when it appears in the viewport. 图像是正在加载的微调器占位符,然后当实际图像出现在视口中时才加载真实图像。 I want the placeholder and the real image to be centred. 我希望占位符和真实图像居中。 They are both in the same element, which has the class .loadMeLater . 它们都在同一个元素中,该元素具有类.loadMeLater

I tried margin:0 auto and that doesn't work, I can't use absolute positioning and left:0 and right:0 because that causes the image's container to be too small. 我尝试了margin:0 auto ,但是这行不通,我不能使用绝对定位,而left:0right:0不能使用,因为这会导致图像的容器太小。

Currently I'm centring the spinner with relative positioning, left:50% , and margin-left:-0.5*spinner-width; 目前,我将微调器的相对位置定为居中, left:50%margin-left:-0.5*spinner-width; And I am centring the image using a calculation in javascript involving the width of the image and the parent. 我将使用javascript的计算(包括图像的宽度和父对象的宽度)来居中图像。

There has to be an easier way to centre the image. 必须有一种更简单的方法来使图像居中。 Anyone have any ideas how to do so? 任何人有任何想法怎么做?

Here is the site: http://irfandesign.com/dev/ 这是网站: http : //irfandesign.com/dev/

To show the elements that have the images, click on ' learn more about me ' or ' work '. 要显示具有图像的元素,请单击“了解有关我的更多信息”或“工作”。 Then scroll down to them. 然后向下滚动到它们。

Try just 尝试一下

margin:auto;

I'm not sure if what you have is actually a valid implementation. 我不确定您所拥有的实际上是否有效。

By default an img tag has 'display: inline-block;' 默认情况下,img标签具有“ display:inline-block;”

if you select your img tag and do this: 如果选择img标签并执行以下操作:

img {
    display:block;
    margin: 0 auto;
}

It will center your image within its container div 它将使您的图片在其容器div中居中

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

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