Css 具有绝对位置的div中的水平中心动态图像
我在网络上到处寻找这个答案,但在我看来,为了使图像在div中以绝对位置水平居中,我需要知道图像的尺寸,但它是动态的 这是我的html: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&
<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">© FTIeStore 2013 • 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*/
}