我的导航栏有问题。我希望“注册”按钮位于“登录”按钮的右侧,与窗口右边缘之间的间隔为xxxpx,这样当您将鼠标悬停在“登录”按钮上时,带有输入不会出现在屏幕上。我试图将“注册”按钮放在“”中,但是由于CSS代码,即使在“注册”按钮上悬停了鼠标也不是我期望的行为,但我还是获得了登录表单。如何将“注册”按钮放在“登录”按钮的右侧? 附言这是用于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()">☰</a>
</div>
答案 0 :(得分:1)
只需将“注册”按钮的锚标记放置在“下拉”区域上方。
<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()">☰</a>
</div>