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
1. Display effect: 2, html structure <div clas...
1. Install JDK 1.1 Check whether the current virt...
Project scenario: Dark Horse Vue project manageme...
This article example shares the specific code of ...
Copy code The code is as follows: <iframe id=&...
Preface: In some previous articles, we often see ...
The page length in the project is about 2000px or...
1. Install SVN server yum install subversion 2. C...
1. Build the basic image of jmeter The Dockerfile...
Uninstall MySQL 1. In the control panel, uninstal...
The MySQL explain command can analyze the perform...
Table of contents Create a new user Authorize new...
Some web pages may not look large but may be very...
The Internet is an organism that is constantly ev...
1. Find mysqldump.exe in the MySQL installation p...