html - 图片下方的居中链接

标签 html css

如何在我的图片下方居中链接,我尝试了一切。当我使用 clear:both 时,这对我有帮助,但是我的图像在一列中,我不希望这样。所以基本上我想在每个以图片为中心的链接/文本下面。我知道此 HTML/CSS 不包含我想放在图片下方的链接,我删除了所有可能造成混淆的内容。

谢谢。

* {
  margin: 0;
  padding: 0;
  text-decoration: none;
  list-style: none;
}
html {
  height: 100%;
  background-color: #F0FFF0;
}
body {
  height: 100%;
  width: 100%;
  font-size: 100%;
  margin: 0;
}
.container {
  display: box;
  width: 100%;
  height: 100%;
  margin: 5%;
  padding: 0%;
  clear: both;
}
.products img {
  width: 31%;
  height: 31%;
  position: relative;
  float: left;
  display: inline;
  margin: 0 2% 3% 0%;
}
<!DOCTYPE>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Products</title>
  <link rel="stylesheet" type="text/css" href="css/products.css" />
</head>

<body>
  <div class="container">
    <div class="products">
      <a href="#">
        <img src="images/tunel.jpg" alt="Product name">
      </a>

      <a href="#">
        <img src="images/tunel.jpg" alt="Product name">
      </a>

      <a href="#">
        <img src="images/tunel.jpg" alt="Product name">
      </a>

      <a href="#">
        <img src="images/tunel.jpg" alt="Product name">
      </a>

      <a href="#">
        <img src="images/tunel.jpg" alt="Product name">
      </a>

      <a href="#">
        <img src="images/tunel.jpg" alt="Product name">
      </a>

      <a href="#">
        <img src="images/tunel.jpg" alt="Product name">
      </a>

      <a href="#">
        <img src="images/tunel.jpg" alt="Product name">
      </a>

      <a href="#">
        <img src="images/tunel.jpg" alt="Product name">
      </a>


    </div>
  </div>

</body>

</html>

最佳答案

给你:

不要 float 图片, float 链接。

将图片设为display:block 并在链接中添加text-align:center

* {
  margin: 0;
  padding: 0;
  text-decoration: none;
  list-style: none;
}
html {
  height: 100%;
  background-color: #F0FFF0;
}
body {
  height: 100%;
  width: 100%;
  font-size: 100%;
  margin: 0;
}
.container {
  display: box;
  width: 100%;
  height: 100%;
  margin: 5%;
  padding: 0%;
  clear: both;
}
.products a {
  width: 31%;
  height: 31%;
  position: relative;
  float: left;
  margin: 0 2% 3% 0%;
  text-align: center
}
.products a img {
  display: block;
  margin: auto;
}
<!DOCTYPE>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Products</title>
  <link rel="stylesheet" type="text/css" href="css/products.css" />
</head>

<body>
  <div class="container">
    <div class="products">
      <a href="#">
        <img src="http://www.fillmurray.com/g/140/100" alt="Product name">Link Text
      </a>

      <a href="#">
        <img src="http://www.fillmurray.com/g/140/100" alt="Product name">Link Text
      </a>

      <a href="#">
        <img src="http://www.fillmurray.com/g/140/100" alt="Product name">Link Text
      </a>


    </div>
  </div>

</body>

</html>

关于html - 图片下方的居中链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36894787/

相关文章:

html - ul 类未与网站 Logo 内联

javascript - AWS Cognito - 如何自定义输入字段?

javascript - 显示图像加载进度条

javascript - 将多个 CSS ID 更改为类

html - 如何在未知框架内使用 CSS 调整 div 中的图像?

php - PHP 中同名的不同元素

html - 将页面添加到 slider (html)

javascript - 为什么在 onsubmit 返回 undefined 之后提交表单?

html - 如何在 Bootstrap 中自定义两个文本输入之间的空间

javascript - 如何根据 CSS 类和二进制数字开/关位置突出显示特定列值