user6373874
user6373874

Reputation:

CSS - How to align drop-down sub menu to the parent's right?

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">&nbsp;Home</a>
    </li>

    <li class="left"><a class="fa fa-shopping-cart" href="#home">&nbsp;Carrello</a>
    </li>

    <li class="right"><a class="fa fa-user-plus" href="">&nbsp;Signup</a>
    </li>

    <li id="login" class="right"><a class="fa fa-user-plus" href="">&nbsp;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

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

These are the issues you have:

  1. The first thing I noticed is, you have opening <nav> tag at the bottom.
  2. You have to give the parent <li> (to which you are planning to position, a position: relative).
  3. Get rid of 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">&nbsp;Home</a></li>
    <li class="left"><a class="fa fa-shopping-cart" href="#home">&nbsp;Carrello</a></li>
    <li class="right"><a class="fa fa-user-plus" href="">&nbsp;Signup</a></li>
    <li id="login" class="right">
      <a class="fa fa-user-plus" href="">&nbsp;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

Related Questions