Center a fixed div with unknown width
This question already has an answer here:
Demo
css
img {
position:fixed;
top:0;
bottom:0;
left:0;
right:0;
margin:auto;
background: #000;
}
I suppose you want the .wrapper to have a fixed position because you want to have it as a header or footer. This makes it wrap to its contents, so you need to explicitly tell it to stretch to 100% width. Next, img are displayed inline. Therefore, to use the auto margin trick, you need to set its display to block:
.wrapper {
position: fixed;
width: 100%;
background: lightblue;
}
img {
display: block;
margin: 0 auto;
}
Working jsfiddle here: http://jsfiddle.net/7swkv/
链接地址: http://www.djcxy.com/p/13218.html下一篇: 居中未知宽度的固定格