html - 水平对齐 div 中的图像

标签 html css

我试图水平对齐这些图像,但它不起作用。我尝试过 float 它们并使用内联 block CSS,但我认为我只是错过了一些简单的东西。

<div class="footer-bottom"> 
    <div class="footer-container">

       Gold Sponsors<br />
       <img src="http://placehold.it/350x150">
       <img src="http://placehold.it/140x100">
       <img src="http://placehold.it/350x150">
       <img src="http://placehold.it/140x100"><br />
       Silver Sponsors<br />
       <img src="http://placehold.it/350x150">
       <img src="http://placehold.it/140x100">
       <img src="http://placehold.it/350x150">
       <img src="http://placehold.it/140x100"><br />

    </div>
</div>

<style>
   .footer-container {
       width: 120px;
       height: 72px;
       display: inline-block;
   }

   /* resize images */
   .footer-container img {
       width: 100%;
       height: auto;
   }

</style>

最佳答案

我不确定您想做什么,所以我将提供两个答案。

1)如果你想水平分布你的图片,你需要两件事。首先,增大 footer-bottom 。现在,它太小,无法容纳多张图片。我将此容器的宽度更改为auto,以适应屏幕的宽度。

.footer-bottom {
  width: auto;
  background-color: #f1f1f1;
}

/* resize images */
.footer-container img {
  width: 50px;
  height: auto;
}
<div class="footer-bottom"> 
    <div class="footer-container">

       Gold Sponsors<br />
       <img src="http://placehold.it/350x150">
       <img src="http://placehold.it/140x100">
       <img src="http://placehold.it/350x150">
       <img src="http://placehold.it/140x100"><br />
       Silver Sponsors<br />
       <img src="http://placehold.it/350x150">
       <img src="http://placehold.it/140x100">
       <img src="http://placehold.it/350x150">
       <img src="http://placehold.it/140x100"><br />

    </div>
</div>


如果你想水平对齐图片,比如居中,只需设置 .footer-containermargin-leftmargin-right > 到自动

.footer-container {
   width: 120px;
   height: 72px;
   margin-left: auto;
   margin-right: auto;
}

 /* resize images */
.footer-container img {
   width: 100%;
   height: auto;
}
<div class="footer-bottom"> 
    <div class="footer-container">

       Gold Sponsors<br />
       <img src="http://placehold.it/350x150">
       <img src="http://placehold.it/140x100">
       <img src="http://placehold.it/350x150">
       <img src="http://placehold.it/140x100"><br />
       Silver Sponsors<br />
       <img src="http://placehold.it/350x150">
       <img src="http://placehold.it/140x100">
       <img src="http://placehold.it/350x150">
       <img src="http://placehold.it/140x100"><br />

    </div>
</div>

关于html - 水平对齐 div 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30905000/

相关文章:

HTML/CSS - 滚动时固定背景

HTML、CSS : Append inline-element to the right of a centered inline-element

javascript - 如何隐藏文本内容而不是子元素?

html - 无法消除图像之间的差距?

html - 如何在 HTML img 标签中屏蔽图像?

css - 为什么 css 下拉菜单中的子菜单不可见?

html - 选项卡面板空间

html - "word-wrap:break-word"在使用 SpraedPDFBundle 生成 PDF 时不起作用

html - 未定义的属性名称 (xmlns)

css - 相邻显示器中的 iframe :table-cell element pushes down the content on its neighboring cell