【问题标题】:display: flex shows underlined iconsdisplay: flex 显示带下划线的图标
【发布时间】:2018-01-25 07:39:19
【问题描述】:

如果我将导航栏中的图标与display: flex 对齐,它会在我的图标下方显示一个下划线。如果我禁用它,它们会消失,但我的图标没有正确对齐。见下图。

我怎样才能摆脱它们?

代码如下:

.nav {
  width: 60px;
  height: 100vh;
  top: 100px;
  min-height: 100vh;
  background-color: #cccccc;
  position: fixed;
  z-index: 999;
  opacity: .4;
  border-right: 1px solid black;
  display: flex;
  flex-direction: column;
}

.nav a {
  width: 100%;
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
  border: none;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div class="nav">
  <a href=""><i class="material-icons">home</i></a>
  <a href=""><i class="material-icons">shopping_basket</i></a>
  <a href=""><i class="material-icons">business</i></a>
  <a href=""><i class="material-icons">mail</i></a>
  <a href=""><i class="material-icons">card_giftcard</i></a>
</div>

【问题讨论】:

  • 您的代码请...!
  • 您的问题有点含糊,但是您可以传递有关图像的任何信息吗?你是从 fontawesome 那里得到的吗?你创造了那些?您在这些图标中添加了哪个标签?
  • 抱歉添加了代码,我正在使用材料设计图标:fonts.googleapis.com/icon?family=Material+Icons"> 之前从未遇到过问题
  • 发送您的网站链接。您的图标链接错误
  • @Sharvan 你必须从链接中删除最后一个 to 字符,然后它才能工作

标签: html css flexbox


【解决方案1】:

之所以在添加display: flex之前没有下划线,是因为&lt;i&gt;设置为display: inline-block,下划线不渲染。

添加display: flex 后,&lt;i&gt; 上的display: inline-block 将不再应用,因为弹性项目是块状 元素。

将text-decoration: none 添加到a 将解决此问题


由于a 具有固定高度,另一种方法是删除display: flex 并使用text-align/line-height 使图标居中。

.nav {
  width: 60px;
  height: 100vh;
  top: 100px;
  min-height: 100vh;
  background-color: #cccccc;
  position: fixed;
  z-index: 999;
  opacity: .4;
  border-right: 1px solid black;
  display: flex;
  flex-direction: column;
}

.nav a {
  width: 100%;
  height: 60px;
  border: none;
  text-align: center;
  
}

.nav a i {
  line-height: 60px;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div class="nav">
  <a href=""><i class="material-icons">home</i></a>
  <a href=""><i class="material-icons">shopping_basket</i></a>
  <a href=""><i class="material-icons">business</i></a>
  <a href=""><i class="material-icons">mail</i></a>
  <a href=""><i class="material-icons">card_giftcard</i></a>
</div>

【讨论】:

    【解决方案2】:

    &lt;a&gt; 标签默认有text-decoration:underline,这就是为什么下面的行。

    将text-decoration: none 应用于您的锚标签.nav a

    堆栈片段

    .nav {
      width: 60px;
      height: 100vh;
      top: 100px;
      min-height: 100vh;
      background-color: #cccccc;
      position: fixed;
      z-index: 999;
      opacity: .4;
      border-right: 1px solid black;
      display: flex;
      flex-direction: column;
    }
    
    .nav a {
      width: 100%;
      height: 60px;
      display: flex;
      justify-content: center;
      align-items: center;
      border: none;
      text-decoration: none;
    }
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <div class="nav">
      <a href=""><i class="material-icons">home</i></a>
      <a href=""><i class="material-icons">shopping_basket</i></a>
      <a href=""><i class="material-icons">business</i></a>
      <a href=""><i class="material-icons">mail</i></a>
      <a href=""><i class="material-icons">card_giftcard</i></a>
    </div>

    【讨论】:

      【解决方案3】:

      试试text-decoration: none; 链接

      【讨论】:

        猜你喜欢
        • 2013-08-22
        • 1970-01-01
        • 1970-01-01
        • 2012-02-26
        • 2013-07-19
        • 1970-01-01
        • 1970-01-01
        • 2012-11-19
        • 1970-01-01
        相关资源
        最近更新 更多