Page 1: Responsive Web Page Tutorial, HTML/CSS Only
Page 2: Page Structure using Flexbox and a Media Query
Page 3: Footer (nested flexboxes)
Page 4: Header (menu/search toggles, transition effects)
Page 5: Sticky Aside
Page 6: Make it Beautiful
Page 7: JavaScript Efficiencies, Breadcrumbs, Cats, Audio and More
Page 8: Embedded Note from Creator and Floating Hearts
Page 9: Move On To, Video Tutorial

Header: Menu/Search Toggles, Transition Effects

The header has a logo, links, and search. On small screens, a ‘hamburger menu’ (triple bar icon) gives access to the links. There are transition effects when opening/closing the menu and search, for an improved user experience.

(1a) Small screens, initial display:
Header, small screens

(1b) Small screens, hamburger menu open:
Header, small screens, hamburger menu open

(1c) Small screens, menu and search open:
Header, small screens, menu and search open

(1d) Small screens, search only open:
Header, small screens, search only open

(1e) Wider screens, hamburger menu gone, links wrapping:
Header, wider screens, hamburger menu gone, links wrapping

(1f) Widest screens, search open:
Header, widest screens, search open

Code Chunk 4 replaces the  <header></header>  container in Code Chunk 2. Only skeleton code is given below; actual links and icons were inserted for the images above.

CODE CHUNK 4:
<header>
  <div id="headerLogo">logo</div>
  <nav id="headerNav">
    <a>link1</a> <!-- repeat line as needed -->
  </nav>
  <div id="headerHamburgerIcon">
    <img onclick="toggle('headerNavMobileOnly','flex');" src="source" />
  </div>
  <div id="headerSearchIcon">
    <img onclick="toggle('headerSearchForm','block');" src="source" />
  </div>
</header>
<nav id="headerNavMobileOnly">&nsbp;</nav>
<script>
  document.getElementById('headerNavMobileOnly').innerHTML = document.getElementById('headerNav').innerHTML;
</script>
<form id="headerSearchForm" style="display:none;">
  <!-- search form contents -->
</form>
The script that toggles the menu and search:
<script>
function toggle(theID,attributeToToggle) {
   var theElmt = document.getElementById(theID);
   if (theElmt.style.display === "none") {
     theElmt.style.display = attributeToToggle;
   } else {
     theElmt.style.display = "none";
   }
}
</script>

Here's CSS styling for Code Chunk 4:
style for Code Chunk 4

More CSS styling for Code Chunk 4:
more style for Code Chunk 4

Notes on Code Chunk 4 and its CSS:

Transition Effects

When things (like menus and search boxes) appear and disappear instantly, it can be a bit disorienting to a user. It's a much better user experience if things appear/disappear gradually—enter CSS transitions.

 

For example, to make the green square appear/disappear instantly, click here. A bit disconcerting, isn't it?

For comparison, make it fade in/out by clicking here. Isn't that a little easier on the eyes?

To make our menu items and search box have a similar fade in/out effect, replace the previous  toggle  function and add two new functions,  transitionIn  and  transitionOut:

functions for transition effects

Also add the new CSS:

functions for transition effects

A CSS transition can be coded as:  transition: attribute1 time1, attribute2 time2;

A CSS transition is applied whenever a specified attribute is changed. For example, whenever the value for  opacity  is changed, then it will transition from the current value to new value in 1.5 seconds. For us, the value is changed when (say) the hamburger menu is clicked, causing the  toggle  function to be called.

Lurking in the background, there is a transition-timing-function that describes the rate at which a numerical value changes (like  opacity  going from 0 to 1). The default  transition-timing-function  is  ease, which is equivalent to cubic-bezier(0.25, 0.1, 0.25, 1.0).

The thing that's tricky here is the ‘setTimeout’ stuff. Transitioning to/from  display:none;  is known to be problematic (search for ‘display: none’ on this page). We need to insert some brief pauses to assure that things work as expected. Here's the logic:

The Responsive Web Page, So Far!

Here's our responsive web page thus far. Change its size and watch the header/footer/main sections respond accordingly. Click on the hamburger menu and search icon. There has been no professional styling yet; we've only put in enough CSS to make things work.

Go on to Page 5 of the Responsive Page Tutorial