首页 > 解决方案 > 无法让图像链接出现在水平线上

问题描述

我不确定我在这里缺少什么。我正在尝试向我的自定义主页添加一行书签。在遵循本指南以及对其他 stackoverflow 帖子进行故障排除后,我无法让这些图像链接水平排列。这是我用来使其水平的 HTML。

<h3>
            <div class="row">
                <div class="column">
                    <a href="https://wiki.archlinux.org/">
                        <img src="/home/henry/Documents/FFH/Assets/atlas.svg" style="width:100%">
                    </a>
                </div>
                <div class="column">
                    <a href="https://www.gmail.com/">
                        <img src="/home/henry/Documents/FFH/Assets/envelope.svg" style="width:100%">
                    </a>
                </div>
                <div class="column">
                    <a href="https://www.youtube.com/">
                        <img src="/home/henry/Documents/FFH/Assets/play.svg" style="width:100%">
                    </a>
                </div>
                <div class="column">
                    <a href="https://www.reddit.com/">
                        <img src="/home/henry/Documents/FFH/Assets/book.svg" style="width:100%">
                    </a>
                </div>
                <div class="column">
                    <a href="https://www.bankofamerica.com/">
                        <img src="/home/henry/Documents/FFH/Assets/dollar-sign.svg" style="width:100%">
                    </a>
                </div>
            </div>
        </h3>    

这是CSS:

/* Three image containers (use 25% for four, and 50% for two, etc) */
.column {
    float: left;
    width: 20%;
    padding: 10px;
  }
  
  /* Clearfix (clear floats) */
  .row::after {
    content: "";
    clear: both;
    display: table;
  }

这是指向该网站小提琴的链接。谢谢您的帮助 :)

标签: htmlcss

解决方案


这里有几个问题。简单的快速修复是h3从图像周围的代码中删除标签。您在 CSS 中将其设置为 40px 宽度。您还可以修复 h3 CSS。

body {
  background-color: #4e5461;
  border-width: 50px 20px;
}

h1 {
  display: block;
  margin-left: auto;
  margin-right: auto;
  margin-top: 200px;
  outline-style: none;
}

h2 {
  margin-left: auto;
  margin-right: auto;
}

.logo {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 200px;
  height: 200px;
}

iframe path {
  fill: #f3f4f5;
}

table,
th,
td {
  background-color: #4e5461;
  color: #4CAF50;
  margin-left: auto;
  margin-right: auto;
}

h3 {
  width: 40px;
  height: 40px;
  white-space: nowrap;
}


/* Three image containers (use 25% for four, and 50% for two, etc) */

.column {
  float: left;
  width: 20%;
  padding: 10px;
}


/* Clearfix (clear floats) */

.row::after {
  content: "";
  clear: both;
  display: table;
}
<h1>
  <img class="logo" src="https://via.placeholder.com/200x200" alt="globe">
</h1>
<h2>
  <form method="get" action="http://www.google.com/search">
    <div class="table-wrapper">
      <table cellpadding="0" style="color: #4e5461;">
        <tr>
          <td>
            <input type="text" name="q" size="60" style="color:#4e5461;" maxlength="255" value="">

          </td>
        </tr>
      </table>
    </div>
  </form>
</h2>
<div class="row">
  <div class="column">
    <a href="https://wiki.archlinux.org/">
      <img src="https://via.placeholder.com/140x100">
    </a>
  </div>
  <div class="column">
    <a href="https://www.gmail.com/">
      <img src="https://via.placeholder.com/140x100">
    </a>
  </div>
  <div class="column">
    <a href="https://www.youtube.com/">
      <img src="https://via.placeholder.com/140x100">
    </a>
  </div>
  <div class="column">
    <a href="https://www.reddit.com/">
      <img src="https://via.placeholder.com/140x100">
    </a>
  </div>
  <div class="column">
    <a href="https://www.bankofamerica.com/">
      <img src="https://via.placeholder.com/140x100">
    </a>
  </div>
</div>

话虽如此,您在这些 div 中没有任何文本,它只是一行图像链接,因此您根本不需要所有的包装列 div。可以text-align: center在您的容器上使用并列出图像链接。

body {
  background-color: #4e5461;
  border-width: 50px 20px;
}

h1 {
  display: block;
  margin-left: auto;
  margin-right: auto;
  margin-top: 200px;
  outline-style: none;
}

h2 {
  margin-left: auto;
  margin-right: auto;
}

.logo {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 200px;
  height: 200px;
}

iframe path {
  fill: #f3f4f5;
}

table,
th,
td {
  background-color: #4e5461;
  color: #4CAF50;
  margin-left: auto;
  margin-right: auto;
}


/* Three image containers (use 25% for four, and 50% for two, etc) */

.row {
  text-align: center;
}

.row a {
  margin: 5px;
  text-decoration: none;
}
<h1>
  <img class="logo" src="https://via.placeholder.com/200x200" alt="globe">
</h1>
<h2>
  <form method="get" action="http://www.google.com/search">
    <div class="table-wrapper">
      <table cellpadding="0" style="color: #4e5461;">
        <tr>
          <td>
            <input type="text" name="q" size="60" style="color:#4e5461;" maxlength="255" value="">

          </td>
        </tr>
      </table>
    </div>
  </form>
</h2>
<div class="row">
  <a href="https://wiki.archlinux.org/">
    <img src="https://via.placeholder.com/50x20">
  </a>
  <a href="https://www.gmail.com/">
    <img src="https://via.placeholder.com/50x20">
  </a>
  <a href="https://www.youtube.com/">
    <img src="https://via.placeholder.com/50x20">
  </a>
  <a href="https://www.reddit.com/">
    <img src="https://via.placeholder.com/50x20">
  </a>
  <a href="https://www.bankofamerica.com/">
    <img src="https://via.placeholder.com/50x20">
  </a>
</div>


推荐阅读