Icon的悬停不会排队

时间:2017-10-11 02:50:03

标签: html css

登录图标的悬停功能一直向左移动,而不是与图标本身对齐。

我知道这是因为它在nav元素中有一个文本类,但是我完全知道如何修复它。我想在nav元素中保留图标。任何帮助,将不胜感激。

HTML和CSS

.fixed-nav-bar {
         position: fixed;
         top: 0;
         left: 0;
         z-index: 9999;
         width: 100%;
         height: 46px;
         background-color: #262626;
         overflow: hidden;
       }
     
       .fixed-nav-bar a {
        color: #ffffff;
        float: left;
        padding: 14px 16px;
        font-size: 16px;
        text-decoration: none;
      }
     
      .fixed-nav-bar a:hover {
         background-color: #ddd;
         color: black;
      }
     
      .fixed-nav-bar a.active {
          background-color: #4CAF50;
          color: white;
      }
     
   
    .material-icons {

        cursor: pointer;
        position: absolute;
        top: 3px;
        right: 12px;
     }  
   
      .material-icons {
        text-align: right;
        color: white;
        font-size: 40;
      }
   
       .material-icons a:hover {
          background-color: #ddd;
          color: black;
        } 
<nav class="fixed-nav-bar">
        <a href="home">Home</a>
        <a href="Example">Examples</a>
        <a href="About">About</a>
        <a href="Contact">Contact</a>
        <a href="login"><i class="material-icons">&#xe851;</i></a>
    </nav>

3 个答案:

答案 0 :(得分:0)

问题是该图标的<a>标记应用了与其他链接相同的float: left(在.fixed-nav-bar a中定义)。

您可以将float: right规则应用于图标的<a>标记,并使用较高 specificity 的选择器来更正此问题。没有简单的选择器,但幸运的是你可以使用 :last-of-type 伪选择器:

.fixed-nav-bar a:last-of-type {
  float: right;
}

在以下示例中可以看到这一点:

.fixed-nav-bar {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9999;
  width: 100%;
  height: 46px;
  background-color: #262626;
  overflow: hidden;
}

.fixed-nav-bar a {
  color: #ffffff;
  float: left;
  padding: 14px 16px;
  font-size: 16px;
  text-decoration: none;
}

.fixed-nav-bar a:hover {
  background-color: #ddd;
  color: black;
}

.fixed-nav-bar a.active {
  background-color: #4CAF50;
  color: white;
}

.material-icons {
  cursor: pointer;
  position: absolute;
  top: 3px;
  right: 12px;
}

.material-icons {
  text-align: right;
  color: white;
  font-size: 40;
}

.material-icons a:hover {
  background-color: #ddd;
  color: black;
}

.fixed-nav-bar a:last-of-type {
  float: right;
}
<nav class="fixed-nav-bar">
  <a href="home">Home</a>
  <a href="Example">Examples</a>
  <a href="About">About</a>
  <a href="Contact">Contact</a>
  <a href="login"><i class="material-icons">&#xe851;</i></a>
</nav>

希望这有帮助! :)

答案 1 :(得分:0)

class="material-icons"移至<a>代码,并使用.material-icons

更新您的css top:0px

<强>的变化:

<a href="login" class="material-icons"><i>&#xe851;</i></a>

    .material-icons {

        cursor: pointer;
        position: absolute;
        top: 0px;
        right: 12px;
     }  

<强>段:

.fixed-nav-bar {
         position: fixed;
         top: 0;
         left: 0;
         z-index: 9999;
         width: 100%;
         height: 46px;
         background-color: #262626;
         overflow: hidden;
       }
     
       .fixed-nav-bar a {
        color: #ffffff;
        float: left;
        padding: 14px 16px;
        font-size: 16px;
        text-decoration: none;
      }
     
      .fixed-nav-bar a:hover {
         background-color: #ddd;
         color: black;
      }
     
      .fixed-nav-bar a.active {
          background-color: #4CAF50;
          color: white;
      }
     
   
    .material-icons {

        cursor: pointer;
        position: absolute;
        top: 0px;
        right: 12px;
     }  
   
      .material-icons {
        text-align: right;
        color: white;
        font-size: 40;
      }
   
       .material-icons a:hover {
          background-color: #ddd;
          color: black;
        } 
<nav class="fixed-nav-bar">
        <a href="home">Home</a>
        <a href="Example">Examples</a>
        <a href="About">About</a>
        <a href="Contact">Contact</a>
        <a href="login"  class="material-icons"><i>&#xe851;</i></a>
    </nav>

答案 2 :(得分:0)

插入此内容:

a[href="login"] {
    float: right;
}

.fixed-nav-bar {
         position: fixed;
         top: 0;
         left: 0;
         z-index: 9999;
         width: 100%;
         height: 46px;
         background-color: #262626;
         overflow: hidden;
       }
     
       .fixed-nav-bar a {
        color: #ffffff;
        float: left;
        padding: 14px 16px;
        font-size: 16px;
        text-decoration: none;
      }
     
      .fixed-nav-bar a:hover {
         background-color: #ddd;
         color: black;
      }
     
      .fixed-nav-bar a.active {
          background-color: #4CAF50;
          color: white;
      }
     
   a[href="login"]{
    float: right;
   }
   
    .material-icons {

        cursor: pointer;
        position: absolute;
        top: 3px;
        right: 12px;
     }  
   
      .material-icons {
        text-align: right;
        color: white;
        font-size: 40;
      }
   
       .material-icons a:hover {
          background-color: #ddd;
          color: black;
        }
<nav class="fixed-nav-bar">
        <a href="home">Home</a>
        <a href="Example">Examples</a>
        <a href="About">About</a>
        <a href="Contact">Contact</a>
        <a href="login"><i class="material-icons">&#xe851;</i></a>
</nav> 

要获得100%的身高,请移除position: absolute;

.fixed-nav-bar {
         position: fixed;
         top: 0;
         left: 0;
         z-index: 9999;
         width: 100%;
         height: 46px;
         background-color: #262626;
         overflow: hidden;
       }
     
       .fixed-nav-bar a {
        color: #ffffff;
        float: left;
        padding: 14px 16px;
        font-size: 16px;
        text-decoration: none;
      }
     
      .fixed-nav-bar a:hover {
         background-color: #ddd;
         color: black;
      }
     
      .fixed-nav-bar a.active {
          background-color: #4CAF50;
          color: white;
      }
     
   a[href="login"]{
    float: right;
   }

  
      .material-icons {
        text-align: right;
        color: white;
        font-size: 40;
        cursor: pointer;

      }
   
       .material-icons a:hover {
          background-color: #ddd;
          color: black;
        }
<nav class="fixed-nav-bar">
        <a href="home">Home</a>
        <a href="Example">Examples</a>
        <a href="About">About</a>
        <a href="Contact">Contact</a>
        <a href="login"><i class="material-icons">&#xe851;</i></a>
</nav>