Sticky Header on Scroll Only HTML and CSS

Sticky Header on Scroll Only HTML and CSS


How to Create a Sticky Header?

today I learn how to create a sticky header on scroll only HTML and CSS.

Sticky Header In Only CSS

First, we will create a header then we will add an ID in it and add JavaScript on top of the last body clause.

Follow Step:-

Step 1:- Create index.html File and add Create a Custom Header.

<!DOCTYPE html>
<html>
<head>
	<title>sticky Header</title>
</head>

<body>

<h1>sticky header on scroll only HTML and CSS</h1>

<nav id="myHeader">
	<ul>
		<li><a href="#">Home</a></li>
		<li><a href="#">about</a></li>
		<li><a href="#">Quality</a></li>
		<li><a href="#">Contact</a></li>
	</ul>
</nav>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

</body>
</html>

Step 2:- Add CSS Style in this page

<style>
	*{
		margin: 0;
		padding: 0;
		box-sizing: border-box;
	}
	h1{
		padding: 150px;
		text-align: center;
		font-weight: 700;
		font-size: 50px;
	}
	p{
		padding: 150px 500px;
		text-align: center;
	}
	nav{
		background: #606060;
		padding: 50px;
	}
	nav ul li{
		display: inline;
		list-style: none;
		padding: 20px;
	}
	nav ul li a{
		text-decoration: none;
		color: #fff;
	}

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

	.sticky + .content {
 	padding-top: 102px;
	}
</style>

Step 3:- Add JavaScript on top of the last body clause.

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

var header = document.getElementById("myHeader");
var sticky = header.offsetTop;

function myFunction() {
  if (window.pageYOffset > sticky) {
    header.classList.add("sticky");
  } else {
    header.classList.remove("sticky");
  }
}
</script>

Demo Available How to Create a Sticky Header on your HTML Page.


Related Posts

One thought on “Sticky Header on Scroll Only HTML and CSS

Leave a Reply

Your email address will not be published. Required fields are marked *