导航栏中的搜索输入

Search input in navigation bar

我正在尝试在我网站的导航栏中实现 input/search-field。我非常希望它像 YouTube 导航栏一样工作,其中输入字段位于左侧徽标和右侧链接之间。但也使得输入字段的宽度是动态的,并在浏览器页面宽度减小时压缩。

我不擅长 CSS 并且我什至能够让搜索字段位于导航栏内的唯一方法是将位置设置为绝对(在输入字段 class).我知道您可以使用 CSS 以多种不同的方式实现相同的结果,而且我知道摆弄位置是正确的错误方法,如果它也是动态的。

请在下面找到一个 JSFiddle 和 html/CSS 代码。我正在使用 MaterializeCSS 创建网站,当浏览器 window 低于特定阈值时,'brand-logo' class 用于使徽标居中。然后我在 ul 上使用 class 'hide-on-med-and-down' 使链接在徽标居中时消失。但是,我删除了下面 JSFiddle 中的 'hide-on-med-and-below' class 以使其更加清楚搜索输入当前的工作方式。

https://jsfiddle.net/gwm6e031/19/

  <nav>
    <div class="nav-wrapper">
      <a href="#" class="brand-logo">
        <div class="logo-text" href="#"><object href="#" class="logo-svg"></object>LOGO</div>
      </a>
      <ul class="right hide-on-med-and-down">
        <li><a href="#">Link 1</a></li>
        <li><a href="#">Link 2</a></li>
        <li><a href="#">Link 3</a></li>
        <li><a href="#">Link 4</a></li>
      </ul>
    </div>
    <div class="input-field hide-on-med-and-down">
      <input type="text" class="autocomplete z-depth-0" id="autocomplete-input" placeholder="Search.." />
    </div>
  </nav>

CSS代码:

  body,
  html {
    height: 100%;
    width: 100%;
    margin: 0;
    font-size: 16px;
    background-color: var(--main-bg-color);
  }

  /* Navigation bar */

  nav {
    position: fixed;
    background: rgba(0, 0, 0, 0.4);
    z-index: 20;
    height: 80px;
    line-height: 80px;
  }

  nav .nav-wrapper {
    padding: 0px 50px;
  }

  nav .logo-text {
    padding-top: 15px;
    font-family: "Raleway", sans-serif;
    font-size: 38px;
    line-height: 50px;
    font-weight: bold;
    overflow: hidden;
  }

  nav .logo-svg {
    width: 50px;
    height: 50px;
    float: left;
    background-color: #fff;
  }

  nav .right {
  }

  nav ul a {
    font-family: 'Montserrat', sans-serif;
    font-size: 12px;
    color: #fff;
    text-transform: uppercase;
    display: block;
  }

  nav ul a:focus {
    text-decoration: underline;
  }

  nav li a:hover {
    background: #006699;
  }

  /* Search bar TOP */
  nav .input-field {
    position: absolute;
    bottom: 0px;
    left: 420px;
    width: 22%;
    max-width: 300px;
  }

  nav input {
    font-family: "Lato", sans-serif !important;
    height: 20px !important;
    background: #545a69 !important;
    background: linear-gradient( rgba(84, 90, 105, 0.9), rgba(84, 90, 105, 0.9)) !important;
    border: none !important;
    font-size: 14px !important;
    color: #fff !important;
    padding: 5px 0px 5px 5px !important;
    padding-left: 15px !important;
    -webkit-border-radius: 5px !important;
    -moz-border-radius: 5px !important;
    border-radius: 5px !important;
  }

请试试这段代码,我尽量写得尽可能简单,这样您就可以轻松地根据需要更改 CSS 参数。 (看看 HTML,你会注意到我已经将输入块代码的位置移到 ul 列表上方。)

<!DOCTYPE html>
<html lang="en">
<head>
  <title>title</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="description" content="" />
  <meta name="keywords" content="" />
  <meta name="robots" content="index,follow" />
  <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.css">
</head>
<style>
@import url('https://fonts.googleapis.com/css?family=Montserrat:300,400,500,600,700');
@import url('https://fonts.googleapis.com/css?family=Raleway');
@import url('https://fonts.googleapis.com/css?family=Roboto');
:root {
  --main-bg-color: #121a22;
  --main-bg-color-gradient: linear-gradient( rgba(18, 26, 34, 0.8), rgba(18, 26, 34, 0.8));
}

body,
html {
  height: 100%;
  width: 100%;
  margin: 0;
  font-size: 16px;
  background-color: var(--main-bg-color);
}

/* Navigation bar */
nav {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
}
.nav-wrapper {
    display: flex;
    max-height: 50px;
}

.logo-text {
    display: flex;
    width: 300px;
    background-color: #e5e5e5;
    align-items: center;
}
.logo-svg {
    display: block;
    width: 50px;
    height: 50px;
    background-color: white;
    margin-right: 10px;
}
.brand-logo {
    text-decoration: none;
}
.input-field {
    display: flex;
    align-items: center;
    width: calc(100% - 600px);
}

input#autocomplete-input {
    display: block;
    margin: 0 auto;
    width: 90%;
    max-width: 700px;
    height: 30px;
}
.nav-wrapper ul {
    display: flex;
    justify-content: space-around;
    align-items: center;
    width: 300px;
    padding: 0;
    margin: 0;
    background-color: #e5e5e5;
}
.nav-wrapper ul li {
    list-style: none;
    height: 100%;
    background-color: transparent;
    line-height: 50px;
    padding: 0 10px;
    transition: background-color .3s ease;
}
.nav-wrapper ul li:hover {
    background-color: white;
}
.nav-wrapper ul li a {
    text-decoration: none;
}
</style>
<body>
    <nav>
      <div class="nav-wrapper">
        <a href="#" class="brand-logo">
          <div class="logo-text" href="#"><object href="#" class="logo-svg"></object>LOGO</div>
        </a>
        <div class="input-field">
            <input type="text" class="autocomplete z-depth-0" id="autocomplete-input" placeholder="Search.." />
        </div>
        <ul class="right">
          <li><a href="#">Link 1</a></li>
          <li><a href="#">Link 2</a></li>
          <li><a href="#">Link 3</a></li>
          <li><a href="#">Link 4</a></li>
        </ul>
      </div>
    </nav>


</body>
</html>