如何在菜单导航中添加类别搜索栏并将其居中



我创建了一个下拉菜单和一个带有类别的搜索栏(如亚马逊),我正在尝试合并它们,但结果很糟糕。你能帮我把下面的搜索框居中到菜单栏中吗?

这是菜单的代码:

.fa {
  line-height: 1em;
}
ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  background-color: black;
}
/*hack stack-overflow*/
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: gray;
}
li#login {
  position: relative;
}
li#login form {
  display: none;
  position: absolute;
  z-index: 999;
  background-color: gray;
  padding: 20px;
  border-radius: 20px 0px 20px 20px;
  right: 0;
  box-shadow: 5px 5px 5px black;
}
li#login:hover form {
  display: block;
}
input.login_field {
  margin: 10px;
}
#submit-div {
  text-align: center;
}
<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 id="search">
    </li>
    <li class="right"><a class="fa fa-user-plus" href="">&nbsp;Signup</a>
    </li>
    <li id="login" class="right">
      <a class="fa fa-sign-in" 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>

这里是搜索框的代码:

$border-color:orange;
 #form-wrapper {
  width: 50%;
  height: 40px;
}
.nav-list {
  padding: 10px 25px 10px 5px;
  position: relative;
  float: left;
  border: 1px solid $border-color;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
}
#dropdown {
  cursor: pointer;
  position: absolute;
  height: 100%;
  left: 0;
  top: 0;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  border: 1px solid $border-color;
}
#dropdown:hover {
  background-color: lightgray;
}
.current-selection {
  display: inline-block;
  font-size: 14px;
}
.in-wrap {
  overflow: hidden;
  height: 100%;
}
#search-box {
  width: 100%;
  height: 36px;
  border: 1px solid $border-color;
  border-left: none;
  border-right: none;
  line-height: 25px;
  font-size: 18px;
  padding-left: 100px;
}
#search-box:focus {
  outline: none;
}
.go-button {
  float: right;
  height: 100%;
  background-color: orange;
  border: 1px solid $border-color;
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
  margin: 0;
  padding: 0 15px;
}
.go-button:hover {
  background-color: rgb(255, 115, 0);
  cursor: pointer;
}
<link href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" />
<div id="form-wrapper">
  <button class="go-button fa fa-search"></button>
  <span class="nav-list">
  <span class="current-selection">
  </span>
  <select id="dropdown">
    <option value="books-and-ebooks">Books & eBooks</option>
    <option value="audiobooks">Audiobooks</option>
    <option value="dvds">DVDs</option>
    <option value="other-resources">Other Resources</option>
    <option value="random">Random</option>
  </select>
  </span>
  <div class="in-wrap">
    <input type="text" name="query" id="search-box">
  </div>
</div>

但是,搜索框在代码笔上看起来不同(更好):在此处输入链接说明

这就是你想要的?

Codepen

上的搜索栏的问题在于,在 Codepen 上,CSS 被编译为 SCSS

你会注意到Codepen上的CSS显示CSS (SCSS),并且在它的右侧有一个小的View Complied按钮。单击它将为您提供CSS,并通过使用它获得以下结果。

至于菜单,我已经将两者结合起来了,但坦率地说,您的html有些缺陷 - 我在搜索栏中添加了最小宽度,否则它不会出现,我希望您不打算在手机上使用它,否则您会遇到问题。

但它有效 - 希望这就是你想要的!

请记住 - 此处预览的宽度会破坏它 - 单击整页,您将看到您想要的内容。

.fa {
  line-height: 1em;
}
ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  background-color: black;
}
/*hack stack-overflow*/
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: gray;
}
li#login {
  position: relative;
}
li#login form {
  display: none;
  position: absolute;
  z-index: 999;
  background-color: gray;
  padding: 20px;
  border-radius: 20px 0px 20px 20px;
  right: 0;
  box-shadow: 5px 5px 5px black;
}
li#login:hover form {
  display: block;
}
input.login_field {
  margin: 10px;
}
#submit-div {
  text-align: center;
}
#form-wrapper {
  width: 50%;
  height: 40px;
}
.nav-list {
  padding: 10px 25px 10px 5px;
  position: relative;
  float: left;
  border: 1px solid orange;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
}
#dropdown {
  cursor: pointer;
  position: absolute;
  height: 100%;
  left: 0;
  top: 0;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  border: 1px solid orange;
}
#dropdown:hover {
  background-color: lightgray;
}
.current-selection {
  display: inline-block;
  font-size: 14px;
}
.in-wrap {
  overflow: hidden;
  height: 100%;
}
#search-box {
  width: 100%;
  min-width: 400px;
  height: 36px;
  border: 1px solid orange;
  border-left: none;
  border-right: none;
  line-height: 25px;
  font-size: 18px;
  padding-left: 100px;
}
#search-box:focus {
  outline: none;
}
.go-button {
  float: right;
  height: 100%;
  background-color: orange;
  border: 1px solid orange;
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
  margin: 0;
  padding: 0 15px;
}
.go-button:hover {
  background-color: #ff7300;
  cursor: pointer;
}
<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 id="search">
      <div id="form-wrapper">
        <button class="go-button fa fa-search"></button>
        <span class="nav-list">
  <span class="current-selection">
  </span>
        <select id="dropdown">
    <option value="books-and-ebooks">Books & eBooks</option>
    <option value="audiobooks">Audiobooks</option>
    <option value="dvds">DVDs</option>
    <option value="other-resources">Other Resources</option>
    <option value="random">Random</option>
  </select>
        </span>
        <div class="in-wrap">
          <input type="text" name="query" id="search-box">
        </div>
      </div>
    </li>
    <li class="right"><a class="fa fa-user-plus" href="">&nbsp;Signup</a>
    </li>
    <li id="login" class="right">
      <a class="fa fa-sign-in" 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>

最新更新