You are on page 1of 3

Certainly!

Below is an example of a simple HTML, CSS, and JavaScript code snippet for creating a sticky
header on a website:

```html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>

body {

margin: 0;

font-family: Arial, sans-serif;

header {

background-color: #333;

color: white;

padding: 15px;

text-align: center;

position: fixed;

width: 100%;

top: 0;

transition: top 0.3s;

section {

padding: 50px;

}
</style>

</head>

<body>

<header id="header">

<h1>Sticky Header</h1>

</header>

<section>

<h2>Main Content</h2>

<p>This is the main content of your website.</p>

</section>

<script>

window.onscroll = function() {

var header = document.getElementById('header');

if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {

header.style.top = "0";

} else {

header.style.top = "-50px";

};

</script>

</body>

</html>

```
This code creates a basic webpage with a sticky header. The header becomes visible when you scroll
down and hides when you scroll up. You can customize the styles and behavior according to your
specific needs.

You might also like