简体   繁体   English

如何将插图中嵌套的图像与figcaptions并排对齐?

[英]How do I align images nested in a figure with figcaptions side by side?

I am trying to align several images that I have nested in a figure element side-by-side. 我试图并排对齐嵌套在图形元素中的多个图像。 I would like for each image to have its own, individual caption. 我希望每个图像都有自己的标题。 However, when I add in the they stop aligning side-by-side. 但是,当我添加它们时,它们将停止并排对齐。 This is my current code; 这是我当前的代码;

<figure>
   <img src="image1.jpg" alt= "image1" width="195" height="195">
      <figcaption>image1</figcaption>
   <img src="image2.jpg" alt= "image2" width="195" height="195">
      <figcaption>image2</figcaption>
   <img src="image3.pjg" alt= "image3" width="195" height="195">
      <figcaption>image3</figcaption>
   <img src="image4.jpg" alt= "image4" width="195" height="195">
      <figcaption>image4</figcaption>
</figure>

Hope this can help, at least it works for me. 希望这会有所帮助,至少对我有用。 I had the same problem in a bootstrap proyect and I've solved it using this code: 我在bootstrap proyect中遇到了同样的问题,并且使用以下代码解决了该问题:

<ul class="list-unstyled list-inline text-center">
  <li>
    <img src="image1.jpg" alt= "image1" width="195" height="195">
    <figcaption>image1</figcaption>
  </li>
  <li>
    <img src="image1.jpg" alt= "image2" width="195" height="195">
    <figcaption>image2</figcaption>
  </li>
  <li>
    <img src="image3.jpg" alt= "image3" width="195" height="195">
    <figcaption>image1</figcaption>
  </li>
  <li>
    <img src="image1.jpg" alt= "image4" width="195" height="195">
    <figcaption>image4</figcaption>
  </li>
</ul>

The css for these classes in bootstrap.css is : bootstrap.css中这些类的css是:

.list-unstyled {
        padding-left: 0;
        list-style: none;
    }
    .list-inline {
        padding-left: 0;
        margin-left: -5px;
        list-style: none;
    }
    .list-inline > li {
        display: inline-block;
        padding-right: 5px;
        padding-left: 5px;
    }
    .text-center {
        text-align: center;
    }

As already stated before, you should not/ must not use multiple figcaption tag inside a single figure . 如前所述,您不应该/不得在单个figure使用多个figcaption标签。

You could bundle your images and their figcaption tags inside nested figure tags. 您可以将图像及其图像figcaption标签捆绑在嵌套的figure标签中。

<figure>
<figcaption>Caption for the bundle of images</figcaption>

    <figure>
    <img src="picture1.jpg" alt="Description of picture 1">
    <figcaption>Caption for Picture 1</figcaption>
    </figure>

    <figure>
    <img src="picture2.jpg" alt="Description of picture 2">
    <figcaption>Caption for Picture 2</figcaption>
    </figure>

</figure>

This doesn't answer your question, but... don't do that. 这不能回答您的问题,但是...请不要这样做。

Only one element may be nested within a <figure> <figure>只能嵌套一个元素

You can have many images, but each figure should have a single caption. 您可以有很多图像,但是每个图形都应该有一个标题。

Either use a single figcaption for the figure, or change your markup to this: 对图形使用单个figcaption,或将标记更改为此:

<figure>
   <img src="image1.jpg" alt= "image1" width="195" height="195">
   <figcaption>image1</figcaption>
</figure>
<figure>
   <img src="image2.jpg" alt= "image2" width="195" height="195">
   <figcaption>image2</figcaption>
</figure>
<figure>
   <img src="image3.pjg" alt= "image3" width="195" height="195">
   <figcaption>image3</figcaption>
</figure>
<figure>
   <img src="image4.jpg" alt= "image4" width="195" height="195">
   <figcaption>image4</figcaption>
</figure>

You shouldn't use more than one figcaption within figure 图中您不应使用多个figcaption

The <figcaption> element is optional and can appear before or after the content within the <figure>. Only one <figcaption> element may be nested within a <figure>, although the <figure> element itself may contain multiple other child elements (eg, <img> or <code>). see http://html5doctor.com/the-figure-figcaption-elements/ 参见http://html5doctor.com/the-figure-figcaption-elements/

but if you insist on using multi figcaption per figure, you can do that by using css float:left 但是,如果您坚持要对每个图形使用多figcaption,则可以使用css float:left来做到这一点

<figure>
    <div style="float:left" >
      <img src="image1.jpg" alt= "image1" width="195" height="195">
      <figcaption>image1</figcaption>
    </div>
    <div style="float:left" >
        <img src="image2.jpg"  alt= "image2" width="195" height="195">
        <figcaption>image2</figcaption>
    </div>
    <div style="float:left" >
       <img src="image3.pjg" alt= "image3" width="195" height="195">
       <figcaption>image3</figcaption>
    </div>
    <div style="float:left" >
       <img src="image4.jpg" alt= "image4" width="195" height="195">
      <figcaption>image4</figcaption>
    </div>
</figure>

http://jsfiddle.net/ZQLCq/1/ http://jsfiddle.net/ZQLCq/1/

You could do the following... 您可以执行以下操作...

Do not include more than on figcaption in the figure. 在图中不要包含超过figcaption的内容。 See http://html5doctor.com/the-figure-figcaption-elements/ 参见http://html5doctor.com/the-figure-figcaption-elements/

Wrap the figures in divs and spans. 将数字以div和span换行。

<div>
<span>
  <figure>
    <img src="image1.jpg" alt= "image1" width="195" height="195">
    <figcaption>image1</figcaption>
  </figure>
 </span>
 <span>
  <figure>
    <img src="image2.jpg" alt= "image1" width="195" height="195">
    <figcaption>image2</figcaption>
  </figure>
</span>
</div>

Then align it using css. 然后使用CSS对齐它。 See Align span next to each other 请参见将跨度对齐

vertical-align:top;

You can play around with this here http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_figcaption 您可以在这里尝试一下http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_figcaption

If your images are displaying like zigzag in a row even after using the figure and figurecaption tags with css display:inline-block then do the following 如果即使在使用带CSS和displaycaption标签的图片时,图像仍连续显示为之字形,请执行以下操作:

<div class="container">
  <figure>
   <img src="image.jpg" width=150px; height=150px;>
   <figcaption>image.jpg</figcaption>
  </figure>
</div>

css:
.container{
 display: flex
}

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

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