Home

>

Prompts Database

>

Website building
Sticky Header
avatar
Buffy Summers
Share
Leave a comment
623 days ago

Prompt type:

Create website, Write HTML code, Write code

Category:

Website building, Web Development

Media type:

Use with AI:

ChatGPT from OpenAI
View Full Output

Example output

Sure! Implementing a sticky header using CSS and JavaScript is a common feature in web development. Here's an example of how you can achieve it:

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 Example</title> <link rel="stylesheet" href="styles.css"> </head> <body> <header id="header"> <h1>Sticky Header Example</h1> </header> <main> <!-- Your main content here --> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec euismod magna sit amet massa posuere tincidunt. Integer et consequat urna. Duis vel purus id libero accumsan rhoncus. Ut auctor arcu sed metus tincidunt, eget condimentum lectus molestie. Sed nec orci vitae nulla feugiat lobortis.</p> <!-- Add more content as needed --> </main> <script src="script.js"></script> </body> </html>

CSS (styles.css):

body { font-family: Arial, sans-serif; margin: 0; padding: 0; } #header { background-color: #333; color: #fff; padding: 20px; text-align: center; width: 100%; position: fixed; /* Fixed position to make it sticky */ top: 0; /* Stick to the top */ z-index: 1000; /* Ensures the header stays on top of other content */ } main { margin-top: 80px; /* Adjust according to header height */ padding: 20px; }

This code creates a simple web page with a sticky header. The header remains fixed at the top of the viewport as the user scrolls down the page. You can further enhance this with additional CSS styles or JavaScript functionality as per your requirements.

Description or instructions

This prompt involves implementing a sticky header on a website using CSS and JavaScript.

Prompt

I want to implement a sticky header on my website. Can you provide an example of how to do that using CSS and JavaScript?