CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Fixed Position


When an element's position is set to fixed, it is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are then used to place the element. A fixed element does not leave a gap in the page where it would normally have been located.


Example 1: Fixed Navigation Bar

HTML

<!DOCTYPE html>
<html>
<head>
<style>
  /* Style for the body to enable scrolling */
  body {
    height: 2000px;
    margin: 0;
  }

  /* Fixed navigation bar styling */
  .fixed-nav {
    position: fixed; /* Fix the element relative to the viewport */
    top: 0;
    left: 0;
    width: 100%;
    background-color: #333;
    color: white;
    text-align: center;
    padding: 10px;
  }
</style>
</head>
<body>

<div class="fixed-nav">
  I am a fixed navigation bar!
</div>

</body>
</html>

Explanation: This code creates a navigation bar that remains at the top of the viewport when the user scrolls. The position: fixed; rule anchors the element, and top: 0; and left: 0; place it at the very top-left corner of the screen.


Example 2: Fixed "Back to Top" Button

HTML

<!DOCTYPE html>
<html>
<head>
<style>
  /* Style for the body to enable scrolling */
  body {
    height: 2000px;
  }

  /* "Back to Top" button styling */
  .back-to-top {
    position: fixed; /* Keeps the button in place during scroll */
    bottom: 20px;
    right: 20px;
    background-color: #007BFF;
    color: white;
    padding: 10px 15px;
    border-radius: 5px;
    text-decoration: none;
  }
</style>
</head>
<body>

<a href="#" class="back-to-top">Top</a>

</body>
</html>

Explanation: This example demonstrates a "Back to Top" link that is fixed to the bottom-right corner of the screen. The position: fixed; ensures it's always visible, and bottom: 20px; and right: 20px; provide spacing from the viewport edges.


Example 3: Fixed Social Media Icons Bar

HTML

<!DOCTYPE html>
<html>
<head>
<style>
  /* Style for the body to enable scrolling */
  body {
    height: 2000px;
  }

  /* Fixed social media bar styling */
  .social-bar {
    position: fixed; /* Fix the element to the viewport */
    top: 30%;
    left: 0;
    background-color: #f0f0f0;
    padding: 10px;
    border-radius: 0 5px 5px 0;
  }
</style>
</head>
<body>

<div class="social-bar">
  Social
</div>

</body>
</html>

Explanation: Here, a social media bar is fixed to the left side of the viewport. The position: fixed; rule keeps it stationary on scroll, while top: 30%; positions it vertically down from the top edge of the browser window.


Example 4: Fixed Header

HTML

<!DOCTYPE html>
<html>
<head>
<style>
  /* Style for the body to enable scrolling and provide top margin */
  body {
    height: 2000px;
    margin-top: 80px; /* Prevents content from being hidden by the fixed header */
  }

  /* Fixed header styling */
  .fixed-header {
    position: fixed; /* Fix the header to the top */
    top: 0;
    width: 100%;
    height: 60px;
    background-color: #28a745;
    color: white;
    text-align: center;
    line-height: 60px;
  }
</style>
</head>
<body>

<header class="fixed-header">
  This is a Fixed Header
</header>

<div class="content">
  <p>Scroll down to see the effect.</p>
</div>

</body>
</html>

Explanation: This code creates a fixed header at the top of the page. A margin-top is added to the <body> to prevent the initial content from being obscured by the fixed-header which is taken out of the normal document flow.


Example 5: Fixed Footer

HTML

<!DOCTYPE html>
<html>
<head>
<style>
  /* Style for the body to enable scrolling and provide bottom margin */
  body {
    height: 2000px;
    margin-bottom: 60px; /* Prevents content from being hidden by the fixed footer */
  }

  /* Fixed footer styling */
  .fixed-footer {
    position: fixed; /* Fix the footer to the bottom */
    bottom: 0;
    width: 100%;
    height: 40px;
    background-color: #6c757d;
    color: white;
    text-align: center;
    line-height: 40px;
  }
</style>
</head>
<body>

<footer class="fixed-footer">
  © 2025 Fixed Footer
</footer>

</body>
</html>

Explanation: This example places a footer that is always visible at the bottom of the viewport. position: fixed; and bottom: 0; lock it to the bottom edge of the browser window, and a margin-bottom on the body prevents content overlap.


Example 6: Fixed Alert or Cookie Banner

HTML

<!DOCTYPE html>
<html>
<head>
<style>
  /* Style for the body to enable scrolling */
  body {
    height: 2000px;
  }

  /* Fixed cookie banner styling */
  .cookie-banner {
    position: fixed; /* Fix the banner to the viewport */
    bottom: 0;
    left: 0;
    width: 100%;
    background-color: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 15px;
    text-align: center;
  }
</style>
</head>
<body>

<div class="cookie-banner">
  This website uses cookies to ensure you get the best experience.
</div>

</body>
</html>

Explanation: This creates a common cookie notification banner fixed to the bottom of the screen. The position: fixed; property ensures that the banner remains in view until it is dismissed by the user, regardless of page scrolling.


Example 7: Fixed Full-Screen Overlay

HTML

<!DOCTYPE html>
<html>
<head>
<style>
  /* Style for the body */
  body {
    margin: 0;
    font-family: sans-serif;
  }

  /* Fixed overlay styling */
  .overlay {
    position: fixed; /* Fix the overlay relative to the viewport */
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5); /* Semi-transparent background */
    z-index: 1000; /* Ensures it's on top of other content */
    color: white;
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>
</head>
<body>

<div class="overlay">
  <h1>Fixed Overlay Content</h1>
</div>

<p>Other page content is behind the overlay.</p>

</body>
</html>

Explanation: This code creates a full-screen overlay, often used for modal dialogs or loading screens. By setting position: fixed; and top, left, width, and height to cover the viewport, it effectively blocks interaction with the content underneath.