How To Set Sticky Position For HTML Element Using CSS

admin_img Posted By Bajarangi soft , Posted On 03-11-2020

In position: sticky; is positioned based on the user's scroll position.A sticky element toggles between relative and fixed, depending on the scroll position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).So today we discuss how to do it

How To Set Sticky Position For HTML Element Using CSS

Step 1:Create index.html file and implement as below code in it.

<div class="sticky">I am sticky!</div>

Step 2:Implement CSS code as below to set p element at Sticky position.

<style>
    body {
        background: #2e9ad0;
    }
    .sticky {
        position: -webkit-sticky;
        position: sticky;
        top: 0;
        padding: 5px;
        background-color: #cae8ca;
        border: 2px solid red;
    }
</style>

Complete Code For Setting Sticky Position For HTML Element Using CSS.

<!DOCTYPE html>
<html>
<head>
    <title>How To Set Sticky Position For HTML Element Using CSS</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</head>
<style>
    body {
        background: #2e9ad0;
    }
    .sticky {
        position: -webkit-sticky;
        position: sticky;
        top: 0;
        padding: 5px;
        background-color: #cae8ca;
        border: 2px solid red;
    }
</style>
<body>
<br/><br/>
<div class="container">
    <br><br><br>
    <div class="text-center">
        <h1 id="color" style="color: white;">Set Sticky Position For HTML Element Using CSS</h1>
    </div>
    <br>
    <div class="well">

        <div class="sticky">I am sticky!</div>

    </div>
    <br>
</div>
</body>
</html>

Related Post