简体   繁体   English

如何在`内部水平居中和对齐图像 <div> `元素?

[英]How to center and justify images horizontally inside a `<div>` element?

I've got a <div> box, inside are one ore more images. 我有一个<div>框,里面有一个或多个图像。

Those images should be horizontally centered, and the distance between two or more images should always be equal. 这些图像应水平居中,两个或多个图像之间的距离应始终相等。 So, when there's just one image, left and right spaces should be equal, when there's more than one, all spaces between the images, and the borders of the <div> element should be equal. 因此,当只有一个图像时,左右空间应该相等,当存在多个图像时,图像之间的所有空间和<div>元素的边界应该相等。

This is the CSS code for the <div> element: 这是<div>元素的CSS代码:

div.bild {
    text-align:center;
    position:absolute;
    bottom: 0;
    margin-left: -30px;
    margin-bottom: 30px;
    width: 100%;
}

And this is the CSS for he <img> tags: 这是他的<img>标签的CSS:

img {
    margin: auto;
}

And this is the relevant HTML excerpt: 这是相关的HTML摘录:

<div id="box1" class="box">
    <h2><a name="box1">(title)</a></h2>
    <p>(text)</p>
    <div class="bild">
        <img id="imgleft" src="images/Comic-1.png"/>
        <img id="imgright" src="images/Comic-2.png"/>
    </div>
</div>

Using "inline-block" would allow you to have the centering advantages of text elements and the positioning advantages of block elements. 使用“inline-block”可以让您拥有文本元素的中心优势和块元素的定位优势。 (in your case a fix margin) (在您的情况下是固定保证金)

http://jsfiddle.net/meo/bYb8y/ http://jsfiddle.net/meo/bYb8y/

div.bild {
    text-align:center;
    position:absolute;
    bottom: 0;
    margin-left: -30px;
    margin-bottom: 30px;
    width: 100%;
}

img {
    display: inline-block;
    margin: 20px;
}​

if you want each image to be on a new line, use display block. 如果您希望每个图像都在新行上,请使用显示块。 i'm not 100% sure if it is what you want, if not it would be nice to provide an image that represents the wished behavior. 我不是100%确定它是否是你想要的,如果不是,提供代表所希望的行为的图像会很好。 Also could you tell us if the images can be next to each other or must they always be stacked vertically? 您也可以告诉我们这些图像是否可以彼此相邻,还是必须始终垂直堆叠?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM