JSFiddle - React, Tailwind, and code Playground
HTML
<div class="container"><div id="header" class="header">Hello World</div></div>
CSS
.container{
height:2000px;
width:100%;
background-color:yellow;
}
.header{
text-align:center;
background-color:red;
height:100px;
min-height:50px;
width:100%;
}
JavaScript
window.onscroll=function () {
var top = window.pageXOffset ? window.pageXOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
if(top > 50){document.getElementById("header").style.position = "fixed";
document.getElementById("header").style.height="50px"
}
else {
document.getElementById("header").style.position = "relative";
document.getElementById("header").style.height="100px"
}
}