Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSS

css sticky header

<!-- https://www.w3schools.com/howto/howto_js_sticky_header.asp -->

<style>

.sticky {
  position: fixed;
  top: 0;
  width: 100%;
}

.sticky + .content {
  padding-top: 102px;
}

</style>

<script>
	window.onscroll = function() {myFunction()};

	var desktopHeader = document.getElementById("yourHeader");
	var stickyHeader = desktopHeader.offsetTop;

	function myFunction()
	{
		if(window.pageYOffset > stickyHeader) 
		{
			desktopHeader.classList.add("sticky");
		} 
		else 
		{
			desktopHeader.classList.remove("sticky");
		}
	}
</script>
Source by dev.to #
 
PREVIOUS NEXT
Tagged: #css #sticky #header
ADD COMMENT
Topic
Name
1+3 =