How to adjust the title size while scrolling
- Previous Page Sticky Header
- Next Page Pricing Table
Learn how to use CSS and JavaScript to shrink the title while scrolling.
How to shrink the title while scrolling
First step - Add HTML:
Create the page header:
<div id="header">Header</div>
Second step - Add CSS:
Set the style of the page header:
#header { background-color: #f1f1f1; /* Gray background */ padding: 50px 10px; /* Some padding */ color: black; text-align: center; /* Centered text */ font-size: 90px; /* Large font size */ font-weight: bold; position: fixed; /* Fixed position - at the top of the page */ top: 0; width: 100%; /* Full width */ transition: 0.2s; /* Add transition effect (reduce font size during scrolling) */ }
Third step - Add JavaScript:
// When the user scrolls down 50px from the top of the document, adjust the font size of the title window.onscroll = function() {scrollFunction()}; function scrollFunction() { if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 50) { document.getElementById("header").style.fontSize = "30px"; } document.getElementById("header").style.fontSize = "90px"; } }
- Previous Page Sticky Header
- Next Page Pricing Table