2012-07-23 6 views
2

Je ne peux pas centrer une image dans un div. Comment puis-je centrer l'image avec le balisage suivant?Impossible de centrer l'image

code html est comme suit:

<div id='post-area' style='background:#000; padding:20px 0;'> 
    <a class='fancybox' rel='group' href='example-big.png'> 
    <img src='example.png' /> 
    </a> 
</div> 

code CSS est la suivante:

#post-area { 
    font-size:13px; 
    color:#000; 
    text-align:left; 
    width:450px; 
    height:auto; 
    font-family:Meera 
    clear:both; 
    margin-top:20px; 
    letter-spacing:normal; 
} 

#post-area img { 
    max-width:250px; 
    height:auto; 
    float:none; 
    margin:0 auto; 
} 

S'il y a une façon de le faire

+0

* Note: * Votre CSS 'propriété font-family' est missi ng un point-virgule. En outre, HTML devrait avoir des guillemets doubles, pas des guillemets simples. – arttronics

Répondre

8

Si vous voulez juste au centre l'image horizontalement, vous pouvez changer l'alignement du texte: left vers text-align: center.

+0

Cela ne fonctionne plus. Je ne pense pas que cela fonctionnera si le parent 'div' est' position: absolute; ' – username

2

HTML

<div id='post-area' style='background:#000; padding:20px 0;'> 
    <a class='fancybox' rel='group' href='example-big.png'> 
    <img src='http://blog.spoongraphics.co.uk/wp-content/uploads/2011/colourful-logo/18.jpg' /> 
    </a> 
</div> 

CSS

#post-area { 
    font-size:13px; 
    color:#000; 
    text-align:center; 
    width:450px; 
    height:auto; 

    font-family:Meera; 
    clear:both; 
    margin-top:20px; 
    letter-spacing:normal; 
} 

#post-area img { 
    max-width:250px; 
    height:auto; 
    float:none; 
    margin:0 auto; 

}

voir la démo: - http://tinkerbin.com/gWYGnA6m

Questions connexes