我有一个元素包含两个图像,一个层叠在另一个之上。包含元素左侧浮动并排成三列。我已经使用了jQuery修补程序将图像集中到其元素中。我不得不使用jQuery,因为它是一个响应式设计,因此图像更改大小。除了某些移动设备之外,这一切都很有效。在按下随机元素之前,图像宽度似乎没有被计算出来?我希望这是有道理的。该网址是mooble.co.uk及其左侧较大的图片。居中绝对定位元素
**编辑
对不起,我没有说清楚,我已经使用该解决方案工作正常,但在移动设备上的宽度和左边距似乎并没有被负载计算?大概20秒后,它似乎就会弹出位置。
编辑**
<div class="g1" id="mass">
<p><span class="highlight"></span></p>
<div id="mass-cont">
<img class="bottom" src="images/mass.png" />
<img class="top" src="images/no-mass.png" />
</div>
</div>
CSS
#mass{position:relative;}
#mass-cont{}
#mass img{position:absolute;left:50%;}
#mass img{transition: opacity 1s ease-in-out;display:block;}
#mass img.top:hover{opacity:0;}
JS
//Functions
function setImageHeight(){
var imageSize = $('.bottom').height();
$('#mass-cont').height(imageSize);
var imageWidth = $('.bottom').width();
var position = imageWidth/2;
$('#mass-cont > img').css('margin-left' , '-'+ position + 'px');
}
//Load Document
$(document).ready(function() {
//Set image height
$('.bottom').load(function(){
setImageHeight();
});
$(window).resize(function() {
setImageHeight();
});
});
试试这个:1)http://www.zachgraeve.com/2006/10/01/center-abosulte-position-div/ 2)http://www.sitepoint.com/css-center -position-absolute-div/ –