<html>
<head>
<title>Sticky Header Height Change on Scroll</title>
</head>
<body>
<header>
<h1>My Website</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
<div class="content">
<div class="blog-roll">
<div class="blog-post">
<h2>Blog Post Title 1</h2>
<img src="https://i.ibb.co/wNvCs0v/blog-image.jpg" alt="Blog Post Image">
<div class="author-meta">
<div class="author">By <a href="#">John Smith</a></div>
<div class="date">15 Jan 2024</div>
</div>
<p class="excerpt">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus non purus et mauris convallis maximus.</p>
</div>
<div class="blog-post">
<h2>Blog Post Title 2</h2>
<img src="https://i.ibb.co/wNvCs0v/blog-image.jpg" alt="Blog Post Image">
<div class="author-meta">
<div class="author">By <a href="#">John Smith</a></div>
<div class="date">15 Jan 2024</div>
</div>
<p class="excerpt">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus non purus et mauris convallis maximus.</p>
</div>
<div class="blog-post">
<h2>Blog Post Title 3</h2>
<img src="https://i.ibb.co/wNvCs0v/blog-image.jpg" alt="Blog Post Image">
<div class="author-meta">
<div class="author">By <a href="#">John Smith</a></div>
<div class="date">15 Jan 2024</div>
</div>
<p class="excerpt">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus non purus et mauris convallis maximus.</p>
</div>
<div class="blog-post">
<h2>Blog Post Title 4</h2>
<img src="https://i.ibb.co/wNvCs0v/blog-image.jpg" alt="Blog Post Image">
<div class="author-meta">
<div class="author">By <a href="#">John Smith</a></div>
<div class="date">15 Jan 2024</div>
</div>
<p class="excerpt">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus non purus et mauris convallis maximus.</p>
</div>
<div class="blog-post">
<h2>Blog Post Title 5</h2>
<img src="https://i.ibb.co/wNvCs0v/blog-image.jpg" alt="Blog Post Image">
<div class="author-meta">
<div class="author">By <a href="#">John Smith</a></div>
<div class="date">15 Jan 2024</div>
</div>
<p class="excerpt">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus non purus et mauris convallis maximus.</p>
</div>
<div class="blog-post">
<h2>Blog Post Title 6</h2>
<img src="https://i.ibb.co/wNvCs0v/blog-image.jpg" alt="Blog Post Image">
<div class="author-meta">
<div class="author">By <a href="#">John Smith</a></div>
<div class="date">15 Jan 2024</div>
</div>
<p class="excerpt">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus non purus et mauris convallis maximus.</p>
</div>
<div class="blog-post">
<h2>Blog Post Title 7</h2>
<img src="https://i.ibb.co/wNvCs0v/blog-image.jpg" alt="Blog Post Image">
<div class="author-meta">
<div class="author">By <a href="#">John Smith</a></div>
<div class="date">15 Jan 2024</div>
</div>
<p class="excerpt">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus non purus et mauris convallis maximus.</p>
</div>
<div class="blog-post">
<h2>Blog Post Title 8</h2>
<img src="https://i.ibb.co/wNvCs0v/blog-image.jpg" alt="Blog Post Image">
<div class="author-meta">
<div class="author">By <a href="#">John Smith</a></div>
<div class="date">15 Jan 2024</div>
</div>
<p class="excerpt">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus non purus et mauris convallis maximus.</p>
</div>
<div class="blog-post">
<h2>Blog Post Title 9</h2>
<img src="https://i.ibb.co/wNvCs0v/blog-image.jpg" alt="Blog Post Image">
<div class="author-meta">
<div class="author">By <a href="#">John Smith</a></div>
<div class="date">15 Jan 2024</div>
</div>
<p class="excerpt">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus non purus et mauris convallis maximus.</p>
</div>
</div>
</div>
</body>
</html>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
font-size: 18px;
}
header {
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
background-color: #000;
color: #fff;
padding: 25px;
display: flex;
justify-content: space-between;
align-items: center;
transition: padding 0.3s ease;
}
header.scrolled {
padding: 5px 25px; /* Adjusted padding when scrolled */
}
header h1 {
margin: 0;
padding: 0;
font-size: 24px;
text-align: center;
}
nav ul {
list-style: none;
padding: 0;
text-align: center;
}
nav ul li {
display: inline;
margin: 0 10px;
}
nav ul li a {
color: #fff;
text-decoration: none;
padding: 10px 15px;
}
nav ul li a:hover {
text-decoration: underline;
}
.content {
max-width: 1180px; /* Added max-width */
width: 100%; /* Added width */
margin: auto;
}
.blog-roll {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); /* Modified */
grid-gap: 20px;
padding: 20px;
}
.blog-post {
background-color: #e9e9e9;
padding: 20px 0;
border-radius: 3px;
width: 100%; /* Added width */
}
.blog-post h2 {
margin-top: 0;
padding: 0 20px;
}
.blog-post img {
width: 100%;
}
.author-meta{
display: flex;
align-items: center;
margin: 15px 20px 20px;
font-style: italic;
font-size: 14px;
color: #888;
}
.author-meta a{
color: #666;
text-decoration: none;
}
.author-meta div{
margin: 0 10px 0 0;
}
.excerpt {
margin: 0px 20px 0;
color: #333;
}
window.addEventListener('scroll', function() {
// Select your header
var header = document.querySelector('header');
// Check if the scroll position is greater than 50 pixels
if (window.scrollY > 50) {
// Add a CSS class to reduce header size
header.classList.add('scrolled');
} else {
// Remove the CSS class to restore header size
header.classList.remove('scrolled');
}
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.