导航右侧的浮动登录表单

时间:2019-01-31 21:34:59

标签: html css navigation navbar

我正在尝试使导航项居中并使快速登录表单与导航栏的右侧对齐,我尝试了float:right,但是将其添加到下一行,显示:inline-block也不高兴。

我错过了一些非常明显的东西,还是仅用HTML和CSS很难做到?

谢谢

任何帮助将不胜感激。

Example:

<h1 class="class1">First</h1>
<h1>Second</h1>
<h1>Third</h1>

h1:not([class]) { 
  color: red;
}
nav#navBar {
  text-align: center;
  margin-top: -8px;
  margin-left: -8px;
  border-bottom: 2px solid #191919;
}

nav#navBar ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

nav#navBar ul li {
  font-weight: 600;
  font-size: 14px;
  margin-top: 5px;
  display: inline-block;
  font-family: 'Raleway';
  font-variant: small-caps;
}

nav#navBar ul li a, visited {
  color: #191919;
  text-decoration: none;
  padding: 25px;
  display: block;
}

nav#navBar ul li a:hover {
  color: grey;
  margin-bottom: -2px;
  border-bottom: 2px solid grey;
}

nav#navBar ul.loginForm {
  display: inline-block;
  float: right;
}

3 个答案:

答案 0 :(得分:0)

对不起。这对您有帮助吗?我希望是这样:)

enter image description here

nav#navBar {
  text-align: center;
  margin-top: -8px;
  margin-left: -8px;
  border-bottom: 2px solid #191919;
  display: -webkit-flex;
  /* Safari */
  -webkit-flex-wrap: wrap;
  /* Safari 6.1+ */
  display: flex;
  flex-wrap: wrap;
}

nav#navBar ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

nav#navBar ul li {
  font-weight: 600;
  font-size: 14px;
  margin-top: 5px;
  display: inline-block;
  font-family: 'Raleway';
  font-variant: small-caps;
}

nav#navBar ul li a,
visited {
  color: #191919;
  text-decoration: none;
  padding: 25px;
  display: block;
}

nav#navBar ul li a:hover {
  color: grey;
  margin-bottom: -2px;
  border-bottom: 2px solid grey;
}

nav#navBar ul.loginForm {
  margin-top: 25px;
  flex-grow: 1;
  display: inline-block;
  float: right;
}

ul form {
  float: right;
}
<nav id="navBar">
  <ul>
    <li><a href="#">HOME</a></li>
    <li><a href="#">CONTENT</a></li>
    <li><a href="#">PROJECTS</a></li>
    <li><a href="#">CONTACT</a></li>
  </ul>

  <ul class="loginForm">
    <form>
      <input type="text" placeholder="Insert username" />
      <input type="password" placeholder="Insert password" />
      <button type="submit">Login</button>
    </form>
  </ul>
</nav>

答案 1 :(得分:0)

您可以如下更改css。

nav#navBar {
    margin-top: -8px;
    margin-left: -8px;
    border-bottom: 2px solid #191919;
    display: table;
    width: 100%;
}

nav#navBar ul {
    list-style-type: none;
    padding: 0;
    margin: 0;
    display: table-cell;
    vertical-align: middle;
}

nav#navBar ul li {
    font-weight: 600;
    font-size: 14px;
    margin-top: 5px;
    display: inline-block;
    font-family: 'Raleway';
    font-variant: small-caps;
}

nav#navBar ul li a, visited {
    color: #191919;
    text-decoration: none;
    padding: 25px;
    display: block;
}

nav#navBar ul li a:hover {
    color: grey;
    margin-bottom: -2px;
    border-bottom: 2px solid grey;
}

nav#navBar ul.loginForm {
    display: table-cell;
    vertical-align: middle;
    text-align: right;
    padding-right: 25px;
}

并且html中没有任何变化。

答案 2 :(得分:-1)

在nav#navBar行中尝试使用CSS

display: -webkit-flex; /* Safari */
-webkit-flex-wrap: wrap; /* Safari 6.1+ */
display: flex;
flex-wrap: wrap;