Center a fixed div with unknown width

This question already has an answer here:

  • How to horizontally center a <div> in another <div>? 82 answers

  • 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

    上一篇: 如何在<div>中水平居中<input>?

    下一篇: 居中未知宽度的固定格