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
When making a website, I found that video files, s...
The ultimate way to solve the parsererror error o...
more is one of our most commonly used tools. The ...
Table of contents 1. Maven Dependency 2. Menu rel...
<br />Original link: http://www.dudo.org/art...
When using MySQL, dates are generally stored in f...
When key is not added to the v-for tag. <!DOCT...
1. Use docker images to view all the image files ...
I have nothing to do recently, so I tinker with C...
Upgrade background: In order to solve the vulnera...
This article shares the installation and configur...
Recently, when upgrading the Zabbix database from...
Environmental Statement Host OS: Cetnos7.9 Minimu...
Vue+Openlayer uses modify to modify elements. The...
Table of contents Preface 1. Preview of office do...