html - 导航栏图标与超链接标签分开

标签 html css

我正在尝试创建一个带有 a 标签和图标的导航栏,但由于某种原因,它们之间存在分离,其中 a 标签背景并不直接位于图标。 这是我的意思的图像:https://gyazo.com/986c59e17f031ce0b94547af6bceebac 这是我当前的导航栏 html 和 css:

.nav a {
  float: right;
  display: block;
  margin-right: 10px;
  background-color: black;
  text-align: center;
  line-height: 50px;
  text-decoration: none;
  padding: 3px;
  height: 10px;
  width: 10px;
}

.nav a:hover:not(.active) {
  background-color: grey;
  border-radius: 20px;
}

.nav a:not(.active) {
  color: white;
  border-radius: 20px;
}

.active {
  background-color: yellow;
  border-radius: 20px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="nav">
  <a href="#"><i class="fa fa-fw fa-align-justify"></i></a>
  <a href="#"><i class="fa fa-fw fa-wifi"></i></a>
  <a href="#"><i class="fa fa-fw fa-user"></i></a>
  <a class="active" href="#"><i class="fa fa-fw fa-gear"></i></a>
</div>

如何修复此问题以使 a 标签和图标对齐?

最佳答案

您必须删除 line-heightsize在您的链接上<a> ,像这样:

.nav a {
  float: right;
  display: block;
  margin-right: 10px;
  background-color: black;
  text-align: center;
  text-decoration: none;
  padding: 3px;
}

.nav a:hover:not(.active) {
  background-color: grey;
  border-radius: 20px;
}

.nav a:not(.active) {
  color: white;
  border-radius: 20px;
}

.active {
  background-color: yellow;
  border-radius: 20px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="nav">
  <a href="#"><i class="fa fa-fw fa-align-justify"></i></a>
  <a href="#"><i class="fa fa-fw fa-wifi"></i></a>
  <a href="#"><i class="fa fa-fw fa-user"></i></a>
  <a class="active" href="#"><i class="fa fa-fw fa-gear"></i></a>
</div>

关于html - 导航栏图标与超链接标签分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61054982/

相关文章:

jquery一步步显示隐藏div

html - SEO单页应用

jquery - Bootstrap 导航栏不保持顶部和宽度

javascript - 我如何以循环方式为图像制作动画?`

css - 带圆 Angular 的 3 列布局

javascript - 如何将html显示为文本?

html - 如何让我的 UL 在其下方进行下一次潜水而不是留下巨大的空间

javascript - 通过 Javascript 创建 Div - 语法问题

javascript - 如何以正确的方向显示图像,横向或纵向

javascript - 存储/检索 CSS 元素