Reputation:
I'm trying to create a full width drop-down menu and I would like to align the sub-menu of "Login" to the parent's right side.
I tried with position: relative
for the parent and position: absolute
with right: 0;
for the child but it doesn't seem to work.
Here's the code:
.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>
Upvotes: 1
Views: 1433
Reputation: 167172
These are the issues you have:
<nav>
tag at the bottom.<li>
(to which you are planning to position, a position: relative
).overflow: hidden
hack and use ::after
to clear: both;
.See the updated working snippet:
.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>
Upvotes: 1