在HTML中,可以使用<figure>
元素和<figcaption>
元素来显示带标题的图像。<figure>
元素用于包裹图像,<figcaption>
元素则用于包含图像的标题。
下面是一个示例代码:
<figure>
<img src="example.jpg" alt="Example Image">
<figcaption>这是一个示例图像</figcaption>
</figure>
在上面的代码中,<figure>
元素包裹了一个图像元素<img>
和一个标题元素<figcaption>
。图像元素的src
属性指定了图像的URL,alt
属性则提供了图像的替代文本。标题元素<figcaption>
中包含了图像的标题。
值得注意的是,<figure>
和<figcaption>
元素并不会自动地创建图像的标题和图像之间的关联。为了确保图像和标题被正确地关联起来,应该给<img>
元素添加id
属性,并用<figcaption>
元素的for
属性指定该id
值。示例如下:
<figure>
<img src="example.jpg" alt="Example Image" id="example-image">
<figcaption for="example-image">这是一个示例图像</figcaption>
</figure>
在上面的代码中,<img>
元素的id
属性被设置为example-image
,而<figcaption>
元素的for
属性则指向该id
值,从而确保图像和标题被正确地关联。