将导航栏的元素移到最左侧

时间:2018-02-15 05:17:13

标签: html css navbar

我有以下导航栏。我想在导航栏的最左侧移动它的登录/注册组件,但无法找到方法。可能的解决方案是什么,我在登录li元素上尝试了padding-left:0,但这似乎不起作用。任何帮助都将不胜感激

HTML:



body {
  margin: 0;
  background: #222;
  font-weight: 300;
  background-image: url('bg.jpeg');
}

header {
  background: #d9c2ac;
  position: relative;
}

header::after {
  content: '';
  display: table;
  clear: both;
}

nav {
  float: left;
}

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

nav li {
  display: inline-block;
  margin-left: 70px;
  padding-top: 30px;
  position: relative;
}

nav ul li a {
  color: #444;
  text-decoration: none;
  text-transform: uppercase;
  font-size: 14px;
  font-weight: bold;
}

nav a:hover {}

nav a::before {
  content: '';
  display: block;
  height: 5px;
  width: 0%;
  background-color: #444;
  transition: all ease-in-out 500ms;
}

nav a:hover::before {
  width: 100%;
}

form .form {
  float: right;
}

.search-bar {
  float: right;
}

ul li:last-child {
  position: absolute;
  right: 0;
  bottom: 0;
  margin: 15px;
  margin-bottom: 0px;
  padding: 0;
}

<body>
  <header>
    <div class="container" id="#home">
      <nav>
        <ul>
          <li id="login"> <a href="#login" style="text-align: left;"> Login/Register </a> </li>
          <li> <a href="#home"> Home </a></li>
          <li> <a href="#about"> About </a></li>
          <li> <a href="#"> Services </a></li>
          <li> <a href="#Products"> Products </a></li>
          <li> <a href="#contact"> Contact Us </a></li>
          <li>
            <form class="form"> <input type="text" name="Search" placeholder="Search"> </form>
          </li>
        </ul>
      </nav>
    </div>
  </header>
&#13;
&#13;
&#13;

Navbar

3 个答案:

答案 0 :(得分:1)

您可以使用伪css来对齐项目。

nav li:first-child {
  margin-left: 0;
}

您可以查看here

答案 1 :(得分:1)

使用rm-auto将边距设置为0

&#13;
&#13;
.navbar-tan
{
  background-color:tan;
}
&#13;
<html>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<nav class="navbar navbar-expand-sm navbar-tan" >
  <ul class="navbar-nav  mr-auto">
    <li class="nav-item active">
      <a class="nav-link" href="#">Login/Register</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Home</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">About</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Services</a>
    </li>
     <li class="nav-item">
      <a class="nav-link" href="#">Products</a>
    </li>
     <li class="nav-item">
      <a class="nav-link disabled" href="#">Contact Us</a>
    </li>
    </ul>
    <ul class="navbar-nav">
    
      <li class="nav-item">
      <input type="text" name="" placeholder="Search" class="form-control">
    </li>
  </ul>
</nav>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

尝试在此处使用 Flexbox ...并使用margin实用程序来对齐第一个和最后一个项...

...将margin-right:auto应用于第一个元素以将其对齐最左侧角落

...并将margin-left:auto应用于最后一个元素以使其最右边对齐..

我也更改了你的一些css并删除了所有float值。此处也不需要在搜索框中使用position:absolute

Stack Snippet

&#13;
&#13;
body {
  margin: 0;
  background: #222;
  font-weight: 300;
  background-image: url('bg.jpeg');
}

header {
  background: #d9c2ac;
  position: relative;
}

nav ul {
  margin: 0;
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  padding: 20px 0;
}

nav li {
  position: relative;
  margin: 0 10px;
}

nav ul li a {
  color: #444;
  text-decoration: none;
  text-transform: uppercase;
  font-size: 12px;
  font-family: verdana;
}

nav a:hover {}

nav a::before {
  content: '';
  display: block;
  height: 5px;
  width: 0%;
  background-color: #444;
  transition: all ease-in-out 500ms;
}

nav a:hover::before {
  width: 100%;
}

nav ul li:first-child {
  margin-right: auto;
}

nav ul li:last-child {
  margin-left: auto;
}
&#13;
<header>
  <div class="container" id="#home">
    <nav>
      <ul>
        <li id="login"> <a href="#login" style="text-align: left;"> Login/Register </a> </li>
        <li> <a href="#home"> Home </a></li>
        <li> <a href="#about"> About </a></li>
        <li>
          <form class="form"> <input type="text" name="Search" placeholder="Search"> </form>
        </li>
      </ul>
    </nav>
  </div>
</header>
&#13;
&#13;
&#13;