简体   繁体   English

将CSS中的多个图像并排居中

[英]Centering multiple images in CSS side by side

I'm a beginner at CSS and HTML so I'm sure this is a mess. 我是CSS和HTML的初学者,所以我确定这是一团糟。 But what I'm trying to do is center 3 images side by side in a horizontal center in CSS. 但是我想做的是将3个图像并排放置在CSS的水平中心。 I've tried different solutions have gotten them to align properly but they still stay stuck to the left of the page or will stack on top of each other (and sometimes overlap). 我尝试过使用不同的解决方案来使它们正确对齐,但是它们仍然停留在页面左侧,或者会堆叠在彼此的顶部(有时会重叠)。

<div id="imagesMain">
    <img src="IMG_20140930_140020.jpg">
    <img src="IMG_20140922_164619.jpg">
    <img src="IMG_20140608_181811.jpg">
</div>

And my CSS: 而我的CSS:

#imagesMain{
    display: inline-block;
    position:relative;
    padding: 0;
    margin-left:auto;
    margin-right:auto;
    margin-top: 20px;
    text-align:center;
}
#imagesMain img{
    height: 400px;
    width: 300px;
    vertical-align: center;
}

The images by default are huge. 默认情况下,图像很大。 the 2nd CSS block resizes them but I can't get them to do much else. 第二个CSS块调整了它们的大小,但我无法让他们做更多的事情。 Any ideas? 有任何想法吗?

You can use the almost same CSS, but with one simple correction, change : 您可以使用几乎相同的CSS,但只需进行一次简单的修改即可

vertical-align: middle;

And remove these: 删除这些:

display: inline-block;
position: relative;

There's no center here. 这里没有center It must be middle . 它一定是middle Please correct it. 请更正。 And remove display: inline-block from the <div> . 并从<div>删除display: inline-block Your final code should be like: 您的最终代码应类似于:

 #imagesMain { padding: 0; margin-left: auto; margin-right: auto; margin-top: 20px; text-align: center; } #imagesMain img { height: 400px; width: 300px; vertical-align: middle; } 
 <div id="imagesMain"> <img src="IMG_20140930_140020.jpg"> <img src="IMG_20140922_164619.jpg"> <img src="IMG_20140608_181811.jpg"> </div> 

Click on Run Code Snippet and press Full Page to check if this is what you are expecting. 单击“ 运行代码片段” ,然后按“整 页”以检查是否符合您的期望。

尝试将display: inline-block更改为display: block (以及删除margin-left: auto; margin-right: auto; 。如果您#imagesMain占用屏幕宽度的100%,图像居中放置,可以正常工作。

Probably your problem is the container, because the image are correct align to the center, I have simplify your code and colored the container and images: 可能是您的问题是容器,因为图像与中心对齐正确,所以我简化了代码并为容器和图像上色:

#imagesMain{

    position:relative;
    display: inline-block;

    width:100%;
    height:250px;
    margin-top:20px;

    background-color:red;

    text-align:center;
}
#imagesMain img{
    background-color:blue;
    height: 200px;
    width: 150px;
    margin-left:-4px; /* trick for remove the space beetwen */
}

https://jsfiddle.net/bcpph0pp/1/ https://jsfiddle.net/bcpph0pp/1/

UPDATE 更新

Reading other comments I think you want all aligned in the middle, this is a good resource for generate the code for FLEX BOX: http://the-echoplex.net/flexyboxes/ 阅读其他注释,我想您希望所有注释都对齐,这是生成FLEX BOX代码的好资源: http : //the-echoplex.net/flexyboxes/

And this is the example: https://jsfiddle.net/bcpph0pp/2/ 这是示例: https : //jsfiddle.net/bcpph0pp/2/

try learing flexbox because it has many uses for nicely aligning items and content. 尝试泄漏flexbox,因为它有许多用途,可以很好地对齐项目和内容。 it also keeps your css very small. 这也使您的CSS非常小。

if you would like to keep them centered al the time. 如果您想让它们始终居中。 you should use justify-content: center; 您应该使用justify-content: center;

 #imagesMain{ display: flex; justify-content: center; } #imagesMain img{ height: 400px; width: 300px; margin: 0 10px; } 
 <div id="imagesMain"> <img src="IMG_20140930_140020.jpg"> <img src="IMG_20140922_164619.jpg"> <img src="IMG_20140608_181811.jpg"> </div> 

for alternative uses look at css tricks they give good examples for how to use flexbox. 对于其他用途,请查看css技巧,它们为如何使用flexbox提供了很好的示例。

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

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