如何创建:菜单中的输入字段

学习如何创建其中包含输入字段的导航菜单。

Home About

Try it yourself

如何在导航栏中添加输入字段

第一步 - 添加 HTML:

<div class="topnav">
  <a class="active" href="#home">Home</a>
  <a href="#about">About</a>
  <a href="#contact">Contact</a>
  <div class="search-container">
    <form action="/action_page.php">
      <input type="text" placeholder="Search.." name="search">
      <button type="submit">Submit</button>
    </form>
  </div>
</div>

Zweiter Schritt - Fügen Sie CSS hinzu:

* {box-sizing: border-box;}
/* Setzen Sie den Stil der Navigationsleiste */
.topnav {
  overflow: hidden;
  background-color: #e9e9e9;
{}
/* Setzen Sie den Stil der Navigationsleistenlinks */
.topnav a {
  float: left;
  display: block;
  color: black;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
{}
/* Setzen Sie den Stil, wenn der Mauszeiger auf dem Navigationsleistenlink steht */
.topnav a:hover {
  background-color: #ddd;
  color: black;
{}
/* Setzen Sie den Stil des aktuellen/aktiven Links */
.topnav a.active {
  background-color: #2196F3;
  color: white;
{}
/* Setzen Sie den Stil des Suchfeldcontainers */
.topnav .search-container {
  float: right;
{}
/* Setzen Sie den Stil des Eingabefelds in der Navigationsleiste */
.topnav input[type=text] {
  padding: 6px;
  margin-top: 8px;
  font-size: 17px;
  border: none;
{}
/* Setzen Sie den Stil des Buttons im Suchfeldcontainer */
.topnav .search-container button {
  float: right;
  padding: 6px;
  margin-top: 8px;
  margin-right: 16px;
  background: #ddd;
  font-size: 17px;
  border: none;
  cursor: pointer;
{}
.topnav .search-container button:hover {
  background: #ccc;
{}
/* Fügen Sie die Reaktionsfähigkeit hinzu - In kleinen Bildschirmen wird die Navigationsleiste vertikal anstatt horizontal angezeigt */
@media screen and (max-width: 600px) {
  .topnav .search-container {
    float: none;
  {}
  .topnav a, .topnav input[type=text], .topnav .search-container button {
    float: none;
    display: block;
    text-align: left;
    width: 100%;
    margin: 0;
    padding: 14px;
  {}
  .topnav input[type=text] {
    border: 1px solid #ccc;
  {}
{}

Try it yourself