How to Create a Responsive Header with CSS?

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

You are aware of the headers on websites, and you know that no matter what the screen size, they precisely match your device. An example of such header design CSS is shown below. Observe that this header changes according to the screen size and perfectly fits the device width.

Header visible on desktop view:

  • Github (Desktop view)

    GITHUB DESKTOP VIEW

  • Tesla (Desktop view)

    TESLA DESKTOP VIEW

Header visible on mobile view:

  • Github (Mobile view)

    GITHUB MOBILE VIEW

  • Tesla (Mobile view)

    TESLA MOBILE VIEW

And, many people nowadays prefer to use mobile phones instead of desktops to surf through websites. The headers play an important role to navigate on the entire website and thus it should look clean on all the devices. In this section, we are going to learn how you can too create the header design CSS so that it can look good on all sizes of devices.

Pre-Requisites

The Technique

There is not only a single correct way but many ways to design and build a header design CSS.

In this article, we are going to build a header using CSS flexbox. The header is separated into three sections and there are some content or links available inside the header to navigate through the website.

The three sections include:

  • Middle section: Contains hyperlinks to navigate throughout the website.

    THE TECHNIQUE ONE

  • Left section: A company logo or the branding of the company.

    THE TECHNIQUE TWO

  • Right section: A call to action button.

    THE TECHNIQUE THREE

Although this is not the only way to design header CSS, in this article we are going to create such headers which follow the above given technique.

The Code - HTML, CSS, and JavaScript

Now, let's take an example of the header design CSS.

HTML

The above HTML code produces the below output. As shown in the output below, there are no designs or colors present in the header design CSS. As well as, the header is not fully structured appropriately.

Output: HTML HEADER

You can apply CSS as shown below to add design, color, and alignment of the header.

CSS

The output of the header with the CSS file attached to it is shown below:

Output:

CSS HEADER

As you can see in the image below, the header is not responsive yet. When you decrease the screen size the header design CSS breaks and all the content of the header does not align as it was before.

Output:

HEADER SIZE

So, to make it responsive, you need to add a little bit of more CSS which is used to add responsive behavior to the header.

Adding responsiveness

Adding below CSS code to the above programs adds responsiveness to the header design CSS. When the screen size reduces, the header takes a new shape with the hamburger menu.

Responsiveness

Output:

HEADER DESIGN

As shown in the output above, the header is restructured according to the responsive design added to the CSS code. The JavaScript is used to open and close the hamburger menu which is shown in the code below:

JavaScript

Output:

menu button.

Conclusion

  • In this article, you learned how to create a responsive header design CSS.
  • Responsiveness in the header can be included using various CSS tricks like the flexbox method, media queries, etc.
  • In the example above, we have used media queries and JS functionality to add a responsive behavior to the header.