How to create sticky header bar for a website
Asked Answered
J

5

15

I want to create a sticky header bar for a website just like the sticky header on this website (http://www.fizzysoftware.com/) if any on can can help me out with coding or any resource that helps me to create the same. Your reply would be of great help to me.

Jerol answered 3/11, 2012 at 19:22 Comment(4)
possible duplicate of Sticky header CSS / JSJunto
Possible duplicate of Sticky Header after scrolling downClapper
This should not be considered the duplicate because it's the only one of the three with a good answer.Volley
check my medium article here which should be helpful :)Latif
S
18

In your CSS, add

position: fixed;

to your header element. It's just that simple, really. And next time, try to use right click on something you see on website and choose "Inspect element". I think that every modern browser has it now. Very useful function.

Seafowl answered 3/11, 2012 at 19:32 Comment(0)
C
8

If you want to make it sticky when it's scroll down to a certain point then you can use this function:

$window = $(window);
$window.scroll(function() {
  $scroll_position = $window.scrollTop();
    if ($scroll_position > 300) { // if body is scrolled down by 300 pixels
        $('.your-header').addClass('sticky');

        // to get rid of jerk
        header_height = $('.your-header').innerHeight();
        $('body').css('padding-top' , header_height);
    } else {
        $('body').css('padding-top' , '0');
        $('.your-header').removeClass('sticky');
    }
 });

And sticky class:

.sticky {
  position: fixed;
  z-index: 9999;
  width: 100%;
}

You can use this plugin and it has some useful options

jQuery Sticky Header

Chan answered 10/1, 2016 at 20:26 Comment(0)
M
7

CSS already gives you the answer. Try this out

.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
}

now add the class sticky to any menu sidebar or anything you want to stick to the top and it will automatically calculate the margin and stick to the top. Cheers.

Muttonchops answered 3/9, 2020 at 22:41 Comment(0)
L
0

If you want simplicity in a HTML and CSS option to create a Stiky NavBar you can use the following: Just create a navbar like this one:

<nav class="zone blue sticky">
      <ul class="main-nav">
          <li><a href="">About</a></li>
          <li><a href="">Products</a></li>
          <li><a href="">Our Team</a></li>
          <li class="push"><a href="">Contact</a></li>
  </ul>
  </nav>

Remember to add the classes in this case I created a Zone (to separate my HTML in specific areas I want my CSS to be applied) blue (just a color for the nav) and sticky which is the one that gonna carry our sticky function. You can work on other attributes you want to add is up to you. On the CSS add the following to create the sticky; first I am gonna start with the zone tag

.zone {
    /*padding:30px 50px;*/
    cursor:pointer;
    color:#FFF;
    font-size:2em;
    border-radius:4px;
    border:1px solid #bbb;
    transition: all 0.3s linear;
}

now with the sticky tag

.sticky {
  position: fixed;
  top: 0;
  width: 100%;
}

Position fixed meaning it will always be in the same position; and with top 0 I will always be at the top and a 100% width so it covers the whole screen. And now the color to make our navbar blue

.blue {
    background: #7abcff;

You can use this example to create a sticky navbar of yours and play around with the CSS properties to customize it to your liking.

Lanellelanette answered 17/11, 2020 at 15:7 Comment(0)
N
0

Try This

Add this style to the corresponding

style="position: fixed; width: -webkit-fill-available"

OR

<style>
.className{
    position: fixed; 
    width: -webkit-fill-available;
}
</style>
Neelyneeoma answered 8/11, 2022 at 2:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.