Lãi suất ngân hàng VPBank mới nhất
Sure, here's a basic example of how you can create a sticky header 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 Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header id="header">
<h1>Sticky Header Example</h1>
</header>
<div class="content">
<!-- Your content here -->
</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: fixed;
top: 0;
left: 0;
z-index: 1000;
}
.content {
margin-top: 60px; /* Adjust this margin to compensate for the header height */
padding: 20px;
}
```
JavaScript (script.js):
```javascript
// JavaScript is optional if you only need a sticky header, but you can use it for additional functionality
// If you want the header to be sticky only after a certain scroll position, you can use JavaScript like this:
window.addEventListener('scroll', function() {
const header = document.getElementById('header');
if (window.scrollY > 50) { // Adjust the scroll position as needed
header.classList.add('sticky');
} else {
header.classList.remove('sticky');
}
});
```
In this code:
- HTML provides the structure of the page.
- CSS is used for styling, including making the header sticky using `position: fixed`.
- JavaScript (optional) is used here to make the header sticky after scrolling past a certain point. If you don't need this behavior, you can remove the JavaScript part.
4.9/5 (117 votes)