我正在尝试创建一个全宽下拉菜单,我想将“登录”的子菜单与父母的右侧对齐。
我尝试使用position: relative
作为父级,position: absolute
使用right: 0;
作为孩子,但似乎无效。
以下是代码:
.fa {
line-height: 1em;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: black;
}
li {
display: inline-block;
}
li.left {
float: left;
border-right: 1px solid white;
}
li.right {
float: right;
border-left: 1px solid white;
}
li a {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: red;
}
li#login form {
display: none;
position: absolute;
background-color: gray;
padding: 20px;
}
li#login:hover form {
display: block;
}
<link href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" />
<nav>
<ul>
<li class="left"><a class="fa fa-home" href="index.jsp"> Home</a>
</li>
<li class="left"><a class="fa fa-shopping-cart" href="#home"> Carrello</a>
</li>
<li class="right"><a class="fa fa-user-plus" href=""> Signup</a>
</li>
<li id="login" class="right"><a class="fa fa-user-plus" href=""> Login</a>
<form id="login_form" action="login" method="post">
<input id="login_username" class="login_field" name="username" type="text" placeholder="username" />
<br />
<input id="login_password" class="login_field" name="password" type="password" placeholder="password" />
<br />
<div id="submit-div">
<input type="submit" value="login" />
</div>
</form>
</li>
</ul>
<nav>
答案 0 :(得分:1)
以下是您遇到的问题:
<nav>
标记。<li>
(您计划定位position: relative
)。overflow: hidden
黑客并使用::after
加clear: both;
。请参阅更新的工作代码段:
.fa {
line-height: 1em;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: black;
}
ul:after {
display: block;
clear: both;
content: " ";
}
li {
display: inline-block;
}
li.left {
float: left;
border-right: 1px solid white;
}
li.right {
float: right;
border-left: 1px solid white;
}
li a {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: red;
}
li#login {
position: relative;
}
li#login form {
display: none;
position: absolute;
background-color: gray;
padding: 20px;
right: 0;
}
li#login:hover form {
display: block;
}
<link href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" />
<nav>
<ul>
<li class="left"><a class="fa fa-home" href="index.jsp"> Home</a></li>
<li class="left"><a class="fa fa-shopping-cart" href="#home"> Carrello</a></li>
<li class="right"><a class="fa fa-user-plus" href=""> Signup</a></li>
<li id="login" class="right">
<a class="fa fa-user-plus" href=""> Login</a>
<form id="login_form" action="login" method="post">
<input id="login_username" class="login_field" name="username" type="text" placeholder="username" />
<br />
<input id="login_password" class="login_field" name="password" type="password" placeholder="password" />
<br />
<div id="submit-div">
<input type="submit" value="login" />
</div>
</form>
</li>
</ul>
</nav>