代码之家  ›  专栏  ›  技术社区  ›  user2974848

如何使用标题和描述将三幅图像居中?HTML

  •  0
  • user2974848  · 技术社区  · 8 年前

    我是HTML新手,正在尝试创建一个包含三个居中图像(并排显示)的网页。以下是我的代码:

    <center>
    <figure style="display:inline-block;text-align:center;top:350px">
        <div style="width:275px; font-size:80%;margin:20px;text-align:center;display:inline-block">
        <img src="images/website-main1.jpg" style="margin-bottom:10px" width="275px" height="175px" />
        <figcaption style="text-align:center">This is an example of a super long caption that will make the images offset</figcaption>
        <div style="text-align:left">
        <p>description example</p>
        </div>
        </div>
        <div style="width:275px; font-size:80%;margin:20px;text-align:center;display:inline-block">
        <img src="images/website-main6_1.jpg" style="margin-bottom:10px" width="275px" height="175px" />
        <figcaption style="text-align:center">First image</figcaption>
        <div style="text-align:left">
        <p>description</p>
        </div>
        </div>
        <div style="width:275px; font-size:80%;margin:20px;text-align:center;display:inline-block;">
        <img src="images/a1_orig.jpg" style="margin-bottom:10px" width="275px" height="175px" />
        <figcaption style="text-align:center">First image</figcaption>
        <p>description</p>
        </div>
    </figure>
    </center>

    此代码创建图像并将其居中,但如果标题或描述不完全相同,则图像将偏移(例如:如果最左侧图像上的标题非常冗长,则该图像将显示得高于其他两个)。有没有办法解决这个问题?

    1 回复  |  直到 8 年前
        1
  •  3
  •   cmp    8 年前

    你应该尝试使用CSS!维护HTML要容易得多。

    您还应该考虑使用Flexbox。

    我为您制作了一个演示: https://jsfiddle.net/djzyvtvh/1/

    在所有图片和标题周围添加一个DIV,如下所示:

    .container {
      display:flex;
      width:90%;
      margin:0 auto;
    }
    

    使用: display:flex 启用Flexbox。

    然后给每个盒子一个 flex:1 属性,因此每个项都是相等的:

    .box {
      flex:1;
      margin:0 4px;
    }