Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css 具有绝对位置的div中的水平中心动态图像_Css_Image_Dynamic_Center - Fatal编程技术网

Css 具有绝对位置的div中的水平中心动态图像

Css 具有绝对位置的div中的水平中心动态图像,css,image,dynamic,center,Css,Image,Dynamic,Center,我在网络上到处寻找这个答案,但在我看来,为了使图像在div中以绝对位置水平居中,我需要知道图像的尺寸,但它是动态的 这是我的html: <header> <div id="elogo"> <img src="http://www.ftiweb.com/images/eStore/eStore_wht50.png"> </div> <div id="nav">TOUR | MENU</div> </header&

我在网络上到处寻找这个答案,但在我看来,为了使图像在div中以绝对位置水平居中,我需要知道图像的尺寸,但它是动态的

这是我的html:

<header>
<div id="elogo">
    <img src="http://www.ftiweb.com/images/eStore/eStore_wht50.png">
</div>
<div id="nav">TOUR | MENU</div>
</header>
<div id="content">
<img class="ipad" src="http://ftiweb.com/images/eStore/Ipad_hand.png">
</div>
<footer>
<div id="foot">&#169; FTIeStore 2013 &bull; Privacy Policy</div>
</footer>
目标只是让图像保持在页面的底部/中心,并重新调整大小以适应浏览器窗口。如果我把这件事复杂化了,请随意建议一个替代方案

下面是一个指向js.fiddle的链接:


如果您希望它是绝对位置,请按如下方式操作:

父对象需要具有相对位置,否则它将紧靠主体定位。
你不需要宽度,因为我的图像太大,所以我只加入了宽度。

左=中心位置-图像宽度的一半

img {
   position: absolute;
   bottom: 0;
   left: 50%; /*half the container width*/
   margin-left: -250px; /*half the width of the image*/
}

谢谢你的帮助。这就是我所需要的,这是正确的解决方案。谢谢,我不知道左:0和右:0和页边距左:自动和页边距右:自动!!!我知道已经晚了两年了。但是这个答案应该被接受@Ben CohnI我不确定这是故意遗漏还是意外,但我需要添加
top:0也是为了使图像在垂直和水平方向上居中。我没有包括它,因为问题只是关于水平居中。
img {
  width:20%;
  display:block;
  position:absolute;
  left:0;
  right:0;
  bottom:0;
  margin:auto;
}
img {
   position: absolute;
   bottom: 0;
   left: 50%; /*half the container width*/
   margin-left: -250px; /*half the width of the image*/
}