CSS sample code with search navigation bar

CSS sample code with search navigation bar

This article shows you how to use CSS to create a navigation bar with search.

The following examples are all responsive.

You can first look at the effect diagram:

Create a search bar

<div class="topnav">
  <a class="active" href="#home">Home</a>
  <a href="#about">About</a>
  <a href="#contact">Contact Us</a>
  <input type="text" placeholder="Search..">
</div>
/* Add black background color to the top navigation bar */
.topnav {
    overflow: hidden;
    background-color: #e9e9e9;
}
 
/* Set the link style of the navigation bar */
.topnav a {
    float: left;
    display: block;
    color: black;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
}
 
/* Change the color of the link on hover */
.topnav a:hover {
    background-color: #ddd;
    color: black;
}
 
/*Highlight the currently selected element*/
.topnav a.active {
    background-color: #2196F3;
    color: white;
}
 
/* Set the search box style of the navigation bar*/
.topnav input[type=text] {
    float: right;
    padding: 6px;
    border: none;
    margin-top: 8px;
    margin-right: 16px;
    font-size: 17px;
}
 
/* When the screen width is less than 600px, the menu options and search box are displayed vertically stacked*/
@media screen and (max-width: 600px) {
    .topnav a, .topnav input[type=text] {
        float: none;
        display: block;
        text-align: left;
        width: 100%;
        margin: 0;
        padding: 14px;
    }
    .topnav input[type=text] {
        border: 1px solid #ccc;
    }
}

CSS search navigation bar - with submit button

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Rookie Tutorial (runoob.com)</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box;}

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #e9e9e9;
}

.topnav a {
  float: left;
  display: block;
  color: black;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.topnav a.active {
  background-color: #2196F3;
  color: white;
}

.topnav .search-container {
  float: right;
}

.topnav input[type=text] {
  padding: 8px;
  margin-top: 8px;
  font-size: 17px;
  border: none;
}

.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;
}

@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;  
  }
}
</style>
</head>
<body>

<div class="topnav">
  <a class="active" href="#home">Home</a>
  <a href="#about">About</a>
  <a href="#contact">Contact Us</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>

<div style="padding-left:16px">
   <h2>Responsive search menu</h2>
   <p>There is a search box in the navigation bar. </p>
   <p>Resize your browser window to see the effect. </p>
</div>

</body>
</html>

CSS search navigation bar - with search icon

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Rookie Tutorial (runoob.com)</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
* {box-sizing: border-box;}

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #e9e9e9;
}

.topnav a {
  float: left;
  display: block;
  color: black;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.topnav a.active {
  background-color: #2196F3;
  color: white;
}

.topnav .search-container {
  float: right;
}

.topnav input[type=text] {
  padding: 6px;
  margin-top: 8px;
  font-size: 17px;
  border: none;
}

.topnav .search-container button {
  float: right;
  padding: 6px 10px;
  margin-top: 8px;
  margin-right: 16px;
  background: #ddd;
  font-size: 17px;
  border: none;
  cursor: pointer;
}

.topnav .search-container button:hover {
  background: #ccc;
}

@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;  
  }
}
</style>
</head>
<body>

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

<div style="padding-left:16px">
  <h2>Responsive search menu</h2>
  <p>There is a search box in the navigation bar. </p>
  <p>Resize your browser window to see the effect. </p>
</div>

</body>
</html>

This is the end of this article about the sample code of CSS with search navigation bar. For more relevant CSS search navigation bar content, please search the previous articles of 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  JavaScript function encapsulates random color verification code (complete code)

>>:  Detailed explanation of simple html and css usage

Recommend

How to deploy Vue project using Docker image + nginx

1. Packaging Vue project Enter the following name...

How to use JSX to implement Carousel components (front-end componentization)

Before we use JSX to build a component system, le...

HTML tag dl dt dd usage instructions

Basic structure: Copy code The code is as follows:...

An article to deal with Mysql date and time functions

Table of contents Preface 1. Get the current time...

Vue implements QR code scanning function (with style)

need: Use vue to realize QR code scanning; Plugin...

Installation and configuration tutorial of MongoDB under Linux

MongoDB Installation Choose to install using Yum ...

Detailed tutorial on installing JDK1.8 on Linux

1. Cleaning before installation rpm -qa | grep jd...

Docker creates MySQL explanation

1. Download MySQL Image Command: docker pull mysq...

Vue uses element-ui to implement menu navigation

This article shares the specific code of Vue usin...

Specific method of viewing user authorization information in mysql

Specific method: 1. Open Command Prompt 2. Enter ...

Some common properties of CSS

CSS background: background:#00ffee; //Set the back...

Content-type description, that is, the type of HTTP request header

To learn content-type, you must first know what i...

Use CSS to easily implement some frequently appearing weird buttons

background In the group, some students will ask r...