How to Create a Responsive Website Header - Part 4
In this detailed web development tutorial, I'll teach you how to create a responsive website header with dropdown search and share bars using HTML, CSS, and JavaScript.
This article, which is part 4 of a 5-part series of Web Development tutorials, will guide you through the process of creating a website header. We'll use HTML, CSS, and JavaScript to construct a header layout with Search and Share Bars that open and close as needed. In this example, we'll examine a complete web page demonstration before taking a detailed look at the concepts and code that bring it to life.
This article builds upon topics discussed in How to Create a Responsive Website Layout.
In this article, we'll use our standard HTML Document Structure and CSS Naming Convention.
Skip Ahead
Looking for something specific? Select a topic in this article to read more:
Search and Share Bars
In this example, we'll build upon our Accordion Navigation Menu design to include search and share bars that open and close as needed:
A search and share bar provides visitors with an enhanced ability to find content on our website and to communicate it with a larger audience using social media platforms. When a visitor submits a search query, all of the relevant pages within our website domain are returned as standard search engine results. When a visitor clicks on a social media link, our website is automatically shared to the corresponding platform.
On small screens, the search and share menus are simply added to the end of the existing site menu.
On medium and large screens, the search and share menus behave as dropdowns. When a visitor selects a button from the search and share bar, the corresponding menu opens. When a visitor deselects a button from the search and share bar, the corresponding menu closes.
Header Structure and Style
First, we create our header area.
The construction of the header area is similar to our Accordion Navigation Menu design. But we add:
- CSS to allow the dropdown behavior
Let's start with the HTML used to create the header area for our responsive search and share bar design:
HTML
<!-- Header -->
<header class="header">
<!-- Title -->
...
<!-- Navigation Group -->
...
</header>
Here we create our header area using the
<header>
element and assign it:
-
The
class="header"
attribute to control the layout of the header area
Next the CSS:
CSS
* {
box-sizing: border-box;
margin: 0;
border: 0;
padding: 0;
}
body {position: relative;}
/* Page */
.page {
max-width: 640px;
margin: 0 auto;
}
@media (min-width: 960px) {
.page {max-width: 960px;}
}
/* Header */
.header {
font-size: 0;
text-align: center;
}
First, we create a CSS rule to control the default layout and style of all elements (unless otherwise specified). This rule applies the "border box" sizing model and resets margins, borders, and padding to 0.
Next we create a CSS rule to control the layout of the
body
element.
We include:
-
The
position: relative;
property to allow the subnavigation areas to be positioned relative to thebody
element
Then, we create the
.page
CSS rule to control the layout of our page areas so that:
- The maximum width is 640px on small and medium screens
- The maximum width is 960px on large screens
Finally, we create the
.header
CSS rule to control the layout of our header area.
We include:
-
The
font-size: 0;
property to prevent browsers from adding undesirable whitespace around and between the title heading, navigation menu items, and subnavigation menu items (font size is redefined later so that all text is rendered to the correct size)
Title Structure and Style
The first item inside the header area is our title area.
The construction of the title area is identical to our Accordion Navigation Menu design.
Let's start with the HTML used to create the title area for our responsive search and share bar design:
HTML
<!-- Title -->
<div class="title">
<div class="page title_container">
<h1 class="title_heading">
<a href="#home"
class="title_heading_link">
My Website</a></h1>
<button type="button"
onclick="navToggle()"
class="title_button">
<i class="title_button_icon fa fa-bars"></i></button>
</div>
</div>
First, we create our title area using the
<div>
element and assign it:
-
The
class="title"
attribute to control the style of the title area
Inside the title area we create our title container using the
<div>
element and assign it:
-
The
class="page title_container"
attribute to control the layout of the title container
The first item inside the title container is our title heading.
We create this item using the
<h1>
element and assign it:
-
The
class="title_heading"
attribute to control the style of the title heading
Inside the title heading we use the
<a>
element to create a title heading link that directs visitors to our home page when clicked.
We assign it:
-
The
href="#home"
attribute to specify the destination of the title heading link -
The
class="title_heading_link"
attribute to control the style of the title heading link
The second item inside the title container is the title button that opens and closes our accordion navigation menu on small screens.
We create this item using the
<button>
element and assign it:
-
The
onclick="navToggle()"
attribute to execute the JavaScript function that opens and closes the accordion navigation menu when clicked -
The
class="title_button"
attribute to control the layout and style of the title button
Inside the title button we create our title button icon using the
<i>
element and assign it:
-
The
class="title_button_icon fa fa-bars"
attribute to control the style of the title button icon
Next the CSS:
CSS
/* Title */
.title {background: #4040BF;}
.title_container {
position: relative;
padding: 0 56px;
}
.title_heading {
display: inline-block;
font: bold 24px / 1.5 sans-serif;
}
.title_heading_link {
display: block;
padding: 16px;
text-decoration: none;
color: #FFFFFF;
}
.title_button {
display: none;
width: 56px;
height: 68px;
position: absolute;
right: 0;
top: 0;
padding: 16px;
font: 24px / 1.5 sans-serif;
color: #FFFFFF;
background: transparent;
cursor: pointer;
}
.title_button_icon {width: 24px;}
First, we create the
.title
CSS rule to control the style of our title area.
Next we create the
.title_container
CSS rule to control the layout of our title container.
We include:
-
The
position: relative;
property to allow the title button to be positioned relative to the title container -
The
padding: 0 56px;
property to prevent the title heading and title button from overlapping
Then, we create the
.title_heading
CSS rule to control the style of our title heading.
Next we create the
.title_heading_link
CSS rule to control the style of our title heading link.
Then, we create the
.title_button
CSS rule to control the layout and style of our title button.
We include:
-
The
display: none;
property to hide the title button on devices that are not JavaScript-enabled -
The
position: absolute; right: 0; top: 0;
properties to position the title button in the top-right corner of the title container
Finally, we create the
.title_button_icon
CSS rule to control the style of our title button icon.
Navigation Group Structure and Style
The second item inside the header area is our navigation group.
Let's start with the HTML used to create the navigation group for our responsive search and share bar design:
HTML
<!-- Navigation Group -->
<nav class="nav">
<!-- Site Navigation -->
...
<!-- Search Subnavigation -->
...
<!-- Share Subnavigation -->
...
</nav>
Here we create our navigation group using the
<nav>
element and assign it:
-
The
class="nav"
attribute to control the navigation group
Next the CSS:
CSS
/* Navigation Group */
.nav {}
.nav.accordion {
overflow: hidden;
transition: height 0.25s;
}
Here we create the
.nav
CSS rule to control the navigation group.
To produce the accordion behavior, we create the
.accordion
CSS rule.
On small screens, our JavaScript function will add this CSS rule to the navigation group.
On medium and large screens, our JavaScript function will remove this CSS rule from the navigation group.
When added, this CSS rule applies:
-
The
overflow: hidden;
property to hide the content of the accordion navigation group when it is closed -
The
transition: height 0.25s;
property to allow the height of the accordion navigation group to change smoothly over a 0.25-second duration
Navigation Structure and Style
The first item inside the navigation group is our navigation area.
The construction of the navigation area is similar to our Accordion Navigation Menu design. But we add:
- HTML and CSS to create the buttons used to open and close the search and share menus
Let's start with the HTML used to create the navigation area for our responsive search and share bar design:
HTML
<!-- Site Navigation -->
<div class="nav-1">
<div class="page nav-1_container-1">
<ul class="nav-1_menu">
<li class="nav-1_menu_item">
<a href="#home"
class="nav-1_menu_link">
Home</a></li>
<li class="nav-1_menu_item">
<a href="#about"
class="nav-1_menu_link">
About</a></li>
<li class="nav-1_menu_item">
<a href="#products"
class="nav-1_menu_link">
Products</a></li>
<li class="nav-1_menu_item">
<a href="#services"
class="nav-1_menu_link">
Services</a></li></ul>
<div class="nav-1_container-2">
<button type="button"
onclick="subnavToggle(0)"
class="nav-1_button">
<i class="nav-1_button_icon fa fa-search"></i></button>
<button type="button"
onclick="subnavToggle(1)"
class="nav-1_button">
<i class="nav-1_button_icon fa fa-share-alt"></i></button>
</div>
</div>
</div>
First, we create our navigation area using the
<div>
element and assign it:
-
The
class="nav-1"
attribute to control the style of the navigation area
Inside the navigation area we create our navigation container using the
<div>
element and assign it:
-
The
class="page nav-1_container-1"
attribute to control the layout of the navigation container
The first item inside the navigation container is our navigation menu.
We create this item using the
<ul>
element and assign it:
-
The
class="nav-1_menu"
attribute to control the layout of the navigation menu
Inside the navigation menu we create our navigation menu items using the
<li>
element and assign each:
-
The
class="nav-1_menu_item"
attribute to control the layout and style of the navigation menu item
And inside each navigation menu item we use the
<a>
element to create a navigation menu link that directs visitors to the corresponding page when clicked.
We assign each:
-
The
href="..."
attribute to specify the destination of the navigation menu link -
The
class="nav-1_menu_link"
attribute to control the style of the navigation menu link
The second item inside the navigation container is our search and share bar container.
We create this item using the
<div>
element and assign it:
-
The
class="nav-1_container-2"
attribute to control the layout of the search and share bar container
The first item inside the search and share bar container is the search button that opens and closes our search menu on medium and large screens.
We create this item using the
<button>
element and assign it:
-
The
onclick="subnavToggle(0)"
attribute to execute the JavaScript function that opens and closes the search menu when clicked. Note that the function includes an index number in parenthesis to identify which subnavigation menu node we wish to open. The JavaScript index starts at 0. Since the search menu is the first node, its index number is 0. -
The
class="nav-1_button"
attribute to control the layout and style of the search button
The second item inside the search and share bar container is the share button that opens and closes our share menu on medium and large screens.
We create this item using the
<button>
element and assign it:
-
The
onclick="subnavToggle(1)"
attribute to execute the JavaScript function that opens and closes the share menu when clicked. Note that the function includes an index number in parenthesis to identify which subnavigation menu node we wish to open. The JavaScript index starts at 0. Since the share menu is the second node, its index number is 1. -
The
class="nav-1_button"
attribute to control the layout and style of the share button
Inside each button we create our button icon using the
<i>
element and assign each:
-
The
class="nav-1_button_icon fa ..."
attribute to control the style of the button icon
Next the CSS:
CSS
/* Navigation */
.nav-1 {background: #000000;}
.nav-1_container-1 {overflow: hidden;}
.nav-1_container-2 {
float: right;
overflow: hidden;
}
.nav-1_menu {}
.nav-1_menu_item {
display: block;
font: 16px / 1.5 sans-serif;
}
.nav-1_menu_link {
display: block;
padding: 16px;
text-decoration: none;
color: #FFFFFF;
}
.nav-1_button {
display: none;
width: 48px;
height: 56px;
float: left;
padding: 16px;
font: 16px / 1.5 sans-serif;
color: #FFFFFF;
background: transparent;
cursor: pointer;
}
.nav-1_button_icon {width: 16px;}
@media (min-width: 480px) {
.nav-1_menu {float: left;}
.nav-1_menu_item {display: inline-block;}
}
First, we create the
.nav-1
CSS rule to control the style of our navigation area.
Next we create the
.nav-1_container-1
CSS rule to control the layout of our navigation container.
We include:
-
The
overflow: hidden;
property to allow the navigation menu and search and share bar to be floated without collapsing the height of the navigation container
Then, we create the
.nav-1_container-2
CSS rule to control the layout of our search and share bar container.
We include:
-
The
float: right;
property to align the search and share bar container to the right of the navigation container -
The
overflow: hidden;
property to allow the search and share buttons to be floated without collapsing the height of the search and share bar container
Next we create the
.nav-1_menu
CSS rule to control the layout of our navigation menu.
Note that the navigation menu is floated to the left on medium and large screens.
Then, we create the
.nav-1_menu_item
CSS rule to control the layout and style of our navigation menu items.
Note that the navigation menu items are displayed vertically (as blocks) on small screens and horizontally (as inline blocks) on medium and large screens.
Next we create the
.nav-1_menu_link
CSS rule to control the style of our navigation menu links.
Then, we create the
.nav-1_button
CSS rule to control the layout and style of our search and share buttons.
We include:
-
The
display: none;
property to hide the search and share buttons on devices that are not JavaScript-enabled -
The
float: left;
property to float the search and share buttons to the left of the search and share bar container
Finally, we create the
.nav-1_button_icon
CSS rule to control the style of our search and share button icons.
Subnavigation Structure and Style
The next two items inside the navigation group are our subnavigation areas.
Let's start with the HTML used to create the search menu for our responsive search and share bar design:
HTML
<!-- Search Subnavigation -->
<div class="nav-2">
<form action="https://www.google.com/search"
target="_blank"
method="get"
class="page nav-2_search">
<div class="nav-2_search_container">
<input type="hidden"
name="sitesearch"
value="https://www.mywebsite.com">
<input type="text"
name="q"
class="nav-2_search_input">
<button type="submit"
class="nav-2_search_button">
<i class="nav-2_search_icon fa fa-search"></i></button></div></form>
</div>
First, we create our subnavigation area using the
<div>
element and assign it:
-
The
class="nav-2"
attribute to control the style of the subnavigation area
Inside the subnavigation area we create our search bar using the
<form>
element and assign it:
-
The
action="https://www.google.com/search"
attribute to specify the address of the search engine -
The
target="_blank"
attribute to open the search results in a new tab or window -
The
class="page nav-2_search"
attribute to control the layout of the search bar
Inside the search bar we create our search container using the
<div>
element and assign it:
-
The
class="nav-2_search_container"
attribute to control the layout of the search container
The first item inside the search container is a hidden input field that targets search results to our website domain.
We create this item using the
<input>
element and assign it:
-
The
type="hidden"
attribute to hide the input field -
The
value="https://www.mywebsite.com"
attribute to specify the website domain
The second item inside the search container is our search input.
We create this item using the
<input>
element and assign it:
-
The
type="text"
attribute to specify that the expected type of input is text -
The
class="nav-2_search_input"
attribute to control the layout and style of the search input
The third item inside the search container is our search button.
We create this item using the
<button>
element and assign it:
-
The
type="submit"
attribute to specify that the button submits the form data when clicked -
The
class="nav-2_search_button"
to control the layout and style of the search button
Inside the search button we create our search icon using the
<i>
element and assign it:
-
The
class="nav-2_search_icon fa fa-search"
attribute to control the style of the search icon
Next the HTML used to create the share menu for our responsive search and share bar design:
HTML
<!-- Share Subnavigation -->
<div class="nav-2">
<ul class="page">
<li class="nav-2_menu_item">
<a href="https://www.facebook.com/sharer.php?u=https://www.mywebsite.com"
target="_blank"
class="nav-2_menu_link">
<i class="nav-2_menu_icon fa fa-facebook-square"></i></a></li>
<li class="nav-2_menu_item">
<a href="https://twitter.com/share?url=https://www.mywebsite.com"
target="_blank"
class="nav-2_menu_link">
<i class="nav-2_menu_icon fa fa-twitter-square"></i></a></li>
<li class="nav-2_menu_item">
<a href="https://pinterest.com/pin/create/button/?url=https://www.mywebsite.com"
target="_blank"
class="nav-2_menu_link">
<i class="nav-2_menu_icon fa fa-pinterest-square"></i></a></li>
<li class="nav-2_menu_item">
<a href="https://www.linkedin.com/sharing/share-offsite/?url=https://www.mywebsite.com"
target="_blank"
class="nav-2_menu_link">
<i class="nav-2_menu_icon fa fa-linkedin-square"></i></a></li></ul>
</div>
First, we create our subnavigation area using the
<div>
element and assign it:
-
The
class="nav-2"
attribute to control the style of the subnavigation area
Inside the subnavigation area we create our subnavigation menu using the
<ul>
element and assign it:
-
The
class="page"
attribute to control the layout of the subnavigation menu
Inside the subnavigation menu we create our subnavigation menu items using the
<li>
element and assign each:
-
The
class="nav-2_menu_item"
attribute to control the layout and style of the subnavigation menu item
Inside each subnavigation menu item we use the
<a>
element to create a subnavigation menu link that shares our website to the corresponding social media platform when clicked.
We assign each:
-
The
href="..."
attribute to specify the destination of the subnavigation menu link -
The
class="nav-2_menu_link"
attribute to control the style of the subnavigation menu link
And inside each subnavigation menu link we create our subnavigation menu icon using the
<i>
element and assign each:
-
The
class="nav-2_menu_icon fa ..."
attribute to control the style of the subnavigation menu icon
And the CSS:
CSS
/* Subnavigation */
.nav-2 {background: #808080;}
.nav-2.dropdown {
width: 100%;
position: absolute;
overflow: hidden;
transition: height 0.25s;
}
.nav-2_menu_item {
display: inline-block;
font: 32px / 1.25 sans-serif;
}
.nav-2_menu_link {
display: block;
padding: 8px 16px;
text-decoration: none;
color: #FFFFFF;
}
.nav-2_menu_icon {width: 32px;}
.nav-2_search {padding: 8px 16px;}
.nav-2_search_container {position: relative;}
.nav-2_search_input {
width: 100%;
height: 40px;
border-radius: 20px;
padding: 8px 48px 8px 16px;
font: 16px / 1.5 sans-serif;
color: #404040;
background: #FFFFFF;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25) inset;
}
.nav-2_search_button {
width: 48px;
height: 40px;
position: absolute;
right: 0;
top: 0;
padding: 8px 16px;
font: 16px / 1.5 sans-serif;
color: #000000;
background: transparent;
cursor: pointer;
}
.nav-2_search_icon {width: 16px;}
First, we create the
.nav-2
CSS rule to control the style of our subnavigation areas.
To produce the dropdown behavior, we create the
.dropdown
CSS rule.
On small screens, our JavaScript function will remove this CSS rule from the subnavigation areas.
On medium and large screens, our JavaScript function will add this CSS rule to the subnavigation areas.
When added, this CSS rule applies:
-
The
width: 100%;
property to specify that the dropdown subnavigation menus expand the full width of the screen -
The
position: absolute;
properties to position the dropdown subnavigation menus relative to thebody
element -
The
overflow: hidden;
property to hide the content of the dropdown subnavigation menus when they are closed -
The
transition: height 0.25s;
property to allow the height of the dropdown subnavigation menus to change smoothly over a 0.25-second duration
Next we create the
.nav-2_menu_item
CSS rule to control the layout and style of our subnavigation menu items.
Then, we create the
.nav-2_menu_link
CSS rule to control the style of our subnavigation menu links.
Next we create the
.nav-2_menu_icon
CSS rule to control the style of our subnavigation menu icons.
Then, we create the
.nav-2_search
CSS rule to control the layout of our search bar.
Next we create the
.nav-2_search_container
CSS rule to control the layout of our search container.
We include:
-
The
position: relative;
property to allow the search button to be positioned relative to the search container
Then, we create the
.nav-2_search_input
CSS rule to control the layout and style of our search input.
We include:
-
The
padding: 8px 48px 8px 16px;
property to prevent the search input text and search button from overlapping
Next we create the
.nav-2_search_button
CSS rule to control the layout and style of our search button.
We include:
-
The
position: absolute; right: 0; top: 0;
properties to position the search button in the top-right corner of the search container
Finally, we create the
.nav-2_search_icon
CSS rule to control the style of our search icon.
Function
To complete our search and share bar design, we use JavaScript to add functionality to our layout. Keeping aligned with the principle of "progressive enhancement", our search and share menus remain fully functional for visitors without JavaScript-enabled devices. But for visitors with JavaScript-enabled devices, the dropdown behavior is added to our search and share menus to provide an improved user experience.
To produce this functionality, we add the dropdown behavior on medium and large screens and initially close the search and share menus by setting their height to 0. We also show the buttons required to toggle the search and share menus.
When the button is clicked to open the search menu, its height is set equal to the height of its content and the share menu is closed. When the button is clicked to close the search menu, its height is set back to 0.
When the button is clicked to open the share menu, its height is set equal to the height of its content and the search menu is closed. When the button is clicked to close the share menu, its height is set back to 0.
Let's take a look at a step-by-step breakdown of how we want the JavaScript to function:
When the page is loaded or resized:
-
If screen size is small:
- Add accordion behavior to navigation
- Collapse navigation
- Show navigation button
- Remove dropdown behavior from all instances of subnavigation
- Expand all instances of subnavigation
- Hide subnavigation button for all instances of subnavigation
-
If screen size is medium or large:
- Remove accordion behavior from navigation
- Expand navigation
- Hide navigation button
- Add dropdown behavior to all instances of subnavigation
- Collapse all instances of subnavigation
- Show subnavigation button for all instances of subnavigation
When navigation is toggled:
-
If navigation is closed:
- Expand navigation
-
If navigation is open:
- Collapse navigation
When subnavigation is toggled:
-
If selected instance of subnavigation is closed:
- Collapse all instances of subnavigation
- Send all instances of subnavigation to back
- Expand selected instance of subnavigation
- Bring selected instance of subnavigation to front
-
If selected instance of subnavigation is open:
- Collapse selected instance of subnavigation
- Send selected instance of subnavigation to back
Now the JavaScript that makes it happen:
JavaScript
var screenSize = window.matchMedia("(max-width: 479px)");
// Navigation Variables
var nav = document.getElementsByClassName("nav");
var navButton = document.getElementsByClassName("title_button");
var subnav = document.getElementsByClassName("nav-2");
var subnavButton = document.getElementsByClassName("nav-1_button");
// Initial Setup of Navigation
function navInitial() {
// If Screen Size is Small
if (screenSize.matches) {
// Initial Setup of Navigation
nav[0].classList.add("accordion");
nav[0].style.height = "0";
navButton[0].style.display = "block";
// For All Instances of Subnavigation
for (i = 0; i < subnav.length; i++) {
// Initial Setup of Subnavigation
subnav[i].classList.remove("dropdown");
subnav[i].style.height = "auto";
subnavButton[i].style.display = "none";
}
}
// If Screen Size is Medium or Large
else {
// Initial Setup of Navigation
nav[0].classList.remove("accordion");
nav[0].style.height = "auto";
navButton[0].style.display = "none";
// For All Instances of Subnavigation
for (i = 0; i < subnav.length; i++) {
// Initial Setup of Subnavigation
subnav[i].classList.add("dropdown");
subnav[i].style.height = "0";
subnavButton[i].style.display = "block";
}
}
}
// Toggle Navigation
function navToggle() {
// If Navigation is Closed
if (nav[0].style.height === "0px") {
// Open Navigation
nav[0].style.height = nav[0].scrollHeight + "px";
}
// If Navigation is Open
else {
// Close Navigation
nav[0].style.height = "0";
}
}
// Toggle Subnavigation
function subnavToggle(n) {
// If Selected Instance of Subnavigation is Closed
if (subnav[n].style.height === "0px") {
// For All Instances of Subnavigation
for (i = 0; i < subnav.length; i++) {
// Close Subnavigation
subnav[i].style.height = "0";
subnav[i].style.zIndex = "0";
}
// Open Selected Instance of Subnavigation
subnav[n].style.height = subnav[n].scrollHeight + "px";
subnav[n].style.zIndex = "1";
}
// If Selected Instance of Subnavigation is Open
else {
// Close Selected Instance of Subnavigation
subnav[n].style.height = "0";
subnav[n].style.zIndex = "0";
}
}
// When Page is Loaded
window.onload = navInitial;
// When Page is Resized
window.onresize = navInitial;
And there you have it! A responsive search and share bar.
The demonstration provided expands upon this code to show the header in a complete web page environment. It also includes extra features like tooltips, hover states, toggled icons, and button tabs.
To see it in action, open the demonstration at the beginning of this example in a new window. View the source code in your web browser to see how the page is constructed.
To customize the demonstration, first save a local copy of the document on your computer. Then, open the file in your text editor to add, remove, and modify code to create your own web design masterpiece.