css - 用 <li> 显示包含 <img> 的文本

标签 css html image html-lists

<分区>


关闭 6 年前

我一直在尝试在图像下方或上方显示文本,如下所示:

目前,我坚持只显示图像(给出的示例):

我无法使用 W3schools 提供的确切代码,因为我的缩略图会展开并在单击时切换到响应式轮播。我只希望名称与缩略图一起出现。我无法发布我的代码,所以我上传了一张带有 li> 标签的图片:

<div class="container">
    <ul id="myGallery">
        <li><img src="link.png" /></li>
        <li><img src="link.png" /></li>
        <li><img src="link.png" /></li>
        <li><img src="link.png" /></li>
        <li><img src="link.png" /></li>
    </ul>
</div>

最佳答案

您可以简单地使用 flex 来做到这一点。

类似下面的内容

ul {
 list-style: none;
 display: flex;
 flex-wrap: wrap;
}

ul li {
  margin: 0 5px 5px 0;
  text-align: center;
  border: 1px solid #000;
}
<div class="container">
  <ul id="myGallery">
    <li>
      <img src="http://lorempixel.com/100/100/nature/1/" width="100px" height="100px" />
      <p>First image</p>
    </li>
    <li>
      <img src="http://lorempixel.com/100/100/nature/2/" width="100px" height="100px" />
      <p>Second image</p>
    </li>
    <li>
      <img src="http://lorempixel.com/100/100/nature/1/" width="100px" height="100px" />
      <p>Third image</p>
    </li>
    <li>
      <img src="http://lorempixel.com/100/100/nature/2/" width="100px" height="100px" />
      <p>Fourth image</p>
    </li>    
  </ul>
</div>

第二种方法,使用figure

类似下面的内容

/*figure approach*/

.figures {
  display: flex;
  flex-wrap: wrap;
}

figure {
  margin: 0;
  margin-right: 5px;
  text-align: center;
  border: 1px solid #000;
}
<div class="figures">
  <figure>
    <img src="http://lorempixel.com/100/100/nature/1/" width="100px" height="100px" />
    <figcaption>First image</figcaption>
  </figure>
  <figure>
    <img src="http://lorempixel.com/100/100/nature/2/" width="100px" height="100px" />
    <figcaption>Second image</figcaption>
  </figure>
  <figure>
    <img src="http://lorempixel.com/100/100/nature/1/" width="100px" height="100px" />
    <figcaption>Third image</figcaption>
  </figure>
  <figure>
    <img src="http://lorempixel.com/100/100/nature/2/" width="100px" height="100px" />
    <figcaption>Fourth image</figcaption>
  </figure>
</div>

更新

问题 jsfiddle - https://jsfiddle.net/raw9b1fj/2

解决方案 jsfiddle - https://jsfiddle.net/raw9b1fj/4/

关于css - 用 <li> 显示包含 <img> 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41121293/

上一篇:css - 我可以像在 LESS 中那样在标准 CSS 中嵌套类吗

下一篇:html - 网站自动缩小? CSS 更改不会出现

相关文章:

php - 如何更改指定 div 中存在的所有图像的尺寸

image - 生成和使用许多图像失败

javascript - 从浏览器打印图像文件

javascript - 如何在 DOM `data` 属性中定义一个 JSON 对象?

jquery - CSS从多个具有相同类的目标中定位正确的div

javascript - 如何在保存到文件夹(Javascript)之前修复方向(上传图像)?

html - 链接图像不牢固

html - 放大时我所有的 h1 标签都在右边

CSS Sprite 重复整个 Sprite

html - 响应式设计中的搜索表单 - 删除移动设备上的搜索按钮