如何在图像下写一个标题?

Sam*_*dar 118 html css html5

我有两个需要保持内联的图像; 我想在每张图片下面写一个标题.

<center>
   <a href="http://example.com/hello">
       <img src="hello.png" width="100px" height="100px">
   </a>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
   <a href="http://example.com/hi">
       <img src="hi.png" width="100px" height="100px">
   </a>
</center>
Run Code Online (Sandbox Code Playgroud)

我该如何实施?

McG*_*gle 259

FigureFigcaption标签:

<figure>
    <img src='image.jpg' alt='missing' />
    <figcaption>Caption goes here</figcaption>
</figure>
Run Code Online (Sandbox Code Playgroud)

一定要喜欢HTML5.


见样品

#container {
    text-align: center;
}
a, figure {
    display: inline-block;
}
figcaption {
    margin: 10px 0 0 0;
    font-variant: small-caps;
    font-family: Arial;
    font-weight: bold;
    color: #bb3333;
}
figure {
    padding: 5px;
}
img:hover {
    transform: scale(1.1);
    -ms-transform: scale(1.1);
    -webkit-transform: scale(1.1);
    -moz-transform: scale(1.1);
    -o-transform: scale(1.1);
}
img {
    transition: transform 0.2s;
    -webkit-transition: -webkit-transform 0.2s;
    -moz-transition: -moz-transform 0.2s;
    -o-transition: -o-transform 0.2s;
}
Run Code Online (Sandbox Code Playgroud)
<div id="container">
    <a href="#">
        <figure>
            <img src="http://lorempixel.com/100/100/nature/1/" width="100px" height="100px" />
            <figcaption>First image</figcaption>
        </figure>
    </a>
    <a href="#">
        <figure>
             <img src="http://lorempixel.com/100/100/nature/2/" width="100px" height="100px" />
            <figcaption>Second image</figcaption>
        </figure>
    </a>
</div>
Run Code Online (Sandbox Code Playgroud)

  • @ JukkaK.Korpela这些标签非常重要.http://stackoverflow.com/a/17272444/756329和http://html5doctor.com/lets-talk-about-semantics/ (15认同)
  • 这些标签没有任何帮助.它们只是虚拟标记,您需要在CSS(和/或其他HTML标记)中完成所有工作.而且你需要一个额外的操作来使旧版本的IE识别它们甚至作为虚拟标签.因此使用`div`或`span`更简单. (10认同)
  • 问题是这样的:`[数字]可以移动到另一页或附录,而不会影响主流.`<figure>`和`<figcaption>`不是带有标题的图像的通用替换.他们*只适用于数字.如果您(例如)有一个由与标题照片混合的段落组成的分步操作方法,那么图像在html中定义的相同分段符号中显示可能很重要.因此`<figure>`在这里不适用(据我所知). (3认同)

The*_*pha 21

CSS

#images{
    text-align:center;
    margin:50px auto; 
}
#images a{
    margin:0px 20px;
    display:inline-block;
    text-decoration:none;
    color:black;
 }
Run Code Online (Sandbox Code Playgroud)

HTML

<div id="images">
    <a href="http://xyz.com/hello">
        <img src="hello.png" width="100px" height="100px">
        <div class="caption">Caption 1</div>
    </a>
    <a href="http://xyz.com/hi">
        <img src="hi.png" width="100px" height="100px"> 
        <div class="caption">Caption 2</div>
    </a>
</div>?
Run Code Online (Sandbox Code Playgroud)

这里有一个小提琴.


Gre*_*g B 7

<div style="margin: 0 auto; text-align: center; overflow: hidden;">
  <div style="float: left;">
    <a href="http://xyz.com/hello"><img src="hello.png" width="100px" height="100px"></a>
    caption 1
  </div>
 <div style="float: left;">
   <a href="http://xyz.com/hi"><img src="hi.png" width="100px" height="100px"></a>
   caption 2                      
 </div>
</div>
Run Code Online (Sandbox Code Playgroud)


小智 7

放置图像 - 让我们说它的宽度是140px - 在链接内:

<a><img src='image link' style='width: 140px'></a>
Run Code Online (Sandbox Code Playgroud)

接下来,将标题放在a中并使其宽度小于图像的宽度,同时将其居中:

<a>
  <img src='image link' style='width: 140px'>
  <div style='width: 130px; text-align: center;'>I just love to visit this most beautiful place in all the world.</div>
</a>
Run Code Online (Sandbox Code Playgroud)

接下来,在链接标记中,为链接设置样式,使其不再像链接一样.您可以为其提供任何颜色,但只需删除链接可能带有的任何文字装饰.

<a style='text-decoration: none; color: orange;'>
  <img src='image link' style='width: 140px'>
  <div style='width: 130px; text-align: center;'>I just love to visit this most beautiful place in all the world.</div>
</a>
Run Code Online (Sandbox Code Playgroud)

我用图片的标题将图像包裹在一个链接中,这样就没有任何文字可以将标题推开:标题通过链接与图片相关联.这是一个例子:http://www.alphaeducational.com/p/okay.html

  • 不允许在像“&lt;a&gt;”这样的内联标签中使用像“&lt;div&gt;”这样的块标签 - 您应该使用“&lt;span&gt;”作为标题。 (2认同)

Pat*_*t M 5

对于响应式图像。您可以在图形标签中添加图片和来源标签。

<figure>
  <picture>
    <source media="(min-width: 750px)" srcset="images/image_2x.jpg"/>
    <source media="(min-width: 500px)" srcset="images/image.jpg" />
    <img src="images.jpg" alt="An image">
  </picture>
  <figcaption>Caption goes here</figcaption>
</figure>
Run Code Online (Sandbox Code Playgroud)