如何在一个DIV中居中2个图像

时间:2013-07-19 10:06:04

标签: css image html centering

我有一个包含2个标签的DIV标签。我希望这些数字能够在DIV中居中,DIV本身应该位于页面中间。我该怎么做?

HTML:

<div class="images-captions">
    <figure><img src="images/Prop Pics/From Mike B aka StratosDadRI/intial-blueprinting_thumb.jpg" alt="Intial Blueprinting"><figcaption>After intinal blueprint</figcaption></figure>
    <figure><img src="images/Prop Pics/From Mike B aka StratosDadRI/after-damage_thumb.jpg" alt="After Damage"><figcaption>After damage</figcaption></figure>
</div>

CSS:

.images-captions            { width: 600px; margin: 0 auto; }
.images-captions figure     { float: left; width: 250px; padding: 10px;}
.images-captions figcaption { text-align: center; font-style: italic;   }

2 个答案:

答案 0 :(得分:1)

水平居中

这种情况我会这样解决:

<强> CSS

.images-captions {
    width: 600px;
    margin: 0 auto;
    text-align: center;
}

.images-captions figure {
    display: inline-block;
    width: 200px;
    margin: 0;
    padding: 10px;
}

.images-captions figcaption {
    font-style: italic;
}

<强>演示

Try before buy

垂直居中

你可以这样解决:

<强> CSS

.images-captions {
    width: 600px;
    margin: 0 auto;
    text-align: center;
}

.images-captions figure {
    width: 200px;
    margin: 0 auto;
}

.images-captions figcaption {
    font-style: italic;
}

<强>演示

Try before buy

答案 1 :(得分:0)

您可以通过将text-align添加到图中来解决它

.images-captions figure     { float: left; width: 250px; padding: 10px;text-align:center}
.images-captions figcaption { font-style: italic;   }

理解:
您想要将父文本中的文本对齐。所以你必须将属性添加到父元素而不是元素本身

DEMO on fiddle