App Toss to Cash apk vay tiền online
Sure, here's a simple example of how you can create a sticky header for a website using CSS and JavaScript:
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 id="header">
<!-- Your header content goes here -->
<h1>My Sticky Header</h1>
</header>
<!-- Main content of the website -->
<div id="main-content">
<p>This is the main content of the website.</p>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS (styles.css):
```css
body {
margin: 0;
font-family: Arial, sans-serif;
}
#header {
background-color: #333;
color: #fff;
padding: 10px 0;
text-align: center;
width: 100%;
position: sticky;
top: 0;
z-index: 1000; /* Ensure header stays on top of other elements */
}
#main-content {
padding: 20px;
height: 2000px; /* Just for demonstration purpose to create scrolling */
}
```
JavaScript (script.js):
```javascript
// You can add JavaScript code here if you want to add additional functionality to your sticky header.
// For example, if you want to add a class to the header when the user scrolls down.
// const header = document.getElementById('header');
// window.onscroll = function() {
// if (window.pageYOffset > 100) {
// header.classList.add('scrolled');
// } else {
// header.classList.remove('scrolled');
// }
// };
```
In this code:
- HTML defines the structure of the webpage, including the header and main content.
- CSS styles the header to be sticky at the top of the page and the main content to create enough scrollable space.
- JavaScript is commented out, but you can uncomment it and add functionality like adding a class to the header when the user scrolls down.
4.8/5 (52 votes)