How to create a mobile first header template the modern way
Modern would be using grid and flex-box. Also not using javascript to move parts around, just plain css.
Adding the same content twice to hide or show it on certain screen sizes is considered old-fashioned.
The header setup
What elements do we in general use in the header:
the ubiquitous usp’s
logo
mobile menu icon (on mobile and/or tablet)
search bar
user menu
main menu
So, let’s set up the parts in html.
The usp’s:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<divclass="header-top container"><olclass="usps flex-row"><li><strong>Fast & lightweight</strong></li><li><strong>Fully responsive</strong></li><li><strong>Accessible by default</strong></li><li><strong>Developer friendly</strong></li></ol></div>
The usp’s are a simple ordered list in it’s own container so we can use a different background color, if needed.
The mobile menu icon is set in a details/summary element which gives interaction out of the box. The logo is a svg in a link tag, nothing special there…
The main navigation uses a nav element with the links grouped in a div, the close icon uses a details/summary element with the build in interaction.
And the header structure:
1
2
3
4
5
6
7
8
9
10
11
<header><!-- usp's --><divclass="header-container container header-grid"><!-- mobile menu icon --><!-- logo --><!-- search --><!-- user menu --><!-- main menu --></div></header><mainclass="main-content"aria-label="Content"></main>
All the header components are placed in the header-grid container directly. By using css grid every item gets its place.
Below in the files is the complete html setup.