CSS - 如何将下拉子菜单与父级对齐?

时间:2016-06-02 22:47:13

标签: css drop-down-menu menu

我正在尝试创建一个全宽下拉菜单,我想将“登录”的子菜单与父母的右侧对齐。

我尝试使用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">&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>

1 个答案:

答案 0 :(得分:1)

以下是您遇到的问题:

  1. 我注意到的第一件事是,您在底部打开了<nav>标记。
  2. 您必须向父级<li>(您计划定位position: relative)。
  3. 摆脱overflow: hidden黑客并使用::afterclear: both;
  4. 请参阅更新的工作代码段:

    .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>