Jump to content

How do I make a webpage header sticky?


Diave Inoz

Recommended Posts

  • 2 weeks later...
On 6/2/2023 at 2:13 AM, Ingolme said:

CSS can do it, it's pretty simple. You can read about the CSS position:sticky; in this tutorial page: https://www.w3schools.com/css/css_positioning.asp

Sorry for my late reply but I was expecting an email when when someone responded to my post. Regarding my problem...I saw the tag "<div class="fixed-header"> on another example of how to put a sticky header on a webpage, and that worked. :)
I'm now having trouble with "jump to" links in the header not working properly, so that'll probably be another post for this forum. :)

Link to comment
Share on other sites

  • 1 year later...

To make a webpage header sticky, you can use CSS. Here’s a simple example of how to do it:

HTML

 
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Sticky Header</title> <link rel="stylesheet" href="styles.css"> </head> <body> <header class="sticky-header"> <h1>My Sticky Header</h1> </header> <main> <p>Content goes here...</p> <!-- Add more content to make the page scrollable --> </main> </body> </html>

CSS (styles.css)

 
css
body { margin: 0; font-family: Arial, sans-serif; } .sticky-header { position: -webkit-sticky; /* For Safari */ position: sticky; top: 0; background-color: #333; color: white; padding: 10px 0; text-align: center; z-index: 1000; /* Ensures the header stays above other content */ } main { padding: 20px; height: 2000px; /* Just for demonstration to make the page scrollable */ }

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...