首页 > 解决方案 > 将项目放置在导航栏中的问题

问题描述

我的导航栏有问题。我希望“注册”按钮位于“登录”按钮的右侧,距离窗口的右边缘有 xxxpx 的空间,这样当您将鼠标悬停在“登录”按钮上时,表单带有输入不会出现在屏幕外。我试图将“注册”按钮放在“”内,但由于 CSS 代码,即使将鼠标悬停在“注册”按钮上,我也会获得登录表单,这不是所需的行为。如何将“注册”按钮放在“登录”按钮的右侧?PS 这是 anguar 的代码,但也是一样的。谢谢。

在此处输入图像描述

.space {
  width: 350px;
}

.dropdown {
  float: right;
}

.forminput {
  margin: 5px;
}

.formbutton {
  margin: 5px;
  float: right;
}


/* Add a black background color to the top navigation */

.topnav {
  background-color: #333;
  overflow: hidden;
}


/* Style the links inside the navigation bar */

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}


/* Add an active class to highlight the current page */

.active {
  background-color: #4CAF50;
  color: white;
}


/* Hide the link that should open and close the topnav on small screens */

.topnav .icon {
  display: none;
}


/* Dropdown container - needed to position the dropdown content */

.dropdown {
  overflow: hidden;
}


/* Style the dropdown button to fit inside the topnav */

.dropdown .dropbtn-login {
  font-size: 17px;
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}


/* Style the dropdown content (hidden by default) */

.dropdown-content-login {
  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;
}


/* Style the links inside the dropdown */

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


/* Add a dark background on topnav links and the dropdown button on hover */

.topnav a:hover,
.dropdown:hover .dropbtn-login {
  background-color: #555;
  color: white;
}


/* Add a grey background to dropdown links on hover */

.dropdown-content-login a:hover {
  background-color: #ddd;
  color: black;
}


/* Show the dropdown menu when the user moves the mouse over the dropdown button */

.dropdown:hover .dropdown-content-login {
  display: block;
}


/* When the screen is less than 600 pixels wide, hide all links, except for the first one ("Home"). Show the link that contains should open and close the topnav (.icon) */

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child),
  .dropdown .dropbtn-login {
    display: none;
  }
  .topnav a.icon {
    float: right;
    display: block;
  }
  .space {
    width: 0px;
  }
  .dropdown {
    float: left;
  }
}


/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens (display the links vertically instead of horizontally) */

@media screen and (max-width: 600px) {
  .topnav.responsive {
    position: relative;
  }
  .topnav.responsive a.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
  .topnav.responsive .dropdown {
    float: none;
  }
  .topnav.responsive .dropdown-content-login {
    position: relative;
  }
  .topnav.responsive .dropdown .dropbtn-login {
    display: block;
    width: 100%;
    text-align: left;
  }
}
<div class="topnav" id="myTopnav">
  <a routerLink="/" class="active">Home</a>
  <a routerLink="/gallery">Gallery</a>
  <div class="dropdown">
    <button class="dropbtn-login">Login</button>
    <div class="dropdown-content-login">
      <form>
        <input class="forminput" placeholder="username">
        <input class="forminput" type="password" placeholder="password">
        <button class="formbutton" type="submit">Login</button>
      </form>
    </div>
  </div>
  <a style="float:right" routerLink="/register">Register</a>
  <div style="float:right" class="space"></div>
  <a href="" class="icon" (click)="myFunction()">&#9776;</a>
</div>

标签: htmlcssnavbar

解决方案


只需将“注册”按钮的锚标记放置在“下拉”div 上方。

<div class="topnav" id="myTopnav">
     <a routerLink="/" class="active">Home</a>
     <a routerLink="/gallery">Gallery</a>
  <a style="float:right" routerLink="/register">Register</a>
     <div class="dropdown">
         <button class="dropbtn-login">Login</button>
         <div class="dropdown-content-login">
             <form>
                <input class="forminput" placeholder="username">
                <input class="forminput" type="password" placeholder="password">
                <button class="formbutton" type="submit">Login</button>
            </form>
        </div>
    </div>

    <div style="float:right" class="space"></div>
    <a href="" class="icon" (click)="myFunction()">&#9776;</a>
</div> 

推荐阅读