<img>
を含むものの中央に配置するにはどうすればよい<div>
ですか?
通常margin-left: auto; margin-right: auto;
は機能していません。
max-width
画像はコンテナより大きくすることができ、とによって水平または垂直に拡大縮小されますmax-height
。
以下のコードを持つjsfiddle:http: //jsfiddle.net/9ShGj/1/
HTML:
<div class="container">
<img src="http://www.yensa.com/fat/fat-dude10.jpg" />
</div>
<div class="container">
<img src="http://oi43.tinypic.com/bje2wn.jpg" />
</div>
CSS:
.container {
width: 200px;
height: 200px;
background: rgb(120, 120, 120);
border: 1px solid red;
margin: 5px;
}
.container img {
max-width: 200px;
max-height: 200px;
margin-left: auto;
margin-right: auto;
vertical-align: middle;
}