Open In App

How to set fixed position but relative to container in CSS ?

Last Updated : 02 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In CSS, setting an element’s position as “fixed” relative to a container, rather than the entire viewport, can be done by using a combination of CSS properties. While position: fixed works relative to the viewport, there are other methods like position: sticky and position: absolute that can help achieve the desired behavior within a container.

Approach 1: Using position: sticky

position: sticky allows an element to act like a normal element until it reaches a specified offset value. Once the offset is reached, the element “sticks” in place as the page continues to scroll.

HTML
<!--Driver Code Starts{-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sticky Positioning Example</title>
<!--Driver Code Ends }-->

    <style>
        .container {
            height: 300px;
            width: 300px;
            border: 2px solid #333;
            overflow-y: scroll;
            position: relative;
        }

        .sticky-header {
            position: sticky;
            top: 0;
            background-color: green;
            padding: 10px;
            color: white;
            text-align: center;
        }

        .content {
            padding: 10px;
        }
    </style>
</head>
<body>

<div class="container">
    <div class="sticky-header">I Stay Fixed When Scrolling!</div>
    <div class="content">
        <p>Scroll down to see the sticky behavior.</p>
        <p>Sticky elements remain fixed within their container.</p>
    </div>
</div>

</body>
</html>

  • The .sticky-header remains at the top of the container as the user scrolls.
  • The sticky element stays confined to the parent container’s boundaries..

Approach 2: Using position: absolute with position: relative on Parent

To position an element relative to its container, use position: absolute on the child element and position: relative on the parent container. This method allows you to position the child element anywhere within the container.

HTML
<!--Driver Code Starts{-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Absolute Positioning Example</title>
<!--Driver Code Ends }-->

    <style>
        .container {
            position: relative;
            width: 300px;
            height: 300px;
            border: 2px solid #333;
            overflow: hidden;
        }

        .absolute-element {
            position: absolute;
            bottom: 10px;
            right: 10px;
            background-color: #20b2aa;
            padding: 10px;
            color: white;
        }
    </style>
</head>
<body>

<div class="container">
    <div class="absolute-element">I am Fixed Relative to My Container!</div>
</div>

<!--Driver Code Starts{-->

</body>
</html>

<!--Driver Code Ends }-->
  • The .absolute-element is positioned at the bottom-right of the .container regardless of scrolling.
  • The child element stays within the container’s boundaries and moves with it if the container is scrolled.

Approach 3: Using position: fixed (Viewport-based)

position: fixed positions an element relative to the viewport, not the container. It is fixed in place even when the page is scrolled. While this approach doesn’t work relative to the container, it’s helpful to understand how it behaves.

HTML
<!--Driver Code Starts{-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Fixed Position Example</title>
<!--Driver Code Ends }-->

    <style>
        .fixed-element {
            position: fixed;
            bottom: 10px;
            right: 10px;
            background-color: #ff4500;
            padding: 10px;
            color: white;
        }
    </style>
</head>
<body>

<div class="fixed-element">I am Fixed in the Viewport!</div>


<!--Driver Code Starts{-->
</body>
</html>

<!--Driver Code Ends }-->
  • The .fixed-element stays at the bottom-right of the viewport, not the container.
  • The element stays in the same position on the screen, even when the page is scrolled.


Next Article

Similar Reads