contact@codebetter.in 8823075444
9993928766
  • Tutorials Home
  • Contact

How do you create a fixed position element in CSS?

In CSS, you can create a fixed-position element by using the position property with a value of fixed. A fixed-position element is positioned relative to the viewport (the browser window) rather than its containing element, and it stays in a fixed position even when the page is scrolled.

Example:
<html>
<head>
<style>
.fixed-element {
position: fixed;
top: 20px;
left: 20px;
background-color: #f0f0f0;
padding: 10px;
}
</style>
</head>
<body>
<div class="fixed-element">
This is a fixed element. </div>
<div style="height: 400px; background-color: #ddd;">
Scroll down to see the fixed element. </div>
</body>
</html>
Output:
This is a fixed element.
Scroll down to see the fixed element.