如何在右侧浮动搜索图标

时间:2019-04-01 20:55:42

标签: html css

如何将超棒的字体搜索图标浮动到右侧以进行注册。

这是我的导航栏的屏幕截图,所以您也知道我的意思:

https://imgur.com/rE23FfB

我已经在互联网上搜索了教程,他们都说要使用float:但这似乎不起作用

body {
  margin: 0;
  padding: 0;
  margin-top: 130px;
  overflow-x: hidden;
}

.nav-menu a img {
  float: left;
  margin-left: 10px;
}

.nav-menu ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: rgba(0, 0, 0, 0.9);
  margin-top: -130px;
}

.nav-menu li {
  float: left;
}

.nav-menu li a,
.dropbtn {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.nav-menu li a:hover,
.dropdown:hover .dropbtn {
  border-top: 2px solid #ff0000;
}

.nav-menu li.dropdown {
  display: inline-block;
}

.nav-menu .dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.nav-menu .dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.nav-menu .dropdown-content a:hover {
  background-color: #ff0000;
  color: #fff;
}

.nav-menu .dropdown:hover .dropdown-content {
  display: block;
}

.nav-menu li.social {
  float: right;
}
<div class="nav-menu">
  <ul>
    <a href="#"><img src="img/logo.jpg" style="width:50px"></a>
    <li class="dropdown">
      <a href="javascript:void(0)" class="dropbtn">Games</a>
      <div class="dropdown-content">
        <a href="#">Link 1</a>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
      </div>
    </li>
    <li class="dropdown">
      <a href="javascript:void(0)" class="dropbtn">Crews</a>
      <div class="dropdown-content">
        <a href="#">Link 1</a>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
      </div>
    </li>
    <li><a href="#">Jobs</a></li>
    <li><a href="#">Photos</a></li>
    <li><a href="#">Videos</a></li>
    <li><a href="#">Events</a></li>
    <li><a href="#">News</a></li>
    <li class="social">
      <a href="#">Sign In</a>
      <a href="#" style="color:#ff0000">Sign Up</a>
      <li><a href="#"><i class="fas fa-search"></i></li></a>
  </ul>

2 个答案:

答案 0 :(得分:1)

问题是您的<li>包装了搜索图标。这是无效的代码。 <li>不能是<li>的子代。将其更改为一个<a>元素

body {
  margin: 0;
  padding: 0;
  margin-top: 130px;
  overflow-x: hidden;
}

.nav-menu a img {
  float: left;
  margin-left: 10px;
}

.nav-menu ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: rgba(0, 0, 0, 0.9);
  margin-top: -130px;
}

.nav-menu li {
  float: left;
}

.nav-menu li a,
.dropbtn {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.nav-menu li a:hover,
.dropdown:hover .dropbtn {
  border-top: 2px solid #ff0000;
}

.nav-menu li.dropdown {
  display: inline-block;
}

.nav-menu .dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.nav-menu .dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.nav-menu .dropdown-content a:hover {
  background-color: #ff0000;
  color: #fff;
}

.nav-menu .dropdown:hover .dropdown-content {
  display: block;
}

.nav-menu li.social {
  float: right;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">

<div class="nav-menu">
  <ul>
    <li><a href="#"><img src="img/logo.jpg" style="width:50px"></a></li>
    <li class="dropdown">
      <a href="javascript:void(0)" class="dropbtn">Games</a>
      <div class="dropdown-content">
        <a href="#">Link 1</a>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
      </div>
    </li>
    <li class="dropdown">
      <a href="javascript:void(0)" class="dropbtn">Crews</a>
      <div class="dropdown-content">
        <a href="#">Link 1</a>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
      </div>
    </li>
    <li><a href="#">Jobs</a></li>
    <li><a href="#">Photos</a></li>
    <li><a href="#">Videos</a></li>
    <li><a href="#">Events</a></li>
    <li><a href="#">News</a></li>
    <li class="social">
      <a href="#">Sign In</a>
      <a href="#" style="color:#ff0000">Sign Up</a>
      <a href="#"><i class="fas fa-search"></i></a>
    </li>
  </ul>
</div>

答案 1 :(得分:0)

我知道我对这个问题有点迟了-但我有一个改进的解决方案(至少我是这么认为的!)!

上一个答案的语法仍然有些不正确。您还应该尝试将下拉列表放入嵌套的ul中。 :-)

通过使用display: flex上的align-items: center.nav-menu,您将不需要使用float来对齐所有元素(无论如何,我倾向于避免使用常常会使您的代码表现出您所不希望的那样。)

我还在您的margin-left:auto容器中为您添加了.social,它将把您的所有社交活动推到父母的末端。

编辑: 如果您在菜单项上使用border-top: transparent,则可以确保在:hover上添加彩色边框时文本不会移动-因此,您实际上只是在显示颜色。

html,
body {
  margin: 0;
  padding: 0;
}

.nav-menu {
  display: flex;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.9);
}

.nav-menu img {
  margin: 0 10px;
}

.nav-menu>ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  display: flex;
}

ul.social {
  margin-left: auto;
}

.nav-menu li a,
.dropbtn {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  border-top: 2px solid transparent;
}

.nav-menu li a:hover,
.dropdown:hover .dropbtn {
  border-top: 2px solid #ff0000;
}

.nav-menu li.dropdown {
  display: inline-block;
}

.nav-menu .dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
  list-style: none;
  margin: 0;
  padding: 0;
}

.nav-menu .dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.nav-menu .dropdown-content a:hover {
  background-color: #ff0000;
  color: #fff;
}

.nav-menu .dropdown:hover .dropdown-content {
  display: block;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css">
<!-- just for the example -->

<div class="nav-menu">
  <a href="#"><img src="img/logo.jpg" style="width:50px"></a>
  <ul>
    <li class="dropdown">
      <a href="javascript:void(0)" class="dropbtn">Games</a>
      <ul class="dropdown-content">
        <li><a href="#">Link 1</a></li>
        <li><a href="#">Link 2</a></li>
        <li><a href="#">Link 3</a></li>
      </ul>
    </li>
    <li class="dropdown">
      <a href="javascript:void(0)" class="dropbtn">Crews</a>
      <ul class="dropdown-content">
        <li><a href="#">Link 1</a></li>
        <li><a href="#">Link 2</a></li>
        <li><a href="#">Link 3</a></li>
      </ul>
    </li>
    <li><a href="#">Jobs</a></li>
    <li><a href="#">Photos</a></li>
    <li><a href="#">Videos</a></li>
    <li><a href="#">Events</a></li>
    <li><a href="#">News</a></li>
  </ul>
  <ul class="social">
    <li><a href="#">Sign In</a></li>
    <li><a href="#" style="color:#ff0000">Sign Up</a></li>
    <li><a href="#"><i class="fas fa-search"></i></a></li>
  </ul>
</div>

如果全屏查看代码段,则可以看到完整的效果。