如何使图片与文字垂直对齐

How to vertically align image with text

我正在制作我的第一个网站,但我无法使图标与文本垂直对齐

如果我使用 Font Awesome 图标效果很好,但我不想使用它。

这是我的代码和图片,可以更好地描述我的问题。

"Can't align the arrow icon with text" 图片

.icon {
  width: 20px;
  height: 20px;
  padding-left: 10px;
  margin-right: -12px;
}

.link {
  display: block;
  text-decoration: none;
  font-size: 20px;
  padding-top: 10px;
  padding-bottom: 10px;
}

.leftDiv {
  width: 200px;
  height: 100%;
  background-color: lightgray;
  padding-top: 10px;
  box-shadow: rgba(0, 0, 0, 0.5) 3px 0px 10px;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title>Nature Gallery</title>
  <link rel="shortcut icon" href="Icon/NG.png" />
  <link href="Style/StyleSheet1.css" rel="stylesheet" />
  <link href="Content/font-awesome.css" rel="stylesheet" />
</head>

<body>
  <div class="leftDiv">
    <a href="Home.html" class="link">
      <i class="fa fa-long-arrow-right"></i> Home
    </a>

    <a href="Home.html" class="link">
      <img src="Icon/long-arrow-alt-right-solid.svg" class="icon" /> Home
    </a>

    <a href="Home.html" class="link">
      <img src="Icon/long-arrow-alt-right-solid.svg" class="icon" style="vertical-align:middle;" /> Home
    </a>
  </div>
</body>

</html>

我认为可以通过对图标使用 padding-bottom 来解决问题:

.icon {
  width: 20px;
  height: 20px;
  padding-left: 10px;
  padding-bottom: 5px;
  margin-right: -12px;
}

然后可以根据您可能需要垂直对齐元素的数量添加或减去 padding-bottom

首先,将此添加到 .link

display: flex;
align-items: center;
justify-content: center;

然后,将此添加到 .leftDiv:

display: flex;
align-items: flex-start;
justify-content: center;
flex-direction: column;

您的代码应如下所示:

.icon {
  width: 20px;
  height: 20px;
  padding-left: 10px;
  margin-right: -12px;
}

.link {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: row;
  text-decoration: none;
  font-size: 20px;
  padding-top: 10px;
  padding-bottom: 10px;
}

.leftDiv {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  flex-direction: column;
  width: 200px;
  height: 100%;
  background-color: lightgray;
  padding-top: 10px;
  box-shadow: rgba(0, 0, 0, 0.5) 3px 0px 10px;
}

这里是 flexbox 的完整指南:https://css-tricks.com/snippets/css/a-guide-to-flexbox/